Exemple #1
0
        // Searches for a new best move
        public override Move getInput(Board B)
        {
            MinMax minMaxSearch = new MinMax();
            Move bestMove;

            put(0);

            bestMove = minMaxSearch.runMinMax(B, colour, getSearchDepth(B), true, MinMax.MINIMUM, MinMax.MAXIMUM, put).Item1;

            if (bestMove != null)
            {
                if (bestMove.Equals(secondToLastMove))
                {
                    repetitionMod += 1;
                }
                else
                {
                    repetitionMod = 0;
                }

                secondToLastMove = lastMove;
                lastMove = bestMove;

                return bestMove;
            }
            else
            {
                repetitionMod += 1;
                return new Move();
            }
        }
Exemple #2
0
        // Constructor for Game
        public Game()
        {
            board = new Board();
            fileMonitor = new FileMonitor();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            gui = new ChessForms.GUI(start, pauseUnpause, reset, saveGame, loadGame);

            // Load last game
            bool ok = SaveManager.loadCurrent(ref board);
            if (ok)
            {
                // Set GUI and other stuff
                gui.putString("Loading last game");
                updateOnLoad();
            }
            else
            {
                reset();
            }

            gui.updateBoard(board);
            Application.Run(gui);
        }
Exemple #3
0
        public static List<Tuple<uint, uint>> getPossibleMoves(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> moves = new List<Tuple<uint, uint>>();
            int px = (int) piece.getX();
            int py = (int) piece.getY();

            // Loops over jump locations
            foreach (Tuple<int, int> item in jmpList)
            {
                int jmpX = item.Item1 + px;
                int jmpY = item.Item2 + py;
                if (board.withinBoard(jmpX, jmpY))
                {
                    Piece P = board.getPieceAt((uint) jmpX, (uint) jmpY);
                    if (P != null)
                    {
                        if (!piece.isSameColour(P))
                        {
                            moves.Add(new Tuple<uint, uint>((uint)jmpX, (uint)jmpY));
                        }
                    }
                    else
                    {
                        moves.Add(new Tuple<uint, uint>((uint)jmpX, (uint)jmpY));
                    }
                }
            }

            // Filter for check situations
            CommonRules.checkFilter(ref moves, board, piece);

            return moves;
        }
Exemple #4
0
        // Get all possible moves for a piece.
        public static List<Tuple<uint, uint>> getPossibleMoves(Board board, Piece piece)
        {
            if (piece is Pawn)
            {
                return PawnRules.getPossibleMoves(board, piece);
            }
            else if (piece is Rook)
            {
                return RookRules.getPossibleMoves(board, piece);
            }
            else if (piece is Knight)
            {
                return KnightRules.getPossibleMoves(board, piece);
            }
            else if (piece is Bishop)
            {
                return BishopRules.getPossibleMoves(board, piece);
            }
            else if (piece is Queen)
            {
                return QueenRules.getPossibleMoves(board, piece);
            }
            else if (piece is King)
            {
                return KingRules.getPossibleMoves(board, piece);
            }

            // Should not get here!
            return new List<Tuple<uint, uint>>();
        }
Exemple #5
0
        // --- Graphics interface ---
        // Functions for printing the text and image versions of the board.

        // Update the board and render the GUI. Also reset the selected move.
        public void updateBoard(src.Board board)
        {
            this.board = board;
            drawControl.setBoard(board);

            selectedMove = new src.Move();

            renderGUI();
        }
Exemple #6
0
        // Gets move at row turn in file and makes it
        public override Move getInput(Board B)
        {
            System.Threading.Thread.Sleep(sleepTime);

            uint turn = B.getTurn() - 1;
            if (turn <= moves.Count) {
                Move move = moves.ElementAt((int) turn);
                return move;
            }
            return new Move();
        }
Exemple #7
0
 //Checks if move is possible
 public static bool movePossible(Board board, Piece piece, Move move)
 {
     List<Tuple<uint, uint>> tmp;
     tmp = getPossibleMoves(board, piece);
     foreach (Tuple<uint, uint> item in tmp)
     {
         if ((item.Item1 == move.ToX) &&
             (item.Item2 == move.ToY))
         {
             return true;
         }
     }
     return false;
 }
Exemple #8
0
        // Returns the depth of the search
        private uint getSearchDepth(Board B)
        {
            if (!lateGameMod && B.getNumPieces() < 10)
            {
                lateGameMod = true;
            }

            uint depth = difficulty;
            if (lateGameMod)
            {
                depth += 1;
            }

            depth += repetitionMod;

            return depth;
        }
Exemple #9
0
        // Takes input from textbox converts it to correctly formatted move
        public override Move getInput(Board B)
        {
            string inp = "";
            inp = read();
            if (inp.Length > 3)
            {
                uint xFrom = decodeInput(inp[0]);
                uint yFrom = decodeInput(inp[1]);
                uint xTo = decodeInput(inp[2]);
                uint yTo = decodeInput(inp[3]);

                return new Move(xFrom, yFrom, xTo, yTo);
            }
            else
            {
                return new Move(100, 100, 100, 100);
            }
        }
Exemple #10
0
        public static List<Tuple<uint, uint>> getCover(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> cover = new List<Tuple<uint, uint>>();
            int px = (int)piece.getX();
            int py = (int)piece.getY();

            // Loops over jump locations
            foreach (Tuple<int, int> item in jmpList)
            {
                int jmpX = item.Item1 + px;
                int jmpY = item.Item2 + py;
                if (board.withinBoard(jmpX, jmpY))
                {
                    cover.Add(new Tuple<uint, uint>((uint)jmpX, (uint)jmpY));
                }
            }

            return cover;
        }
Exemple #11
0
        // Returns a list of all covered squares
        public static List<Tuple<uint, uint>> getCover(Board board, Piece piece)
        {
            int x, y;
            int yMod = (piece.getColour() == "white" ? 1 : -1);
            List<Tuple<uint, uint>> cover = new List<Tuple<uint, uint>>();

            x = (int)piece.getX() - 1;
            y = (int)piece.getY() + yMod;
            if (board.withinBoard(x, y))
            {
                cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
            }

            x = (int)piece.getX() + 1;
            if (board.withinBoard(x, y))
            {
                cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
            }

            return cover;
        }
Exemple #12
0
 // Returns true if cover on king of colour "col".
 public static bool getCheck(Board board, string col)
 {
     Square kingSquare = null;
     for (uint j = 0; j < 8; j++)
     {
         for (uint i = 0; i < 8; i++)
         {
             kingSquare = board.getSquareAt(i, j);
             if (kingSquare.getPiece() is King &&
                 kingSquare.getPiece().getColour() == col)
             {
                 break;
             }
         }
         if (kingSquare.getPiece() is King &&
                 kingSquare.getPiece().getColour() == col)
         {
             break;
         }
     }
     return (kingSquare.getEnemyCover(col));
 }
Exemple #13
0
        // Check if a move is possible on a specific board.
        public static bool movePossible(Board board, Move move, string activePlayer)
        {
            // Check if piece at move.from
            Piece p = board.getPieceAt(move.FromX, move.FromY);
            if (p == null)
            {
                return false;
            }

            // Check if correct colour
            if (p.getColour() != activePlayer)
            {
                return false;
            }

            // Check if move is possible
            if (p is Pawn)
            {
                return PawnRules.movePossible(board, p, move);
            }
            else if (p is Rook)
            {
                return RookRules.movePossible(board, p, move);
            }
            else if (p is Knight)
            {
                return KnightRules.movePossible(board, p, move);
            }
            else if (p is Bishop)
            {
                return BishopRules.movePossible(board, p, move);
            }
            else if (p is Queen)
            {
                return QueenRules.movePossible(board, p, move);
            }
            else if (p is King)
            {
                return KingRules.movePossible(board, p, move);
            }

            // Should not get here ever...
            return false;
        }
Exemple #14
0
 // Get all possible white moves, in the format x1,y1, x2,y2.
 public static List<Move> getWhiteMoves(Board board)
 {
     return getMoves(board, "white");
 }
Exemple #15
0
        public static List<Tuple<uint, uint>> getCover(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> cover = new List<Tuple<uint, uint>>() { };

            // Check left moves
            int x = (int)piece.getX();
            int y = (int)piece.getY();
            while (true)
            {
                x--;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                    break;
                }
            }

            // Check right moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                x++;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                    break;
                }
            }

            // Check up moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                y++;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                    break;
                }
            }

            // Check left, up moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                y--;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    cover.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                    break;
                }
            }

            return cover;
        }
