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 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 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); }
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); }