public bool burn(int x, int y)
        {
            Console.WriteLine("Burning: " + x + "," + y);
            Console.WriteLine(BOARD.getPiece(x, y).getClass2());
            if (BOARD.getPiece(x, y).getClass2() == DeflexionPiece.getClass())
            {
                String deleted_type   = ((DeflexionPiece)BOARD.getPiece(x, y)).get_type();
                int    deleted_player = ((DeflexionPiece)BOARD.getPiece(x, y)).getPlayer();
                if (BOARD.removePiece(x, y))
                {
                    if (deleted_type.Equals(DeflexionPiece.PHAROAH, StringComparison.CurrentCultureIgnoreCase))
                    {
                        GAME_ENDED = true;
                        if (deleted_player == PLAYER1)
                        {
                            WINNER = PLAYER2;
                        }
                        else if (deleted_player == PLAYER2)
                        {
                            WINNER = PLAYER1;
                        }
                        else
                        {
                            Console.WriteLine("Invalid player - burn end game");
                        }

                        Console.WriteLine("End game. Winner: " + WINNER);
                    }
                    CONT_ID++;
                    return(true);
                }
            }
            return(false);
        }
        private bool inCheckMate(int Player)
        {
            Position p = findKing(Player);

            if (!(inCheck(Player)))
            {
                return(false);
            }
            int KingX = p.X;
            int KingY = p.Y;

            if (canMoveAnywhere(KingX, KingY, ChessPiece.KING))
            {
                return(false);
            }

            for (int cx = 0; cx <= 8; cx++)
            {
                for (int cy = 0; cy <= 8; cy++)
                {
                    if (BOARD.getPiece(cx, cy).getClass2().Equals(ChessPiece.getClass()))
                    {
                        ChessPiece P1 = (ChessPiece)BOARD.getPiece(cx, cy);
                        if ((P1.PLAYER == Player && !P1.get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            if (canMoveAnywhere(cx, cy, P1.get_type()))
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            return(true);
        }
        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);
        }
        public bool canAdd(int x, int y, Piece P)
        {
            if ((GAME_ENDED) || (WAITING_PLAYERS))
            {
                return(false);
            }
            if (BOARD.getPiece(x, y).getClass2() != NullPiece.getClass())
            {
                return(false);
            }
            int cont_O = BOARD.contPieces(PLAYER2);
            int cont_X = BOARD.contPieces(PLAYER1);

            if (P.getPlayer() == PLAYER2)
            {
                if (cont_O == cont_X - 1)
                {
                    ACTUAL_PLAYER = PLAYER1;
                    return(true);
                }
            }
            else if ((P.getPlayer() == PLAYER1))
            {
                if (cont_O == cont_X)
                {
                    ACTUAL_PLAYER = PLAYER2;
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
 public bool canEat(int x, int y)
 {
     if (BOARD.getPiece(x, y).getClass2() == CheckersPiece.getClass())
     {
         return(true);
     }
     return(false);
 }
 public bool canEat(int x, int y, int x2, int y2, int Player)
 {
     if (BOARD.validCell(x2, y2))
     {
         if (BOARD.getPiece(x2, y2).getClass2() == ChessPiece.getClass())
         {
             return(canMove(x, y, x2, y2, Player));
         }
     }
     return(false);
 }
 public bool canRotate(int x, int y, int Player)
 {
     if (BOARD.validCell(x, y))
     {
         if (BOARD.getPiece(x, y).getClass2() == DeflexionPiece.getClass())
         {
             if (((DeflexionPiece)BOARD.getPiece(x, y)).get_type().Equals(DeflexionPiece.DJED, StringComparison.CurrentCultureIgnoreCase) || ((DeflexionPiece)BOARD.getPiece(x, y)).get_type().Equals(DeflexionPiece.PYRAMID, StringComparison.CurrentCultureIgnoreCase))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Beispiel #8
0
        public override bool move(int x1, int y1, int x2, int y2, int Player)
        {
            if (canMove(x1, y1, x2, y2, Player))
            {
                BOARD.movePiece(x1, y1, x2, y2);
                if ((Player == PLAYER1) && (x2 == 7))
                {
                    ((CheckersPiece)BOARD.getPiece(x2, y2)).TYPE = CheckersPiece.CHECKER;
                }
                else if ((Player == PLAYER2) && (x2 == 0))
                {
                    ((CheckersPiece)BOARD.getPiece(x2, y2)).TYPE = CheckersPiece.CHECKER;
                }

                if (Math.Abs(x2 - x1) == 2 && Math.Abs(y2 - y1) == 2)
                {
                    int x_adversary = x1 + (x2 - x1) / 2;
                    int y_adversary = y1 + (y2 - y1) / 2;
                    eat(x_adversary, y_adversary);
                    if (isPossibleToKeepEating(x2, y2, Player))
                    {
                        EAT_MOVE  = true;
                        EAT_PIECE = BOARD.getPiece(x2, y2);
                        CONT_ID++;
                        if (gameEnded())
                        {
                            Console.WriteLine("End game. Winner: " + WINNER);
                        }
                        return(true);
                    }
                }
                EAT_MOVE = false;
                if (Player == PLAYER1)
                {
                    ACTUAL_PLAYER = PLAYER2;
                }
                else
                {
                    ACTUAL_PLAYER = PLAYER1;
                }
                CONT_ID++;
                if (gameEnded())
                {
                    Console.WriteLine("End game. Winner: " + WINNER);
                }
                return(true);
            }
            return(false);
        }
 public bool eat(int x, int y)
 {
     if (BOARD.getPiece(x, y).getClass2() == ChessPiece.getClass())
     {
         if (BOARD.removePiece(x, y))
         {
             if (gameEnded())
             {
                 Console.WriteLine("End game. Winner: " + WINNER);
             }
             return(true);
         }
     }
     return(false);
 }
Beispiel #10
0
        public bool won(int Player)
        {
            int c1, c2, c3;

            if (Player == -1)
            {
                return(false);
            }
            for (int cont = 0; cont < 3; cont++)
            {
                c1 = BOARD.getPiece(0, cont).getPlayer();
                c2 = BOARD.getPiece(1, cont).getPlayer();
                c3 = BOARD.getPiece(2, cont).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player)
                {
                    return(true);
                }


                c1 = BOARD.getPiece(cont, 0).getPlayer();
                c2 = BOARD.getPiece(cont, 1).getPlayer();
                c3 = BOARD.getPiece(cont, 2).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player)
                {
                    return(true);
                }
            }

            c1 = BOARD.getPiece(0, 0).getPlayer();
            c2 = BOARD.getPiece(1, 1).getPlayer();
            c3 = BOARD.getPiece(2, 2).getPlayer();
            if (c1 == Player && c2 == Player && c3 == Player)
            {
                return(true);
            }


            c1 = BOARD.getPiece(0, 2).getPlayer();
            c2 = BOARD.getPiece(1, 1).getPlayer();
            c3 = BOARD.getPiece(2, 0).getPlayer();
            if (c1 == Player && c2 == Player && c3 == Player)
            {
                return(true);
            }
            return(false);
        }
 public Position findKing(int Player)
 {
     for (int x = 0; x <= 8; x++)
     {
         for (int y = 0; y <= 8; y++)
         {
             if (BOARD.getPiece(x, y).getClass2() != NullPiece.getClass())
             {
                 if ((((ChessPiece)BOARD.getPiece(x, y)).get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase)) && ((ChessPiece)BOARD.getPiece(x, y)).PLAYER == Player)
                 {
                     return(new Position(x, y));
                 }
             }
         }
     }
     return(new Position(-1, -1));
 }
Beispiel #12
0
        private bool cannotMove(int Player)
        {
            if (EAT_MOVE)
            {
                return(false);
            }
            if (GAME_ENDED || WAITING_PLAYERS)
            {
                return(false);
            }
            if (Player != ACTUAL_PLAYER)
            {
                return(false);
            }

            for (int x1 = 0; x1 < 8; x1++)
            {
                for (int y1 = 0; y1 < 8; y1++)
                {
                    if (BOARD.getPiece(x1, y1).getClass2() != NullPiece.getClass())
                    {
                        CheckersPiece P1 = (CheckersPiece)BOARD.getPiece(x1, y1);
                        if (P1.getPlayer() == Player)
                        {
                            for (int dx = 1; dx <= 2; dx++)
                            {
                                for (int posNegX = -1; posNegX < 2; posNegX = posNegX + 2)
                                {
                                    for (int posNegY = -1; posNegY < 2; posNegY = posNegY + 2)
                                    {
                                        int destX = x1 + dx * posNegX;
                                        int destY = x1 + dx * posNegY;
                                        if (BOARD.validCell(destX, destY) && canMove(x1, y1, destX, destY, Player))
                                        {
                                            return(false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
        public bool thereIsSomeoneInTheWay(int x1, int y1, int x2, int y2)
        {
            int add_x = 1;
            int add_y = 1;

            if (x2 - x1 < 0)
            {
                add_x = -1;
            }
            else if (x2 == x1)
            {
                add_x = 0;
            }
            if (y2 - y1 < 0)
            {
                add_y = -1;
            }
            else if (y2 == y1)
            {
                add_y = 0;
            }
            x1 += add_x;
            y1 += add_y;
            int cont = 0;

            while (x1 != x2 || y1 != y2 && cont < 10)
            {
                cont++;

                if (BOARD.validCell(x1, y1))
                {
                    if ((BOARD.getPiece(x1, y1).getClass2() != NullPiece.getClass()))
                    {
                        return(true);
                    }
                }
                x1 += add_x;
                y1 += add_y;
            }
            return(false);
        }
        public override bool canMove(int x1, int y1, int x2, int y2, int Player)
        {
            Console.WriteLine("Deflexion can move: " + x1 + " " + x2 + " " + y1 + " " + y2 + " " + Player + " " + PLAYER1 + " " + PLAYER2 + " " + GAME_ENDED);
            if (GAME_ENDED)
            {
                return(false);
            }

            if (CONFIRM_MOVE)
            {
                return(false);
            }

            if ((Math.Abs(x2 - x1) > 1 || Math.Abs(y2 - y1) > 1))
            {
                return(false);
            }

            if ((Math.Abs(x2 - x1) == 0 && Math.Abs(y2 - y1) == 0))
            {
                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);
            }

            if (BOARD.getPiece(x2, y2).getClass2() != NullPiece.getClass())
            {
                Console.WriteLine("Moving deflexion2.1");
                if (!((DeflexionPiece)BOARD.getPiece(x1, y1)).get_type().Equals(DeflexionPiece.DJED, StringComparison.CurrentCultureIgnoreCase))
                {
                    Console.WriteLine("Moving deflexion2.2");
                    return(false);
                }
                else
                {
                    Console.WriteLine("Moving deflexion2.3");
                    String temp_type = ((DeflexionPiece)BOARD.getPiece(x2, y2)).get_type();
                    if ((temp_type.Equals(DeflexionPiece.DJED)) || (temp_type.Equals(DeflexionPiece.PHAROAH, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        Console.WriteLine("Moving deflexion4");
                        return(false);
                    }
                }
            }

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

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

            Console.WriteLine(Player + " " + P1.getPlayer());
            if (P1.getPlayer() != Player)
            {
                return(false);
            }


            if ((y2 == 0) && (P1.getPlayer() == PLAYER2))
            {
                return(false);
            }

            if ((y2 == 9) && (P1.getPlayer() == PLAYER1))
            {
                return(false);
            }

            return(true);
        }
        public bool won(int Player)
        {
            int c1, c2, c3, c4;

            for (int cont = 0; cont < 16; cont++)
            {
                c1 = BOARD.getPiece(0, cont).getPlayer();
                c2 = BOARD.getPiece(1, cont).getPlayer();
                c3 = BOARD.getPiece(2, cont).getPlayer();
                c4 = BOARD.getPiece(3, cont).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                {
                    return(true);
                }
            }
            for (int cont = 0; cont < 4; cont++)
            {
                for (int cont2 = 0; cont2 < 4; cont2++)
                {
                    c1 = BOARD.getPiece(cont, 0 + cont2 * 4).getPlayer();
                    c2 = BOARD.getPiece(cont, 1 + cont2 * 4).getPlayer();
                    c3 = BOARD.getPiece(cont, 2 + cont2 * 4).getPlayer();
                    c4 = BOARD.getPiece(cont, 3 + cont2 * 4).getPlayer();
                    if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                    {
                        return(true);
                    }
                }
            }


            for (int cont = 0; cont < 4; cont++)
            {
                for (int cont2 = 0; cont2 < 4; cont2++)
                {
                    c1 = BOARD.getPiece(cont, cont2).getPlayer();
                    c2 = BOARD.getPiece(cont, cont2 + 1 * 4).getPlayer();
                    c3 = BOARD.getPiece(cont, cont2 + 2 * 4).getPlayer();
                    c4 = BOARD.getPiece(cont, cont2 + 3 * 4).getPlayer();
                    if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                    {
                        return(true);
                    }
                }
            }



            for (int cont = 0; cont < 4; cont++)
            {
                c1 = BOARD.getPiece(cont, 0).getPlayer();
                c2 = BOARD.getPiece(cont, 5).getPlayer();
                c3 = BOARD.getPiece(cont, 10).getPlayer();
                c4 = BOARD.getPiece(cont, 15).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                {
                    return(true);
                }
            }
            for (int cont = 0; cont < 4; cont++)
            {
                c1 = BOARD.getPiece(cont, 3).getPlayer();
                c2 = BOARD.getPiece(cont, 6).getPlayer();
                c3 = BOARD.getPiece(cont, 9).getPlayer();
                c4 = BOARD.getPiece(cont, 12).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                {
                    return(true);
                }
            }



            for (int cont = 0; cont < 4; cont++)
            {
                c1 = BOARD.getPiece(0, cont).getPlayer();
                c2 = BOARD.getPiece(1, cont + 4).getPlayer();
                c3 = BOARD.getPiece(2, cont + 8).getPlayer();
                c4 = BOARD.getPiece(3, cont + 12).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                {
                    return(true);
                }
            }
            for (int cont = 0; cont < 4; cont++)
            {
                c1 = BOARD.getPiece(3, cont).getPlayer();
                c2 = BOARD.getPiece(2, cont + 4).getPlayer();
                c3 = BOARD.getPiece(1, cont + 8).getPlayer();
                c4 = BOARD.getPiece(0, cont + 12).getPlayer();
                if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
                {
                    return(true);
                }
            }



            c1 = BOARD.getPiece(0, 0).getPlayer();
            c2 = BOARD.getPiece(1, 5).getPlayer();
            c3 = BOARD.getPiece(2, 10).getPlayer();
            c4 = BOARD.getPiece(3, 15).getPlayer();
            if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
            {
                return(true);
            }


            c1 = BOARD.getPiece(3, 0).getPlayer();
            c2 = BOARD.getPiece(2, 5).getPlayer();
            c3 = BOARD.getPiece(1, 10).getPlayer();
            c4 = BOARD.getPiece(0, 15).getPlayer();
            if (c1 == Player && c2 == Player && c3 == Player && c4 == Player)
            {
                return(true);
            }


            return(false);
        }
 public override bool rotate(int x, int y, String direction, int Player)
 {
     if (ACTUAL_PLAYER == Player)
     {
         if (canRotate(x, y, Player))
         {
             DeflexionPiece P1  = ((DeflexionPiece)BOARD.getPiece(x, y));
             String         ori = P1.get_orientation();
             if (direction.Equals(CLOCKWISE, StringComparison.CurrentCultureIgnoreCase))
             {
                 if (ori.Equals("NE", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("SE");
                 }
                 else if (ori.Equals("SE", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("SW");
                 }
                 else if (ori.Equals("SW", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("NW");
                 }
                 else if (ori.Equals("NW", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("NE");
                 }
                 else
                 {
                     Console.WriteLine("Invalid orientation: " + ori);
                 }
             }
             else if (direction.Equals(ANTICLOCKWISE, StringComparison.CurrentCultureIgnoreCase) || direction.Equals(COUNTERCLOCKWISE, StringComparison.CurrentCultureIgnoreCase))
             {
                 if (ori.Equals("NE", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("NW");
                 }
                 else if (ori.Equals("SE", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("NE");
                 }
                 else if (ori.Equals("SW", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("SE");
                 }
                 else if (ori.Equals("NW", StringComparison.CurrentCultureIgnoreCase))
                 {
                     P1.update_orientation("SW");
                 }
                 else
                 {
                     Console.WriteLine("Invalid orientation: " + ori);
                 }
             }
             else
             {
                 Console.WriteLine("Invalid type of rotation.");
                 return(false);
             }
             Console.WriteLine("Deflexion rotation OK");
             BOARD.replacePiece(x, y, P1);
             CONFIRM_MOVE = true;
             endTurn(Player);
             return(true);
         }
     }
     return(false);
 }
        public void auxDisparLaser(int x, int y, String laser_orientation)
        {
            if (BOARD.validCell(x, y))
            {
                Console.Write(x + "," + y + " " + laser_orientation + " ");
                LAST_MESSAGE += x + "," + y + " " + laser_orientation + ";";
                Piece P1 = BOARD.getPiece(x, y);
                if (P1.getClass2() == NullPiece.getClass())
                {
                    if (laser_orientation.Equals("N", StringComparison.CurrentCultureIgnoreCase))
                    {
                        auxDisparLaser(x - 1, y, laser_orientation);
                    }
                    else if (laser_orientation.Equals("S", StringComparison.CurrentCultureIgnoreCase))
                    {
                        auxDisparLaser(x + 1, y, laser_orientation);
                    }
                    else if (laser_orientation.Equals("E", StringComparison.CurrentCultureIgnoreCase))
                    {
                        auxDisparLaser(x, y + 1, laser_orientation);
                    }
                    else if (laser_orientation.Equals("W", StringComparison.CurrentCultureIgnoreCase))
                    {
                        auxDisparLaser(x, y - 1, laser_orientation);
                    }
                    else
                    {
                        Console.WriteLine("ERROR invalid laser orientation - aux_dispar_laser");
                    }
                }
                else if (P1.getClass2() == DeflexionPiece.getClass())
                {
                    String type = ((DeflexionPiece)P1).get_type();
                    if ((type.Equals(DeflexionPiece.PHAROAH, StringComparison.CurrentCultureIgnoreCase)) || (type.Equals(DeflexionPiece.OBELISK, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        burn(x, y);
                        LAST_MESSAGE += P1.getString();
                    }
                    else
                    {
                        String ori = ((DeflexionPiece)P1).get_orientation();

                        String tag1 = "";
                        String tag2 = "";
                        if (laser_orientation.Equals("N", StringComparison.CurrentCultureIgnoreCase))
                        {
                            tag1 = "NE";
                            tag2 = "NW";
                        }
                        else if (laser_orientation.Equals("S", StringComparison.CurrentCultureIgnoreCase))
                        {
                            tag1 = "SE";
                            tag2 = "SW";
                        }
                        else if (laser_orientation.Equals("E", StringComparison.CurrentCultureIgnoreCase))
                        {
                            tag1 = "NE";
                            tag2 = "SE";
                        }
                        else if (laser_orientation.Equals("W", StringComparison.CurrentCultureIgnoreCase))
                        {
                            tag1 = "SW";
                            tag2 = "NW";
                        }

                        if (ori.Equals(tag1, StringComparison.CurrentCultureIgnoreCase) || ori.Equals(tag2, StringComparison.CurrentCultureIgnoreCase))
                        {
                            burn(x, y);
                            LAST_MESSAGE += P1.getString();
                        }
                        else
                        {
                            if ((laser_orientation.Equals("N", StringComparison.CurrentCultureIgnoreCase)) || (laser_orientation.Equals("S", StringComparison.CurrentCultureIgnoreCase)))
                            {
                                if (ori.Equals("NE", StringComparison.CurrentCultureIgnoreCase) || ori.Equals("SE", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    auxDisparLaser(x, y + 1, "E");
                                }
                                else if (ori.Equals("NW", StringComparison.CurrentCultureIgnoreCase) || ori.Equals("SW", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    auxDisparLaser(x, y - 1, "W");
                                }
                                else
                                {
                                    Console.WriteLine("ERROR invalid deflexion: " + laser_orientation + " - " + ori + "- aux_dispar_laser");
                                }
                            }
                            else if ((laser_orientation.Equals("E", StringComparison.CurrentCultureIgnoreCase)) || (laser_orientation.Equals("W", StringComparison.CurrentCultureIgnoreCase)))
                            {
                                if (ori.Equals("NE", StringComparison.CurrentCultureIgnoreCase) || ori.Equals("NW", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    auxDisparLaser(x - 1, y, "N");
                                }
                                else if (ori.Equals("SE", StringComparison.CurrentCultureIgnoreCase) || ori.Equals("SW", StringComparison.CurrentCultureIgnoreCase))
                                {
                                    auxDisparLaser(x + 1, y, "S");
                                }
                                else
                                {
                                    Console.WriteLine("ERROR invalid deflexion2: " + laser_orientation + " - " + ori + "- aux_dispar_laser");
                                }
                            }
                            else
                            {
                                Console.WriteLine("ERROR invalid deflexion3: " + laser_orientation + " - " + ori + "- aux_dispar_laser");
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("ERROR invalid class piece type - aux_dispar_laser");
                }
            }
            else
            {
                Console.WriteLine("No piece burned.");
            }
        }
        public virtual bool inCheck(int Player)
        {
            Position KingPosition = findKing(Player);
            int      KingX        = KingPosition.X;
            int      KingY        = KingPosition.Y;
            int      x2           = KingX;
            int      y2           = KingY;

            for (int x = 0; x <= 8; x++)
            {
                for (int y = 0; y <= 8; y++)
                {
                    if (BOARD.getPiece(x, y).getClass2().Equals(ChessPiece.getClass()))
                    {
                        if (((ChessPiece)BOARD.getPiece(x, y)).PLAYER != Player)
                        {
                            ChessPiece P1 = (ChessPiece)BOARD.getPiece(x, y);
                            int        x1 = x;
                            int        y1 = y;

                            if (P1.get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
                            {
                                if (Player == PLAYER1)
                                {
                                    if ((x2 - x1 == -1) && Math.Abs(y2 - y1) == 1)
                                    {
                                        return(true);
                                    }
                                }
                                else if (Player == PLAYER2)
                                {
                                    if ((x2 - x1 == 1) && Math.Abs(y2 - y1) == 1)
                                    {
                                        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);
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
        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);
        }
Beispiel #20
0
        public override bool canMove(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);
            }
            if (BOARD.getPiece(x2, y2).getClass2() != NullPiece.getClass())
            {
                return(false);
            }

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

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

            if (P1.getPlayer() != Player)
            {
                Console.WriteLine("player:" + P1.getPlayer());
                return(false);
            }



            if ((Math.Abs(x2 - x1) == 1 && Math.Abs(y2 - y1) == 1) && !EAT_MOVE)
            {
                if (P1.TYPE == CheckersPiece.CHECKER)
                {
                    return(true);
                }
                else if (P1.TYPE == CheckersPiece.SIMPLE)
                {
                    if (Player == PLAYER1)
                    {
                        if (x2 > x1)
                        {
                            return(true);
                        }
                    }
                    else if (Player == PLAYER2)
                    {
                        if (x2 < x1)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid player name: " + Player);
                    }
                }
                else
                {
                    Console.WriteLine("Invalid piece type: " + P1.TYPE);
                }
            }
            else
            {
                if ((Math.Abs(x2 - x1) == 2 && Math.Abs(y2 - y1) == 2) && (!EAT_MOVE || EAT_PIECE == BOARD.getPiece(x1, y1)))
                {
                    int x_adversary = x1 + (x2 - x1) / 2;
                    int y_adversary = y1 + (y2 - y1) / 2;
                    if (isAdversary((Piece)BOARD.getPiece(x_adversary, y_adversary), Player))
                    {
                        if (P1.TYPE == CheckersPiece.CHECKER)
                        {
                            if (canEat(x_adversary, y_adversary))
                            {
                                return(true);
                            }
                        }
                        else if (P1.TYPE == CheckersPiece.SIMPLE)
                        {
                            if (Player == PLAYER1)
                            {
                                if (x2 > x1)
                                {
                                    if (canEat(x_adversary, y_adversary))
                                    {
                                        return(true);
                                    }
                                }
                            }
                            else if (Player == PLAYER2)
                            {
                                if (x2 < x1)
                                {
                                    if (canEat(x_adversary, y_adversary))
                                    {
                                        return(true);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid player name: " + Player);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid piece type: " + P1.TYPE);
                        }
                    }
                }
            }
            return(false);
        }
        public override bool move(int x1, int y1, int x2, int y2, int Player)
        {
            bool  eat         = false;
            Piece eaten_piece = BOARD.getPiece(x2, y2);
            bool  CheckMate   = false;

            if (canMove(x1, y1, x2, y2, Player))
            {
                int x3 = x2;
                int y3 = y2;
                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);

                    x3 = x1;
                    y3 = y2;
                }
                if (eaten_piece.getClass2() != NullPiece.getClass())
                {
                    eat = true;
                }



                if ((x1 == 0 && y1 == 0) && (!LEFT_ROOK_PLAYER2_MOVED))
                {
                    LEFT_ROOK_PLAYER2_MOVED = true;
                }
                else if ((x1 == 0 && y1 == 7) && (!RIGHT_ROOK_PLAYER2_MOVED))
                {
                    RIGHT_ROOK_PLAYER2_MOVED = true;
                }
                else if ((x1 == 7 && y1 == 0) && (!LEFT_ROOK_PLAYER1_MOVED))
                {
                    LEFT_ROOK_PLAYER1_MOVED = true;
                }
                else if ((x1 == 7 && y1 == 7) && (!RIGHT_ROOK_PLAYER1_MOVED))
                {
                    RIGHT_ROOK_PLAYER1_MOVED = true;
                }

                if (((ChessPiece)BOARD.getPiece(x1, y1)).get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase))
                {
                    if (Player == PLAYER1)
                    {
                        KING_PLAYER1_MOVED = true;
                    }
                    else
                    {
                        KING_PLAYER2_MOVED = true;
                    }
                }



                bool change_pawn = false;
                if (((ChessPiece)BOARD.getPiece(x1, y1)).get_type().Equals(ChessPiece.PAWN, StringComparison.CurrentCultureIgnoreCase))
                {
                    if (x2 == 0 || x2 == 7)
                    {
                        change_pawn = true;
                    }
                }


                if (x1 == 7 && y1 == 4 && ((ChessPiece)BOARD.getPiece(x1, y1)).get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase) && Math.Abs(y2 - y1) > 1)
                {
                    if (y2 == 2)
                    {
                        BOARD.movePiece(7, 0, 7, 3);
                    }
                    else
                    {
                        BOARD.movePiece(7, 7, 7, 5);
                    }
                }
                else if (x1 == 0 && y1 == 4 && ((ChessPiece)BOARD.getPiece(x1, y1)).get_type().Equals(ChessPiece.KING, StringComparison.CurrentCultureIgnoreCase) && Math.Abs(y2 - y1) > 1)
                {
                    if (y2 == 2)
                    {
                        BOARD.movePiece(0, 0, 0, 3);
                    }
                    else
                    {
                        BOARD.movePiece(0, 7, 0, 5);
                    }
                }

                BOARD.movePiece(x1, y1, x2, y2);


                if (change_pawn)
                {
                    if (x2 == 0)
                    {
                        BOARD.addPiece(x2, y2, new ChessPiece("X", PLAYER1, ChessPiece.QUEEN));
                    }
                    else
                    {
                        BOARD.addPiece(x2, y2, new ChessPiece("O", PLAYER2, ChessPiece.QUEEN));
                    }
                }

                String CheckTag = "";
                if (Player == PLAYER1 && inCheck(PLAYER2))
                {
                    CheckTag = "<CHECK MESSAGE>PLAYER2 IN CHECK</CHECK MESSAGE>";
                }
                else if (Player == PLAYER2 && inCheck(PLAYER1))
                {
                    CheckTag = "<CHECK MESSAGE>PLAYER1 IN CHECK</CHECK MESSAGE>";
                }


                if (eat)
                {
                    LAST_ACTION = new MoveAndEatAction(x1, y1, x2, y2, BOARD.getPiece(x2, y2), eaten_piece, x3, y3);
                }
                else
                {
                    LAST_ACTION = new MoveAction(x1, y1, x2, y2, BOARD.getPiece(x2, y2));
                }


                if (ACTUAL_PLAYER == PLAYER1)
                {
                    ACTUAL_PLAYER = PLAYER2;
                }
                else
                {
                    ACTUAL_PLAYER = PLAYER1;
                }

                CheckMate = inCheckMate(ACTUAL_PLAYER);
                if (CheckMate)
                {
                    if (ACTUAL_PLAYER == PLAYER1)
                    {
                        CheckTag = "<CHECK MESSAGE>PLAYER1 IN CHECKMATE</CHECK MESSAGE>";
                        WINNER   = PLAYER2;
                    }
                    else
                    {
                        CheckTag = "<CHECK MESSAGE>PLAYER2 IN CHECKMATE</CHECK MESSAGE>";
                        WINNER   = PLAYER1;
                    }
                    GAME_ENDED = true;
                }
                LAST_MESSAGE = "<LAST ACTION>" + LAST_ACTION.action2String() + "</LAST ACTION>" + CheckTag;
                if (CheckMate)
                {
                    Console.WriteLine("   CheckMate: " + CheckMate);
                }
                return(true);
            }
            return(false);
        }