コード例 #1
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);
        }
コード例 #2
0
ファイル: PawnRules.cs プロジェクト: smorkeks/chessForms
        // 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);
        }
コード例 #3
0
ファイル: RookRules.cs プロジェクト: smorkeks/chessForms
        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);
        }