Пример #1
0
        public override bool canMove(int x1, int y1, int x2, int y2, int Player)
        {
            if (canMoveAux(x1, y1, x2, y2, Player))
            {
                Piece eaten_piece  = BOARD.getPiece(x2, y2);
                Board BOARD_BACKUP = BOARD.backup();
                if (canMoveAux(x1, y1, x2, y2, Player))
                {
                    if ((y1 != y2) && eaten_piece.getClass2() == NullPiece.getClass() && ((ChessPiece)BOARD.getPiece(x1, y1)).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
                    {
                        eaten_piece = BOARD.getPiece(x1, y2);
                        BOARD.removePiece(x1, y2);
                    }
                    BOARD.movePiece(x1, y1, x2, y2);

                    if (inCheck(Player))
                    {
                        BOARD = BOARD_BACKUP;

                        return(false);
                    }
                    else
                    {
                        BOARD = BOARD_BACKUP;
                        return(true);
                    }
                }
                BOARD = BOARD_BACKUP;
            }
            return(false);
        }
Пример #2
0
        public bool canMoveAux(int x1, int y1, int x2, int y2, int Player)
        {
            if (GAME_ENDED || WAITING_PLAYERS)
            {
                return(false);
            }
            if (Player != ACTUAL_PLAYER)
            {
                return(false);
            }

            if (!BOARD.validCell(x1, y1) || !BOARD.validCell(x2, y2))
            {
                return(false);
            }

            if (BOARD.getPiece(x1, y1).getClass2() == NullPiece.getClass())
            {
                return(false);
            }

            bool null_destination = true;

            if (BOARD.getPiece(x2, y2).getClass2() != NullPiece.getClass())
            {
                null_destination = false;
                if (((ChessPiece)BOARD.getPiece(x2, y2)).PLAYER == Player)
                {
                    return(false);
                }
            }



            ChessPiece P1 = (ChessPiece)BOARD.getPiece(x1, y1);

            if (P1.getPlayer() != Player)
            {
                Console.WriteLine("The piece belong to the other player: " + P1.getPlayer() + ", you are " + Player);
                return(false);
            }


            if (P1.get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
            {
                if (Player == PLAYER1)
                {
                    if ((x2 - x1 == -1) && (y2 == y1) && (null_destination))
                    {
                        return(true);
                    }
                    else if ((x2 - x1 == -2) && (y2 == y1) && (x1 == 6) && (null_destination))
                    {
                        return(true);
                    }
                    else if ((x2 - x1 == -1) && Math.Abs(y2 - y1) == 1)
                    {
                        if (!(null_destination))
                        {
                            return(true);
                        }
                        else if (BOARD.getPiece(x1, y2).getClass2() != NullPiece.getClass())
                        {
                            if (((ChessPiece)BOARD.getPiece(x1, y2)).PLAYER != Player && ((ChessPiece)BOARD.getPiece(x1, y2)).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
                            {
                                if ((LAST_ACTION.getClass2() == MoveAction.getClass()))
                                {
                                    if (((MoveAction)LAST_ACTION).TO_X == x1 && ((MoveAction)LAST_ACTION).TO_Y == y2 && ((ChessPiece)((MoveAction)LAST_ACTION).ACTOR).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase) && ((MoveAction)LAST_ACTION).FROM_X == x1 - 2)
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (Player == PLAYER2)
                {
                    if ((x2 - x1 == 1) && (y2 == y1) && (null_destination))
                    {
                        return(true);
                    }
                    else if ((x2 - x1 == 2) && (y2 == y1) && (x1 == 1) && (null_destination))
                    {
                        return(true);
                    }
                    else if ((x2 - x1 == 1) && Math.Abs(y2 - y1) == 1)
                    {
                        if (!(null_destination))
                        {
                            return(true);
                        }
                        else if (BOARD.getPiece(x1, y2).getClass2() != NullPiece.getClass())
                        {
                            if (((ChessPiece)BOARD.getPiece(x1, y2)).PLAYER != Player && ((ChessPiece)BOARD.getPiece(x1, y2)).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
                            {
                                if ((LAST_ACTION.getClass2() == MoveAction.getClass()))
                                {
                                    if (((MoveAction)LAST_ACTION).TO_X == x1 && ((MoveAction)LAST_ACTION).TO_Y == y2 && ((ChessPiece)((MoveAction)LAST_ACTION).ACTOR).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase) && ((MoveAction)LAST_ACTION).FROM_X == x1 + 2)
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (P1.get_type().Equals(ChessPiece.KNIGHT, StringComparison.CurrentCultureIgnoreCase))
            {
                if (((Math.Abs(x2 - x1) == 1) && (Math.Abs(y2 - y1) == 2)) || ((Math.Abs(x2 - x1) == 2) && (Math.Abs(y2 - y1) == 1)))
                {
                    return(true);
                }
            }
            else if (P1.get_type().Equals(ChessPiece.BISHOP, StringComparison.CurrentCultureIgnoreCase))
            {
                if (Math.Abs(x2 - x1) == Math.Abs(y2 - y1) && y2 != y1)
                {
                    if (!thereIsSomeoneInTheWay(x1, y1, x2, y2))
                    {
                        return(true);
                    }
                }
            }
            else if (P1.get_type().Equals(ChessPiece.ROOK, StringComparison.CurrentCultureIgnoreCase))
            {
                if ((x2 - x1 == 0 && Math.Abs(y2 - y1) > 0) || (y2 - y1 == 0 && Math.Abs(x2 - x1) > 0))
                {
                    if (!thereIsSomeoneInTheWay(x1, y1, x2, y2))
                    {
                        return(true);
                    }
                }
            }
            else if (P1.get_type().Equals(ChessPiece.QUEEN, StringComparison.CurrentCultureIgnoreCase))
            {
                if ((x2 - x1 == 0 && Math.Abs(y2 - y1) > 0) || (y2 - y1 == 0 && Math.Abs(x2 - x1) > 0))
                {
                    if (!thereIsSomeoneInTheWay(x1, y1, x2, y2))
                    {
                        return(true);
                    }
                }
                else if (Math.Abs(x2 - x1) == Math.Abs(y2 - y1) && y2 != y1)
                {
                    if (!thereIsSomeoneInTheWay(x1, y1, x2, y2))
                    {
                        return(true);
                    }
                }
            }
            else if (P1.get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase))
            {
                if ((Math.Abs(y2 - y1) <= 1) && (Math.Abs(x2 - x1) <= 1) && (Math.Abs(y2 - y1) + Math.Abs(x2 - x1) >= 1))
                {
                    return(true);
                }
                else
                {
                    if (Player == PLAYER1)
                    {
                        if (KING_PLAYER1_MOVED == false)
                        {
                            if (x1 == 7 && y1 == 4 && x2 == 7 && y2 == 6)
                            {
                                if (RIGHT_ROOK_PLAYER1_MOVED == false && BOARD.getPiece(7, 5).getClass2() == NullPiece.getClass() && BOARD.getPiece(7, 6).getClass2() == NullPiece.getClass())
                                {
                                    if (BOARD.getPiece(x1, 7).getClass2().Equals(ChessPiece.getClass()))
                                    {
                                        if (((ChessPiece)BOARD.getPiece(x1, 7)).PLAYER == Player && ((ChessPiece)BOARD.getPiece(x1, 7)).get_type().Equals(ChessPiece.ROOK, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            Board BACK_BOARD = BOARD.backup();
                                            if (canMove(x1, y1, x2, 5, Player))
                                            {
                                                BOARD.movePiece(x1, y1, x2, y2);
                                                BOARD.movePiece(x1, y1, x2, 5);
                                                bool CanCastling = !(inCheck(Player));
                                                BOARD = BACK_BOARD;
                                                Console.WriteLine("Can roque!");
                                                return(CanCastling);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (x1 == 7 && y1 == 4 && x2 == 7 && y2 == 2)
                            {
                                if (LEFT_ROOK_PLAYER1_MOVED == false && BOARD.getPiece(7, 1).getClass2() == NullPiece.getClass() && BOARD.getPiece(7, 2).getClass2() == NullPiece.getClass() && BOARD.getPiece(7, 3).getClass2() == NullPiece.getClass())
                                {
                                    if (BOARD.getPiece(x1, 0).getClass2().Equals(ChessPiece.getClass()))
                                    {
                                        if (((ChessPiece)BOARD.getPiece(x1, 0)).PLAYER == Player && ((ChessPiece)BOARD.getPiece(x1, 7)).get_type().Equals(ChessPiece.ROOK, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            Board BACK_BOARD = BOARD.backup();
                                            if (canMove(x1, y1, x2, 3, Player))
                                            {
                                                BOARD.movePiece(x1, y1, x2, y2);
                                                BOARD.movePiece(x1, y1, x2, 3);
                                                bool CanCastling = !(inCheck(Player));
                                                BOARD = BACK_BOARD;
                                                Console.WriteLine("Can roque!");
                                                return(CanCastling);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }


                    if (Player == PLAYER2)
                    {
                        if (KING_PLAYER2_MOVED == false)
                        {
                            if (x1 == 0 && y1 == 4 && x2 == 0 && y2 == 6)
                            {
                                if (RIGHT_ROOK_PLAYER2_MOVED == false && BOARD.getPiece(0, 5).getClass2() == NullPiece.getClass() && BOARD.getPiece(0, 6).getClass2() == NullPiece.getClass())
                                {
                                    if (BOARD.getPiece(x1, 0).getClass2().Equals(ChessPiece.getClass()))
                                    {
                                        if (((ChessPiece)BOARD.getPiece(x1, 0)).PLAYER == Player && ((ChessPiece)BOARD.getPiece(x1, 0)).get_type().Equals(ChessPiece.ROOK, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            Board BACK_BOARD = BOARD.backup();
                                            if (canMove(x1, y1, x2, 5, Player))
                                            {
                                                BOARD.movePiece(x1, y1, x2, y2);
                                                BOARD.movePiece(x1, y1, x2, 5);
                                                bool CanCastling = !(inCheck(Player));
                                                BOARD = BACK_BOARD;
                                                Console.WriteLine("Can roque!");
                                                return(CanCastling);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (x1 == 0 && y1 == 4 && x2 == 0 && y2 == 2)
                            {
                                if (LEFT_ROOK_PLAYER1_MOVED == false && BOARD.getPiece(0, 1).getClass2() == NullPiece.getClass() && BOARD.getPiece(0, 2).getClass2() == NullPiece.getClass() && BOARD.getPiece(0, 3).getClass2() == NullPiece.getClass())
                                {
                                    if (BOARD.getPiece(x1, 0).getClass2().Equals(ChessPiece.getClass()))
                                    {
                                        if (((ChessPiece)BOARD.getPiece(x1, 0)).PLAYER == Player && ((ChessPiece)BOARD.getPiece(x1, 0)).get_type().Equals(ChessPiece.ROOK, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            Board BACK_BOARD = BOARD.backup();
                                            if (canMove(x1, y1, x2, 3, Player))
                                            {
                                                BOARD.movePiece(x1, y1, x2, y2);
                                                BOARD.movePiece(x1, y1, x2, 3);
                                                bool CanCastling = !(inCheck(Player));
                                                BOARD = BACK_BOARD;
                                                Console.WriteLine("Can roque!");
                                                return(CanCastling);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }