def test_move_valid_piece_move(self): g = self.makeOne() # Try to move white pawn 4 squares ahead with pytest.raises(InvalidMove): g.player_move(Square("a2"), Square("a6")) # Try to move white pawn diagonally like a bishop with pytest.raises(InvalidMove): g.player_move(Square("a2"), Square("d4"))
def get_move(self) -> Tuple[Square, Square]: move = input(f"[{self.game.turn}] >>> ") if move == "exit": raise KeyboardInterrupt try: assert len(move) == 4 return Square(move[:2]), Square(move[2:]) except (AssertionError, InvalidSquare): print("Invalid move! Try again...") return self.get_move()
def test_player_move_on_wrong_turn(self): g = self.makeOne() # Try to move black piece on white's turn self.assertEqual(g.turn, "white") with pytest.raises(InvalidMove): g.player_move(Square("a7"), Square("a6")) # Try to move white piece on black's turn g.pass_turn() self.assertEqual(g.turn, "black") with pytest.raises(InvalidMove): g.player_move(Square("a2"), Square("a3"))
def test_horizontal_and_vertical(self): for square in all_squares(): self.assertEqual(len(self._makeOne(square)), 14) # Check that it always stays inthe same row or in the same # column for m in self._makeOne(Square("a1")): self.assertTrue( (m.col == 0 and m.row in range(8)) or (m.col in range(8) and m.row == 0) ) for m in self._makeOne(Square("a5")): self.assertTrue( (m.col == 0 and m.row in range(8)) or (m.col in range(8) and m.row == 4) )
def all_moves(self) -> Set[Square]: moves = set({}) # On starting position, pawn can jump 2 squares pos = self.pos if pos.row == self.start_row: col = pos.col row = pos.row + (self.direction * 2) moves.update({Square(col=col, row=row)}) # Moving one square ahead + capturing moves for side in (-1, 0, 1): row = pos.row + self.direction col = pos.col + side if self._in_board(col, row): moves.update({Square(col=col, row=row)}) return moves
def _all_moves(self) -> Set[Square]: moves = set({}) for vector in self._deltas or []: x, y = vector factor = 1 while True: col = self.pos.col row = self.pos.row if x != 0: col += x * factor if y != 0: row += y * factor factor = factor + 1 if not self._in_board(col, row): break else: moves.update({Square(col=col, row=row)}) return moves
def all_moves(self) -> Set[Square]: moves = set({}) for (x, y) in [ [1, 0], [1, 1], [1, -1], [0, -1], [0, 1], [-1, 0], [-1, 1], [-1, -1], ]: col = self.pos.col + x row = self.pos.row + y if not self._in_board(col, row): continue moves.update({Square(col=col, row=row)}) return moves
def print_board(self) -> None: board = self.game.board tmp = [" ", "a", "b", "c", "d", "e", "f", "g", "h"] bpoints = self.game.advantage("black") wpoints = self.game.advantage("white") print(" ".join(tmp + [f"\tblack: {bpoints}"])) for row in reversed(range(8)): rowprint = [] rowprint.append(str(row + 1)) for col in range(8): sq = Square(col=col, row=row) piece: Optional[IPiece] = board.get_piece(sq) if piece is None: rowprint.append(" ") else: abbr = piece.abbr if piece.color == "black": abbr = abbr.lower() rowprint.append(UNICODE_PIECES[abbr]) rowprint.append(str(row + 1)) print(" ".join(rowprint)) print(" ".join(tmp + [f"\twhite: {wpoints}"]))
def all_squares(): for i in range(8): for j in range(8): yield Square([i, j])
def test_all_moves_black(self): self.assertEqual( self._makeOne(Square("a8"), color="black"), {Square("a7"), Square("b7")} ) self.assertEqual( self._makeOne(Square("a7"), color="black"), {Square("a6"), Square("a5"), Square("b6")}, ) self.assertEqual( self._makeOne(Square("a6"), color="black"), {Square("a5"), Square("b5")} ) self.assertEqual( self._makeOne(Square("a4"), color="black"), {Square("a3"), Square("b3")} ) self.assertEqual(self._makeOne(Square("a1"), color="black"), set({})) self.assertEqual( self._makeOne(Square("b7"), color="black"), {Square("b5"), Square("b6"), Square("a6"), Square("c6")}, ) self.assertEqual( self._makeOne(Square("b5"), color="black"), {Square("b4"), Square("c4"), Square("a4")}, ) self.assertEqual( self._makeOne(Square("b4"), color="black"), {Square("b3"), Square("c3"), Square("a3")}, ) self.assertEqual(self._makeOne(Square("b1"), color="black"), set({})) self.assertEqual( self._makeOne(Square("h8"), color="black"), {Square("h7"), Square("g7")} ) self.assertEqual( self._makeOne(Square("h7"), color="black"), {Square("h6"), Square("h5"), Square("g6")}, ) self.assertEqual( self._makeOne(Square("h6"), color="black"), {Square("h5"), Square("g5")} ) self.assertEqual( self._makeOne(Square("h5"), color="black"), {Square("h4"), Square("g4")} ) self.assertEqual(self._makeOne(Square("h1"), color="black"), set({}))
def test_all_moves(self): for s in all_squares(): self.assertIn(len(self._makeOne(s)), [3, 5, 8]) self.assertEqual( self._makeOne(Square("a1")), {Square("a2"), Square("b2"), Square("b1")} ) self.assertEqual( self._makeOne(Square("c4")), { Square("c3"), Square("b3"), Square("d3"), Square("c5"), Square("b5"), Square("d5"), Square("d4"), Square("b4"), }, )
def clear(self): self._board: Dict[Square, Optional[Piece]] = { Square(col=col, row=row): None for col in range(8) for row in range(8) }
def test_all_moves_white(self): self.assertEqual(self._makeOne(Square("a1")), {Square("a2"), Square("b2")}) self.assertEqual( self._makeOne(Square("a2")), {Square("a3"), Square("a4"), Square("b3")} ) self.assertEqual(self._makeOne(Square("a3")), {Square("a4"), Square("b4")}) self.assertEqual(self._makeOne(Square("a4")), {Square("a5"), Square("b5")}) self.assertEqual(self._makeOne(Square("a8")), set({})) self.assertEqual( self._makeOne(Square("b2")), {Square("b3"), Square("b4"), Square("a3"), Square("c3")}, ) self.assertEqual( self._makeOne(Square("b3")), {Square("b4"), Square("c4"), Square("a4")} ) self.assertEqual( self._makeOne(Square("b4")), {Square("b5"), Square("c5"), Square("a5")} ) self.assertEqual(self._makeOne(Square("b8")), set({})) self.assertEqual(self._makeOne(Square("h1")), {Square("h2"), Square("g2")}) self.assertEqual( self._makeOne(Square("h2")), {Square("h3"), Square("h4"), Square("g3")} ) self.assertEqual(self._makeOne(Square("h3")), {Square("h4"), Square("g4")}) self.assertEqual(self._makeOne(Square("h4")), {Square("h5"), Square("g5")}) self.assertEqual(self._makeOne(Square("h8")), set({}))
def test_all_moves(self): for i in range(0, 8): self.assertEqual(len(self._makeOne(Square((0, i)))), 21) self.assertEqual(len(self._makeOne(Square((i, 0)))), 21) self.assertEqual(len(self._makeOne(Square((7, i)))), 21) self.assertEqual(len(self._makeOne(Square((i, 7)))), 21) for i in range(1, 7): self.assertEqual(len(self._makeOne(Square((1, i)))), 23) self.assertEqual(len(self._makeOne(Square((i, 1)))), 23) self.assertEqual(len(self._makeOne(Square((6, i)))), 23) self.assertEqual(len(self._makeOne(Square((i, 6)))), 23) for i in range(2, 6): self.assertEqual(len(self._makeOne(Square((2, i)))), 25) self.assertEqual(len(self._makeOne(Square((i, 2)))), 25) self.assertEqual(len(self._makeOne(Square((5, i)))), 25) self.assertEqual(len(self._makeOne(Square((i, 5)))), 25) for i in range(3, 5): self.assertEqual(len(self._makeOne(Square((3, i)))), 27) self.assertEqual(len(self._makeOne(Square((i, 3)))), 27) self.assertEqual(len(self._makeOne(Square((4, i)))), 27) self.assertEqual(len(self._makeOne(Square((i, 4)))), 27)
def test_diagonals(self): for square in all_squares(): square_color = square.color moves = self._makeOne(square) for m in moves: assert m.color == square_color # From all the board perimeter squares, bishop has only 7 # possible moves for i in range(0, 8): self.assertEqual(len(self._makeOne(Square((0, i)))), 7) self.assertEqual(len(self._makeOne(Square((i, 0)))), 7) self.assertEqual(len(self._makeOne(Square((7, i)))), 7) self.assertEqual(len(self._makeOne(Square((i, 7)))), 7) # Thes the inner perimeter squares for i in range(1, 7): self.assertEqual(len(self._makeOne(Square((1, i)))), 9) self.assertEqual(len(self._makeOne(Square((i, 1)))), 9) self.assertEqual(len(self._makeOne(Square((6, i)))), 9) self.assertEqual(len(self._makeOne(Square((i, 6)))), 9) for i in range(2, 6): self.assertEqual(len(self._makeOne(Square((2, i)))), 11) self.assertEqual(len(self._makeOne(Square((i, 2)))), 11) self.assertEqual(len(self._makeOne(Square((5, i)))), 11) self.assertEqual(len(self._makeOne(Square((i, 5)))), 11) for i in range(3, 5): self.assertEqual(len(self._makeOne(Square((3, i)))), 13) self.assertEqual(len(self._makeOne(Square((i, 3)))), 13) self.assertEqual(len(self._makeOne(Square((4, i)))), 13) self.assertEqual(len(self._makeOne(Square((i, 4)))), 13)
def test_player_move_where_no_piece_is_found(self): g = self.makeOne() with pytest.raises(InvalidMove): g.player_move(Square("a4"), Square("a5"))
def test_all_moves(self): for sq in ("a1", "a8", "h1", "h8"): self.assertEqual(len(self._makeOne(Square(sq))), 2) for sq in ("a2", "a7", "h2", "h7"): self.assertEqual(len(self._makeOne(Square(sq))), 3) for sq in ("a3", "a6", "h3", "h6"): self.assertEqual(len(self._makeOne(Square(sq))), 4) for sq in ("a3", "a6", "h3", "h6"): self.assertEqual(len(self._makeOne(Square(sq))), 4) self.assertEqual( self._makeOne(Square("d5")), { Square("c7"), Square("e7"), Square("c3"), Square("e3"), Square("f6"), Square("f4"), Square("b6"), Square("b4"), }, )
def get_square(pos: Coordinate) -> Square: if isinstance(pos, str): return Square(pos) # type: ignore return pos