Exemple #16
0
        public static List<Tuple<uint, uint>> getPossibleMoves(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> moves = new List<Tuple<uint, uint>>();

            // Check down moves
            int x = (int)piece.getX();
            int y = (int)piece.getY();
            while (true)
            {
                y--;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    if (p.getColour() != piece.getColour())
                    {
                        moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // Check up moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                y++;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    if (p.getColour() != piece.getColour())
                    {
                        moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // Check right moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                x++;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    if (p.getColour() != piece.getColour())
                    {
                        moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // Check left moves
            x = (int)piece.getX();
            y = (int)piece.getY();
            while (true)
            {
                x--;
                if (!board.withinBoard(x, y))
                {
                    break;
                }

                Piece p = board.getPieceAt((uint)x, (uint)y);
                if (p == null)
                {
                    moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                }
                else
                {
                    if (p.getColour() != piece.getColour())
                    {
                        moves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // Filter for check situations
            CommonRules.checkFilter(ref moves, board, piece);

            // Done, all moves found
            return moves;
        }
Exemple #17
0
        // Reset the game
        public void reset()
        {
            // Stop the game
            running = false;
            paused = false;

            // Create a new board and update the GUI
            board = new Board();
            gui.updateBoard(board);

            // White allways starts
            turnWhite = true;

            // Update GUI fields
            gui.putPlayerTurn(turnWhite);
            gui.putTurn(board.getTurn());
            gui.putScore(0);
        }
Exemple #18
0
 // Promts the agent to make a move
 public abstract Move getInput(Board B);
Exemple #19
0
        private static void checkFilter(ref List<Tuple<uint,uint>> moves, Board board, Piece king)
        {
            uint kx = king.getX();
            uint ky = king.getY();

            List<Piece> threats = new List<Piece>();

            // Make sure no new checks are created by King moving.
            if (board.getSquareAt(kx, ky).getEnemyCover(king.getColour()))
            {
                // King is in check, get all threats
                for (uint y = 0; y < Board.BOARD_SIZE_Y; y++)
                {
                    for (uint x = 0; x < Board.BOARD_SIZE_X; x++)
                    {
                        Piece p = board.getPieceAt(x, y);
                        if (p != null && p.getColour() != king.getColour())
                        {
                            List<Tuple<uint,uint>> cover = Rules.getCover(board, p);
                            if (cover.Contains(new Tuple<uint,uint>(kx, ky)))
                            {
                                threats.Add(p);
                            }
                        }
                    }
                }
            }

            foreach (Piece threat in threats)
            {
                if (threat is Pawn || threat is King || threat is Knight)
                {
                    continue;
                }

                // Get relative position of threat
                uint tx = threat.getX();
                uint ty = threat.getY();
                int xMod;
                int yMod;

                if (tx > kx) xMod = -1;
                else if (tx < kx) xMod = 1;
                else xMod = 0;

                if (ty > ky) yMod = -1;
                else if (ty < ky) yMod = 1;
                else yMod = 0;

                // Get square on the other side of king relative to the threat.
                int x = (int) kx + xMod;
                int y = (int) ky + yMod;
                if (board.withinBoard(x, y))
                {
                    List<Tuple<uint, uint>> tmp = new List<Tuple<uint, uint>>(moves);
                    foreach (Tuple<uint,uint> move in tmp)
                    {
                        if (move.Item1 == x && move.Item2 == y)
                        {
                            moves.Remove(move);
                        }
                    }
                }
            }
        }
Exemple #20
0
 // Check if Remi
 public static bool remi(Board board)
 {
     List<Move> movesW = getWhiteMoves(board);
     List<Move> movesB = getBlackMoves(board);
     return ((movesW.Count == 0 && !getCheck(board, "white") || (movesB.Count == 0 && !getCheck(board, "black"))));
 }
Exemple #21
0
 // Get all possible black moves, in the format x1,y1, x2,y2.
 public static List<Move> getBlackMoves(Board board)
 {
     return getMoves(board, "black");
 }
Exemple #22
0
 // Special King function used to make sure the Kings can't move next to each other
 private static List<Tuple<uint, uint>> getEnemyKingReach(Board board, Piece piece)
 {
     // Find enemy king
     List<Tuple<uint, uint>> enemyKingReach = new List<Tuple<uint, uint>>();
     bool found = false;
     for (uint j = 0; j < Board.BOARD_SIZE_Y; j++)
     {
         for (uint i = 0; i < Board.BOARD_SIZE_X; i++)
         {
             Piece p = board.getPieceAt(i, j);
             if (p != null && p is King && p.getColour() != piece.getColour())
             {
                 found = true;
                 enemyKingReach = getReach(board, p);
                 break;
             }
             if (found)
             {
                 break;
             }
         }
     }
     return enemyKingReach;
 }
Exemple #23
0
        // Filters out moves that would result in a check on the king.
        public static void checkFilter(ref List<Tuple<uint, uint>> moves, Board board, Piece piece)
        {
            string colour = piece.getColour();

            // Find king square
            Square kingSquare = null;
            for (uint j = 0; j < Board.BOARD_SIZE_Y; j++)
            {
                for (uint i = 0; i < Board.BOARD_SIZE_X; i++)
                {
                    kingSquare = board.getSquareAt(i, j);
                    if (kingSquare.getPiece() is King &&
                        kingSquare.getPiece().getColour() == colour)
                    {
                        break;
                    }
                }
                if (kingSquare.getPiece() is King &&
                        kingSquare.getPiece().getColour() == colour)
                {
                    break;
                }
            }

            // Get king position
            uint xKing = kingSquare.getX();
            uint yKing = kingSquare.getY();

            // Check if king has more than one threat
            if ((colour == "white" && kingSquare.getBlackCover() > 1) ||
                (colour == "black" && kingSquare.getWhiteCover() > 1))
            {
                moves.Clear();
            }
            else if ((colour == "white" && kingSquare.getBlackCover() == 1) ||
                     (colour == "black" && kingSquare.getWhiteCover() == 1))
            {
                Piece threat = null;

                // Get all squares with cover of king
                Piece possibleThreat;
                for (uint j = 0; j < Board.BOARD_SIZE_Y; j++)
                {
                    for (uint i = 0; i < Board.BOARD_SIZE_X; i++)
                    {
                        possibleThreat = board.getPieceAt(j, i);
                        if (possibleThreat != null && possibleThreat.getColour() != colour)
                        {
                            // Enemy piece, check if threat
                            List<Tuple<uint, uint>> cover = Rules.getCover(board, possibleThreat);
                            foreach (Tuple<uint, uint> t in cover)
                            {
                                if (t.Item1 == xKing && t.Item2 == yKing)
                                {
                                    // Threat
                                    threat = possibleThreat;
                                    break;
                                }
                            }
                        }
                        if (threat != null)
                            break;
                    }
                    if (threat != null)
                        break;
                }

                // Remove all moves that do not resolve the check
                List<Tuple<uint, uint>> tmpMoves = new List<Tuple<uint, uint>>(moves);
                Piece p = threat;
                if (p is Knight || p is Pawn)
                {
                    // Must take threat
                    foreach (Tuple<uint, uint> move in tmpMoves)
                    {
                        if (move.Item1 != threat.getX() ||
                            move.Item2 != threat.getY())
                        {
                            // Will not take, remove
                            moves.Remove(move);
                        }
                    }
                }

                tmpMoves = new List<Tuple<uint, uint>>(moves);
                if (p is Rook || p is Queen)
                {
                    // Must take or block threat
                    foreach (Tuple<uint, uint> move in tmpMoves)
                    {
                        if (xKing == p.getX())
                        {
                            // Same x
                            if (!(move.Item1 == xKing &&                                   // Move will move to same x
                                  ((yKing > move.Item2 && move.Item2 >= p.getY()) ||       // Move will block or take when King to the right of Rook
                                   (yKing < move.Item2 && move.Item2 <= p.getY()))))       // Move will block or take when King to the left of Rook
                            {
                                // Will not block or take
                                moves.Remove(move);
                            }
                        }
                        else if (yKing == p.getY())
                        {
                            // Same y
                            if (!(move.Item2 == yKing &&                                   // Move will move to same y
                                  ((xKing > move.Item1 && move.Item1 >= p.getX()) ||       // Move will block or take when King above Rook
                                   (xKing < move.Item1 && move.Item1 <= p.getX()))))       // Move will block or take when King below Rook
                            {
                                // Will not block or take
                                moves.Remove(move);
                            }
                        }
                    }
                }

                tmpMoves = new List<Tuple<uint, uint>>(moves);
                if ((p is Bishop || p is Queen) && !(xKing == p.getX() || yKing == p.getY()))
                {
                    // Must take or block threat
                    int xMod = (xKing > p.getX() ? -1 : 1);
                    int yMod = (yKing > p.getY() ? -1 : 1);

                    // Step from King to Threat
                    int steps = (int)Math.Abs((int)xKing - (uint)p.getX());
                    foreach (Tuple<uint, uint> move in tmpMoves)
                    {
                        // Search for any blocking or taking moves
                        int x = (int)xKing;
                        int y = (int)yKing;
                        bool foundGoodMove = false;
                        for (int i = 0; i < steps; i++)
                        {
                            x += xMod;
                            y += yMod;

                            if (move.Item1 == x && move.Item2 == y)
                            {
                                foundGoodMove = true;
                                break;
                            }
                        }

                        // Remove if no good move
                        if (!foundGoodMove)
                        {
                            moves.Remove(move);
                        }
                    }
                }
            }

            // ----------------------------------------------------------------------------------------

            // Make sure that no new check is created by a move
            if (board.getSquareAt(piece.getX(), piece.getY()).getEnemyCover(colour))
            {
                // Empty list of squares with cover of this square
                List<Piece> threats = new List<Piece>();

                // Get all squares with cover of self
                Piece p;
                for (uint j = 0; j < Board.BOARD_SIZE_X; j++)
                {
                    for (uint i = 0; i < Board.BOARD_SIZE_Y; i++)
                    {
                        p = board.getPieceAt(j, i);
                        if (p != null && p.getColour() != colour)
                        {
                            // Remove impossible cases
                            if (p is King || p is Knight || p is Pawn)
                            {
                                continue;
                            }

                            // Enemy piece, check if threat
                            List<Tuple<uint, uint>> cover = Rules.getCover(board, p);
                            foreach (Tuple<uint, uint> t in cover)
                            {
                                if (t.Item1 == piece.getX() && t.Item2 == piece.getY())
                                {
                                    // Threat
                                    threats.Add(p);
                                }
                            }
                        }
                    }
                }

                // Check each threat for possible new check
                Piece realThreat = null;
                int xMod = 0, yMod = 0;
                foreach (Piece threat in threats)
                {
                    int threatX = (int)threat.getX();
                    int threatY = (int)threat.getY();

                    int x = (int)piece.getX();
                    int y = (int)piece.getY();

                    // Find relative position of the threat
                    if (threatX > x) xMod = -1; // Threat to the right
                    else if (threatX == x) xMod = 0;  // Threat at same x
                    else xMod = 1;  // Treat to the left

                    if (threatY > y) yMod = -1; // Threat above
                    else if (threatY == y) yMod = 0;  // Threat at same y
                    else yMod = 1;  // Treat below

                    // Step from self away from threat.
                    // If king is the first piece found, this is the real threatening piece.
                    Piece firstPiece;
                    while (true)
                    {
                        x += xMod;
                        y += yMod;

                        if (!board.withinBoard(x, y))
                        {
                            break;
                        }

                        firstPiece = board.getPieceAt((uint)x, (uint)y);
                        if (firstPiece != null)
                        {
                            if (firstPiece is King && firstPiece.getColour() == piece.getColour())
                            {
                                // This is the friendly king.
                                realThreat = threat;
                            }
                            break;
                        }
                    }

                    if (realThreat != null)
                    {
                        // Real threat found.
                        break;
                    }
                }

                // If a real new threat is found; remove any moves that do not end on the
                // line between this and king.
                if (realThreat != null)
                {
                    int x = (int)realThreat.getX();
                    int y = (int)realThreat.getY();
                    List<Tuple<uint, uint>> possibleMoves = new List<Tuple<uint, uint>>();

                    // First get all the squares that are on a line between the threat piece and the king,
                    // including the threat, excluding the king.
                    while (true)
                    {
                        possibleMoves.Add(new Tuple<uint, uint>((uint)x, (uint)y));
                        x += xMod;
                        y += yMod;
                        if (x == xKing && y == yKing)
                        {
                            // End of possible moves.
                            break;
                        }
                    }

                    // Remove any moves not in possible list.
                    List<Tuple<uint, uint>> tmp = new List<Tuple<uint, uint>>(moves);
                    foreach (Tuple<uint, uint> move in tmp)
                    {
                        if (!possibleMoves.Contains(move))
                        {
                            moves.Remove(move);
                        }
                    }
                }
            }
        }
Exemple #24
0
        public static List<Tuple<uint, uint>> getPossibleMoves(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> moves = new List<Tuple<uint, uint>>();

            int x = (int)piece.getX();
            int y = (int)piece.getY();

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (i == 0 && j == 0)
                    {
                        // Standing still is not a move
                        continue;
                    }
                    int nx = x + i;
                    int ny = y + j;
                    if (board.withinBoard(nx, ny))
                    {
                        Square s = board.getSquareAt((uint)nx, (uint)ny);
                        if ((piece.getColour() == "white" && s.getBlackCover() == 0) ||
                            (piece.getColour() == "black" && s.getWhiteCover() == 0))
                        {
                            Piece p = s.getPiece();
                            if (p == null)
                            {
                                moves.Add(new Tuple<uint, uint>((uint)nx, (uint)ny));
                            }
                            else
                            {
                                if (p.getColour() != piece.getColour())
                                {
                                    moves.Add(new Tuple<uint, uint>((uint)nx, (uint)ny));
                                }
                            }
                        }
                    }
                }
            }

            // Castling
            Square tmp = board.getSquareAt(piece.getX(), piece.getY());
            if ((!piece.movedFromInit()) && (!tmp.getEnemyCover(piece.getColour())))
            {
                for (int i = 1; i < 5; i++)
                {
                    Square s = board.getSquareAt((uint)(x - i), (uint)y);
                    if ((i < 4) && ((s.getPiece() != null) || s.getEnemyCover(piece.getColour())))
                        break;
                    else if ((s.getPiece() != null) && (i == 4))
                        if ((s.getPiece() is Rook) && (!s.getPiece().movedFromInit()))
                            moves.Add(new Tuple<uint, uint>((uint)(x - 2), (uint)y));

                }
                for (int i = 1; i < 4; i++)
                {
                    Square s = board.getSquareAt((uint)(x + i), (uint)y);
                    if ((i < 3) && ((s.getPiece() != null) || s.getEnemyCover(piece.getColour())))
                        break;
                    else if ((s.getPiece() != null) && (i == 3))
                        if ((s.getPiece() is Rook) && (!s.getPiece().movedFromInit()))
                            moves.Add(new Tuple<uint, uint>((uint)(x + 2), (uint)y));

                }
            }

            // Remove squares with enemy king reach
            foreach (Tuple<uint, uint> reach in getEnemyKingReach(board, piece))
            {
                moves.Remove(reach);
            }

            // Remove any moves that create new checks
            checkFilter(ref moves, board, piece);

            // Done, all moves found
            return moves;
        }
Exemple #25
0
 public override Move getInput(Board B)
 {
     return read();
 }
Exemple #26
0
 // Check if a player has lost.
 public static bool playerLost(Board board, string col)
 {
     List<Move> moves = getMoves(board, col);
     return (moves.Count == 0 && getCheck(board, col));
 }
Exemple #27
0
        // Returns a list of all possible moves
        public static List<Tuple<uint, uint>> getPossibleMoves(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> tmpList = new List<Tuple<uint, uint>>();
            short yMod;
            short passantRow;
            if (piece.getColour() == "white")
            {
                yMod = 1;
                passantRow = 4;
            }
            else
            {
                yMod = -1;
                passantRow = 3;
            }

            // Take left
            if (board.withinBoard((int) piece.getX() - 1, (int) piece.getY() + yMod))
            {
                Piece P = board.getPieceAt((uint)(piece.getX() - 1), (uint)(piece.getY() + yMod));
                if (P != null)
                {
                    if (!piece.isSameColour(P))
                    {
                        tmpList.Add(new Tuple<uint, uint>((uint)(piece.getX() - 1), (uint)(piece.getY() + yMod)));
                    }
                }
            }

            //Take right
            if (board.withinBoard((int)piece.getX() + 1, (int)piece.getY() + yMod))
            {
                Piece P = board.getPieceAt((uint)(piece.getX() + 1), (uint)(piece.getY() + yMod));
                if (P != null)
                {
                    if (!piece.isSameColour(P))
                    {
                        tmpList.Add(new Tuple<uint, uint>((uint)(piece.getX() + 1), (uint)(piece.getY() + yMod)));
                    }
                }
            }

            //Move 1
            if (board.withinBoard((int)piece.getX(), (int)piece.getY() + yMod))
            {
                Piece P = board.getPieceAt(piece.getX(), (uint)(piece.getY() + yMod));
                if (P == null)
                {
                    tmpList.Add(new Tuple<uint, uint>(piece.getX(), (uint)(piece.getY() + yMod)));
                }
            }

            //Move 2 (Only first move)
            if (!piece.movedFromInit())
            {
                if (board.withinBoard((int)piece.getX(), (int)piece.getY() + 2 * yMod))
                {
                    Piece P = board.getPieceAt(piece.getX(), (uint)(piece.getY() + 2 * yMod));
                    Piece P2 = board.getPieceAt(piece.getX(), (uint)(piece.getY() + yMod));
                    if (P == null && P2 == null)
                    {
                        tmpList.Add(new Tuple<uint, uint>(piece.getX(), (uint)(piece.getY() + 2 * yMod)));
                        //((Pawn)piece).setDoubleStepTurn(board.getTurn());
                    }
                }
            }

            //En Passant
            if (piece.getY() == passantRow)
            {
                if ((piece.getX() + 1) < Board.BOARD_SIZE_X)
                {
                    Piece P1 = board.getPieceAt(piece.getX() + 1, piece.getY());

                    if (P1 != null)
                    {
                        if (P1 is Pawn)
                            if (((Pawn)P1).getDoubleStepTurn() == board.getTurn() - 1)
                                tmpList.Add(new Tuple<uint, uint>(piece.getX() + 1, (uint)(piece.getY() + yMod)));
                    }
                }

                if ((int)piece.getX() - 1 >= 0)
                {
                    Piece P2 = board.getPieceAt(piece.getX() - 1, piece.getY());
                    if (P2 != null)
                    {
                        if (P2 is Pawn)
                            if (((Pawn)P2).getDoubleStepTurn() == board.getTurn() - 1)
                                tmpList.Add(new Tuple<uint, uint>(piece.getX() - 1, (uint)(piece.getY() + yMod)));
                    }
                }

            }

            // Filter for check situations
            CommonRules.checkFilter(ref tmpList, board, piece);

            return tmpList;
        }
Exemple #28
0
        // Get all moves of a specific colour.
        private static List<Move> getMoves(Board board, string col)
        {
            List<Move> moves = new List<Move>();

            for (uint y = 0; y < Board.BOARD_SIZE_Y; y++)
            {
                for (uint x = 0; x < Board.BOARD_SIZE_X; x++)
                {
                    Piece p = board.getPieceAt(x, y);
                    if (p != null && p.getColour() == col)
                    {
                        List<Tuple<uint, uint>> newMoves = getPossibleMoves(board, p);

                        foreach (Tuple<uint, uint> t in newMoves)
                        {
                            moves.Add(new Move(x, y, t.Item1, t.Item2));
                        }
                    }
                }
            }

            return moves;
        }
Exemple #29
0
 // Deep copy of board
 public void Copy(Board oldBoard)
 {
     for (int i = 0; i < BOARD_SIZE_X; i++)
     {
         for (int j = 0; j < BOARD_SIZE_Y; j++)
         {
             squares[i, j].Copy(oldBoard.squares[i, j]);
         }
     }
     turn = oldBoard.turn;
 }
Exemple #30
0
        // --- Graphics interface ---
        // Functions for printing the text and image versions of the board.
        // Update the board and render the GUI. Also reset the selected move.
        public void updateBoard(src.Board board)
        {
            this.board = board;
            drawControl.setBoard(board);

            selectedMove = new src.Move();

            renderGUI();
        }
Exemple #31
0
        // Special King function used to make sure the Kings can't move next to each other
        public static List<Tuple<uint, uint>> getReach(Board board, Piece piece)
        {
            List<Tuple<uint, uint>> reach = new List<Tuple<uint, uint>>();

            int x = (int)piece.getX();
            int y = (int)piece.getY();

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (i == 0 && j == 0)
                    {
                        // Do not have reach to self
                        continue;
                    }
                    int nx = x + i;
                    int ny = y + j;
                    if (board.withinBoard(nx, ny))
                    {
                        reach.Add(new Tuple<uint, uint>((uint)nx, (uint)ny));
                    }
                }
            }

            return reach;
        }