Example #1
0
        private static void handleMove(Move move)
        {
            List <Piece> added   = move.getAdditions();
            List <Piece> removed = move.getRemovals();

            foreach (Piece p in removed)
            {
                Vector co = p.getCoordinates();
                delete(co.getX(), co.getY());
            }

            foreach (Piece p in added)
            {
                Vector co = p.getCoordinates();

                int     col = co.getX();
                int     row = co.getY();
                Checker c   = new Checker(col, row, p.getColor() == PieceColor.BLACK ? DarkGrey : Colors.Red,
                                          p.getColor() == PieceColor.BLACK ? Colors.Black : DarkRed);
                spaces[col, row].setChecker(c);
                mainCanvas.Children.Add(spaces[col, row].getChecker().getEl2());
                mainCanvas.Children.Add(spaces[col, row].getChecker().getEl1());
                mainCanvas.Children.Add(spaces[col, row].getChecker().getCrown());

                if (p.getType() == PieceType.KING)
                {
                    c.king();
                }
            }
        }
Example #2
0
        public override bool Equals(Object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }
            Vector v = (Vector)obj;

            return(this.x == v.getX() && this.y == v.getY());
        }
Example #3
0
        public MoveAttempt getAnyDoableMoveJump()
        {
            if (multiJumpLoc != null)
            {
                Vector doable = getDoableJumps(board.getCellContents(multiJumpLoc))[0];
                return(new MoveAttempt(multiJumpLoc.getY(), multiJumpLoc.getX(),
                                       multiJumpLoc.getY() + doable.getY(), multiJumpLoc.getX() + doable.getX()));
            }
            MoveAttempt jump = getAnyDoableJumpAttempt();

            if (jump != null)
            {
                return(jump);
            }
            MoveAttempt move = getAnyDoableMoveAttempt();

            if (move != null)
            {
                return(move);
            }
            throw new NoMovesLeftException();
        }
 private bool moveIsJump(Vector start, int yEnd, int xEnd)
 {
     return (Math.Abs(start.getX() - xEnd) == 2 && Math.Abs(start.getY() - yEnd) == 2);
 }
 public Piece getCellContents(Vector v)
 {
     return getCellContents(v.getY(), v.getX());
 }
 //copy Constructor:
 public Vector(Vector copyable)
 {
     this.x = copyable.getX();
     this.y = copyable.getY();
 }
 public MoveAttempt(Vector v, Piece p)
     : this(p.getCoordinates().getY(), p.getCoordinates().getX(),
         v.getY() + p.getCoordinates().getY(), v.getX() + p.getCoordinates().getX())
 {
 }
Example #8
0
 private bool moveIsJump(Vector start, int yEnd, int xEnd)
 {
     return(Math.Abs(start.getX() - xEnd) == 2 && Math.Abs(start.getY() - yEnd) == 2);
 }
Example #9
0
 public MoveAttempt(Vector v, Piece p)
     : this(p.getCoordinates().getY(), p.getCoordinates().getX(),
            v.getY() + p.getCoordinates().getY(), v.getX() + p.getCoordinates().getX())
 {
 }
Example #10
0
 public Piece getCellContents(Vector v)
 {
     return(getCellContents(v.getY(), v.getX()));
 }
Example #11
0
 //copy Constructor:
 public Vector(Vector copyable)
 {
     this.x = copyable.getX();
     this.y = copyable.getY();
 }
Example #12
0
        private Move getMove(Piece start, int yEnd, int xEnd, PieceColor player)
        {
            List <Piece> removals  = new List <Piece>();
            List <Piece> additions = new List <Piece>();

            if (multiJumpLoc != null)
            {
                if (multiJumpLoc.Equals(start.getCoordinates()) && moveIsJump(start.getCoordinates(), yEnd, xEnd))
                {
                }
                else
                {
                    throw new WrongMultiJumpPieceException();
                }
            }

            System.Diagnostics.Debug.WriteLine("start vector is " + start.getCoordinates().ToString());

            Vector startLoc = start.getCoordinates();
            Vector endLoc   = new Vector(yEnd, xEnd);
            Vector myMove   = endLoc.subtract(startLoc);

            //jump logic goes here
            if (this.forceJumps && canJumpSomewhere())
            {
                List <Vector> jumps = getDoableJumps(start);
                bool          found = false;
                foreach (Vector jump in jumps)
                {
                    if (jump.Equals(myMove))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    throw new PlayerMustJumpException();
                }
            }


            System.Diagnostics.Debug.WriteLine("myMove is " + myMove.ToString());
            bool foundValid = false;

            if (Math.Abs(myMove.getX()) == 1 && Math.Abs(myMove.getY()) == 1)  //move is not a jump
            {
                System.Diagnostics.Debug.WriteLine("Move is not a jump.");
                Vector[] moves = getPossibleMoves(start.getColor(), start.getType());
                foreach (Vector move in moves)
                {
                    System.Diagnostics.Debug.WriteLine("testing possible move " + move.ToString());
                    if (myMove.Equals(move))
                    {
                        removals.Add(start);
                        additions.Add(givePieceNewLocationKingCheck(start, start.getCoordinates().add(myMove)));
                        foundValid = true;
                        break;
                    }
                }
            }
            else if (Math.Abs(myMove.getX()) == 2 && Math.Abs(myMove.getY()) == 2)     //move is a jump
            {
                Vector[] moves = getPossibleJumps(start.getColor(), start.getType());
                foreach (Vector move in moves)
                {
                    if (myMove.Equals(move))
                    {
                        Vector jumpedLoc   = start.getCoordinates().add(move.divideVector(2));
                        Piece  jumpedPiece = board.getCellContents(jumpedLoc);
                        if (jumpedPiece == null)
                        {
                            System.Diagnostics.Debug.WriteLine("cannot jump an empty square");
                            throw new InvalidMoveException();
                        }
                        if (jumpedPiece.getColor() == getOppositeColor(start.getColor()))
                        {
                            removals.Add(start);
                            removals.Add(jumpedPiece);
                            additions.Add(givePieceNewLocationKingCheck(start, endLoc));
                            foundValid = true;
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("cannot jump your own piece");
                            throw new InvalidMoveException();
                        }
                        break;
                    }
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("vector is wrong length");
                throw new InvalidMoveException();
            }

            if (!foundValid)
            {
                System.Diagnostics.Debug.WriteLine("Could not find match vector");
                throw new InvalidMoveException();
            }
            int myTurnNumber = turnNumber + 1;

            return(new Move(myTurnNumber, removals, additions, player));
        }