Exemple #1
0
        public Move makeMove(int yStart, int xStart, int yEnd, int xEnd)
        {
            Piece start = board.getCellContents(yStart, xStart);
            Piece end   = board.getCellContents(yEnd, xEnd);

            if (start == null)   //there is no piece here
            {
                throw new CellEmptyException();
            }
            if (end != null)
            {
                throw new CellFullException();
            }
            if (start.getColor() != whoseMove())
            {
                throw new PieceWrongColorException();
            }

            PieceType originalPieceType = start.getType();

            System.Diagnostics.Debug.WriteLine("makeMove called");
            Move myMove = getMove(start, yEnd, xEnd, this.whoseMove());

            doMove(myMove);
            successMoves.Add(new MoveAttempt(yStart, xStart, yEnd, xEnd));
            movesMade.Add(myMove);

            Piece add = myMove.getAdditions()[0];

            if (originalPieceType != add.getType())
            {
                lastAdvantage = turnNumber;
            }
            if (myMove.getRemovals().Count > 1)   //that means a piece has been taken
            {
                lastAdvantage = turnNumber;
            }

            if (originalPieceType == add.getType() && myMove.getRemovals().Count == 2 && getDoableJumps(add).Count != 0)
            {
                this.multiJumpLoc = myMove.getAdditions()[0].getCoordinates();
                //don't change turnNumber
            }
            else
            {
                this.turnNumber++;
                this.multiJumpLoc = null;
            }
            this.moveNumber++;
            //getOptimizedHeuristic(0, 3, new GameLogic(this));
            return(myMove);
        }
Exemple #2
0
        private List <Vector> getDoableJumps(Piece p)
        {
            Vector[]      jumps  = getPossibleJumps(p.getColor(), p.getType());
            List <Vector> doable = new List <Vector>();

            foreach (Vector jump in jumps)
            {
                Vector endLoc = jump.add(p.getCoordinates());
                Piece  endP;
                try {
                    endP = board.getCellContents(endLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (endP != null)
                {
                    continue;
                }
                Vector middleLoc = p.getCoordinates().add(jump.divideVector(2));
                Piece  middleP;
                try {
                    middleP = board.getCellContents(middleLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (middleP == null)
                {
                    continue;
                }

                if (middleP.getColor() == p.getColor())
                {
                    continue;
                }
                doable.Add(new Vector(jump));
            }

            return(doable);
        }
Exemple #3
0
        private List <Vector> getDoableMoves(Piece p)
        {
            Vector[]      moves  = getPossibleMoves(p.getColor(), p.getType());
            List <Vector> doable = new List <Vector>();

            foreach (Vector move in moves)
            {
                Vector endLoc = move.add(p.getCoordinates());
                Piece  endP;
                try {
                    endP = board.getCellContents(endLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (endP != null)
                {
                    continue;
                }
                doable.Add(new Vector(move));
            }

            return(doable);
        }
        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);
        }
        private List<Vector> getDoableMoves(Piece p)
        {
            Vector[] moves = getPossibleMoves(p.getColor(), p.getType());
            List<Vector> doable = new List<Vector>();

            foreach (Vector move in moves) {
                Vector endLoc = move.add(p.getCoordinates());
                Piece endP;
                try {
                    endP = board.getCellContents(endLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (endP != null) {
                    continue;
                }
                doable.Add(new Vector(move));
            }

            return doable;
        }
        private List<Vector> getDoableJumps(Piece p)
        {
            Vector[] jumps = getPossibleJumps(p.getColor(), p.getType());
            List<Vector> doable = new List<Vector>();

            foreach (Vector jump in jumps) {
                Vector endLoc = jump.add(p.getCoordinates());
                Piece endP;
                try {
                    endP = board.getCellContents(endLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (endP != null) {
                    continue;
                }
                Vector middleLoc = p.getCoordinates().add(jump.divideVector(2));
                Piece middleP;
                try {
                    middleP = board.getCellContents(middleLoc);
                } catch (CellOutOfBoundsException) {
                    continue;
                }
                if (middleP == null) {
                    continue;
                }

                if (middleP.getColor() == p.getColor()) {
                    continue;
                }
                doable.Add(new Vector(jump));
            }

            return doable;
        }
Exemple #7
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));
        }