Exemplo n.º 1
0
from src.Chess2 import Board

board = Board()
color_to_move = "white"


def switch_turn(color):
    if color == "white":
        return "black"
    else:
        return "white"


print(board.display())
while not board.game_ended:

    move = input("Enter move in the form <from_square>,<to_square>:")
    from_square = move.split(",")[0]
    to_square = move.split(",")[1]
    if board.move_is_legal(from_square, to_square, color_to_move):
        board.move_if_legal(from_square, to_square, color_to_move)
        color_to_move = switch_turn(color_to_move)
        print("")
        print(board.display())
    else:
        print("illegal move!")
else:
    print(board.result)
Exemplo n.º 2
0
class TestBoard(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_boardCorrectlySetUp_AfterInit(self):
        self.assertEqual("white rook", self.board.a1)
        self.assertEqual("white rook", self.board.h1)
        self.assertEqual("white knight", self.board.b1)
        self.assertEqual("white knight", self.board.g1)
        self.assertEqual("white bishop", self.board.c1)
        self.assertEqual("white bishop", self.board.f1)
        self.assertEqual("white queen", self.board.d1)
        self.assertEqual("white king", self.board.e1)
        self.assertEqual("white pawn", self.board.a2)
        self.assertEqual("white pawn", self.board.b2)
        self.assertEqual("white pawn", self.board.c2)
        self.assertEqual("white pawn", self.board.d2)
        self.assertEqual("white pawn", self.board.e2)
        self.assertEqual("white pawn", self.board.f2)
        self.assertEqual("white pawn", self.board.g2)
        self.assertEqual("white pawn", self.board.h2)
        self.assertEqual("black rook", self.board.a8)
        self.assertEqual("black rook", self.board.h8)
        self.assertEqual("black knight", self.board.b8)
        self.assertEqual("black knight", self.board.g8)
        self.assertEqual("black bishop", self.board.c8)
        self.assertEqual("black bishop", self.board.f8)
        self.assertEqual("black queen", self.board.d8)
        self.assertEqual("black king", self.board.e8)
        self.assertEqual("black pawn", self.board.a7)
        self.assertEqual("black pawn", self.board.b7)
        self.assertEqual("black pawn", self.board.c7)
        self.assertEqual("black pawn", self.board.d7)
        self.assertEqual("black pawn", self.board.e7)
        self.assertEqual("black pawn", self.board.f7)
        self.assertEqual("black pawn", self.board.g7)
        self.assertEqual("black pawn", self.board.h7)

    def test_pieceXonSquareY_AfterPut(self):
        self.board.put("whitePawn", "e4")
        self.assertEqual("whitePawn", self.board.e4)
        self.board.put("blackPawn", "e5")
        self.assertEqual("blackPawn", self.board.e5)

    def test_squareEmpty_AfterFreeSquare(self):
        self.board.free_square("e2")
        self.assertEqual("empty", self.board.e2)

    def test_isSquareOccupied(self):
        self.board.is_occupied("e2")
        self.assertTrue(self.board.is_occupied("e2"))

    def test_squareOccupiedByColor(self):
        self.assertEqual("white", self.board.color_that_occupies("e2"))
        self.assertEqual("black", self.board.color_that_occupies("e7"))
        self.assertEqual("empty", self.board.color_that_occupies("e3"))

    def test_isMoveFromXtoYVertical(self):
        self.assertTrue(move_is_vertical("e2", "e4"))
        self.assertFalse(move_is_vertical("e2", "h2"))
        self.assertFalse(move_is_vertical("e2", "f4"))

    def test_isMoveFromXtoYHorizontal(self):
        self.assertFalse(move_is_horizontal("e2", "e4"))
        self.assertTrue(move_is_horizontal("e2", "h2"))
        self.assertFalse(move_is_horizontal("e2", "f4"))

    def test_isMoveFromXtoYDiagonal(self):
        self.assertFalse(move_is_diagonal("e2", "e4"))
        self.assertFalse(move_is_diagonal("e2", "h2"))
        self.assertFalse(move_is_diagonal("e2", "f4"))
        self.assertTrue(move_is_diagonal("e2", "g4"))

    def test_getDistanceBetweenColumns(self):
        self.assertEqual(5, get_distance_between_columns("a2", "f4"))
        self.assertEqual(1, get_distance_between_columns("b2", "a4"))

    def test_moveFromXtoY(self):
        self.board.move("e2", "e4")
        self.assertEqual("empty", self.board.e2)
        self.assertEqual("white pawn", self.board.e4)

    def test_getMoveLength(self):
        self.assertEqual(4, get_move_length("a2", "a6"))
        self.assertEqual(6, get_move_length("b2", "h8"))
        self.assertEqual(2, get_move_length("f6", "h6"))

    def test_isMoveFromXtoYaKnightMove(self):
        self.assertTrue(move_is_knightmove("e2", "f4"))
        self.assertTrue(move_is_knightmove("e2", "g3"))
        self.assertFalse(move_is_knightmove("e2", "g4"))

    def test_getMoveVector(self):
        self.assertEqual((2, 1), get_move_vector("e2", "f4"))

    def test_squareOnBoard(self):
        self.assertTrue(self.board.is_on_board("e2"))
        self.assertFalse(self.board.is_on_board("t7"))

    def test_pathClear(self):
        self.assertTrue(self.board.path_is_clear("a2", "a7"))
        self.assertFalse(self.board.path_is_clear("a2", "a8"))
        self.assertTrue(self.board.path_is_clear("a3", "h3"))
        self.assertFalse(self.board.path_is_clear("a1", "e1"))
        self.assertTrue(self.board.path_is_clear("a2", "f7"))
        self.assertFalse(self.board.path_is_clear("a2", "g8"))

    def test_getSquaresBetween(self):
        self.assertSetEqual({"a3", "a4", "a5", "a6"},
                            get_squares_in_between("a2", "a7"))
        self.assertSetEqual({"b3", "c3", "d3"},
                            get_squares_in_between("a3", "e3"))
        self.assertSetEqual({"b3", "c4"}, get_squares_in_between("a2", "d5"))

    def test_getSquaresBetweenVertical(self):
        self.assertSetEqual({"a3", "a4", "a5", "a6"},
                            get_squares_in_between_vertical("a2", "a7"))
        self.assertSetEqual({"a3", "a4", "a5", "a6"},
                            get_squares_in_between_vertical("a7", "a2"))

    def test_getSquaresBetweenHorizontal(self):
        self.assertSetEqual({"b3", "c3", "d3"},
                            get_squares_in_between_horizontal("a3", "e3"))
        self.assertSetEqual({"b3", "c3", "d3"},
                            get_squares_in_between_horizontal("e3", "a3"))

    def test_reverseDict(self):
        self.assertDictEqual({1: "a", 2: "b"}, reverse_dict({"a": 1, "b": 2}))

    def test_getSquaresBetweenDiagonal(self):
        self.assertSetEqual({"b3", "c4"},
                            get_squares_in_between_diagonal("a2", "d5"))
        self.assertSetEqual({"c4", "b3"},
                            get_squares_in_between_diagonal("d5", "a2"))
        self.assertSetEqual({"e2", "d3"},
                            get_squares_in_between_diagonal("f1", "c4"))
        self.assertSetEqual({"d3", "e2"},
                            get_squares_in_between_diagonal("c4", "f1"))

    def test_whichKindOfPieceOnSquare(self):
        self.assertEqual("bishop", self.board.get_kind_of_piece_on("c1"))
        self.assertEqual("queen", self.board.get_kind_of_piece_on("d1"))

    def test_pieceCannotMoveFromEmptySquare(self):
        self.assertFalse(self.board.is_reachable("d4", "d5", "white"))

    def test_pieceCannotJumpOverOtherPiece_ExceptKnight(self):
        self.board.put("white pawn", "e3")
        self.board.put("black pawn", "d3")
        self.assertFalse(self.board.is_reachable("e2", "e4", "white"))
        self.assertFalse(self.board.is_reachable("d2", "d4", "white"))
        self.assertFalse(self.board.is_reachable("a1", "a4", "white"))

    def test_squareOccupiedByOppColor(self):
        self.assertTrue(
            self.board.square_is_occupied_by_own_color("white", "e2"))

    def testSign(self):
        self.assertEqual(-1, sign(-4))
        self.assertEqual(1, sign(4))

    def test_PawnOnInitialSquare(self):
        self.assertTrue(self.board.pawn_is_on_initial_square("e2"))
        self.assertTrue(self.board.pawn_is_on_initial_square("e7"))
        self.assertFalse(self.board.pawn_is_on_initial_square("g3"))
        self.assertFalse(self.board.pawn_is_on_initial_square("a5"))

    def test_isCorrectColorMoved(self):
        self.assertTrue(self.board.correct_color_is_moved("e2", "white"))
        self.assertFalse(self.board.correct_color_is_moved("e2", "black"))

    def test_pieceCannotMoveOnSquareOccupiedByPieceOfSameColor(self):
        self.assertFalse(self.board.is_reachable("e1", "e2", "white"))
        self.assertFalse(self.board.is_reachable("c1", "b2", "white"))

    def test_undoLastMove(self):
        self.board.move("e2", "e4")
        self.board.undo_last_move()
        self.assertTrue(self.board.is_occupied("e2"))
        self.assertFalse(self.board.is_occupied("e4"))

    def test_undoLastMoveWithCapture(self):
        self.board.move("e2", "e4")
        self.board.move("d7", "d5")
        self.board.move("e4", "d5")
        self.board.undo_last_move()
        self.assertEqual("black pawn", self.board.d5)
        self.assertEqual("white pawn", self.board.e4)

    def test_getOppColor(self):
        self.assertEqual("white", get_opposite_color("black"))
        self.assertEqual("black", get_opposite_color("white"))

    def test_addOrSubtractOne(self):
        self.assertEqual(2, add_or_subtract_one(1, '+'))
        self.assertEqual(4, add_or_subtract_one(5, '-'))

    @unittest.skip
    def test_displayBoard(self):
        output = self.board.display()
        white_color = "\033[96m"
        black_color = "\033[94m"
        neutral_color = "\033[0m"
        expected_output = (
            "-" * 9 * 8 + "\n" + (" " * 8 + "|") * 8 + "\n" + black_color +
            " rook   " + neutral_color + "|" + black_color + " knight " +
            neutral_color + "|" + black_color + " bishop " + neutral_color +
            "|" + black_color + " queen  " + neutral_color + "|" +
            black_color + " king   " + neutral_color + "|" + black_color +
            " bishop " + neutral_color + "|" + black_color + " knight " +
            neutral_color + "|" + black_color + " rook   " + neutral_color +
            "|" + "\n" + (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + black_color + " pawn   " +
            neutral_color + "|" + black_color + " pawn   " + neutral_color +
            "|" + black_color + " pawn   " + neutral_color + "|" +
            black_color + " pawn   " + neutral_color + "|" + black_color +
            " pawn   " + neutral_color + "|" + black_color + " pawn   " +
            neutral_color + "|" + black_color + " pawn   " + neutral_color +
            "|" + black_color + " pawn   " + neutral_color + "|" + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "        " + neutral_color + "|" + "        " +
            neutral_color + "|" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "\n" + (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "        " + neutral_color + "|" + "        " +
            neutral_color + "|" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "\n" + (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "        " + neutral_color + "|" + "        " +
            neutral_color + "|" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "\n" + (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "        " + neutral_color + "|" + "        " +
            neutral_color + "|" + "        " + neutral_color + "|" +
            "        " + neutral_color + "|" + "        " + neutral_color +
            "|" + "\n" + (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + white_color + " pawn   " +
            neutral_color + "|" + white_color + " pawn   " + neutral_color +
            "|" + white_color + " pawn   " + neutral_color + "|" +
            white_color + " pawn   " + neutral_color + "|" + white_color +
            " pawn   " + neutral_color + "|" + white_color + " pawn   " +
            neutral_color + "|" + white_color + " pawn   " + neutral_color +
            "|" + white_color + " pawn   " + neutral_color + "|" + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8 + "\n" +
            (" " * 8 + "|") * 8 + "\n" + white_color + " rook   " +
            neutral_color + "|" + white_color + " knight " + neutral_color +
            "|" + white_color + " bishop " + neutral_color + "|" +
            white_color + " queen  " + neutral_color + "|" + white_color +
            " king   " + neutral_color + "|" + white_color + " bishop " +
            neutral_color + "|" + white_color + " knight " + neutral_color +
            "|" + white_color + " rook   " + neutral_color + "|" + "\n" +
            (" " * 8 + "|") * 8 + "\n" + "-" * 9 * 8)
        print(expected_output)
        self.assertEqual(expected_output, output)