def test_in_bounds(self): self.assertTrue( all(position.in_bounds for position in self.flat_positions)) self.assertFalse( any(Position(-1, col).in_bounds for col in range(-1, 9))) self.assertFalse( any(Position(8, col).in_bounds for col in range(-1, 9))) self.assertFalse( any(Position(row, -1).in_bounds for row in range(-1, 9))) self.assertFalse( any(Position(row, 8).in_bounds for row in range(-1, 9)))
def newGame(self, fen=None): #if we have a boardstring if fen: turn = self.position.fen.turn self.position = Position(fen) self._start_name = "Custom" # else let it set to the default position else: self.position = Position() self._start_name = "Start"
def test_from_notation(self): for row, notation_row in enumerate(self.notation): for col, square in enumerate(notation_row): board_piece = self.board[row][col] if square == "": self.assertEqual(board_piece, None) else: self.assertEqual(Position(row, col), board_piece.position) self.assertEqual(Color.from_notation(square[0]), board_piece.color) self.assertEqual(Piece.from_notation(square[1]), type(board_piece))
def test_eq_hash(self): for row, position_row in enumerate(self.positions): for col, position in enumerate(position_row): others = self.flat_positions - {position} # test equality self.assertEqual(position, Position(row, col)) self.assertFalse(any(position == other for other in others)) # test hash self.assertIn(position, self.flat_positions) self.assertNotIn(position, others)
def test_add(self): board = Board() self.assertTrue(all(square == None for square in board.squares())) board.add(Color.white, Pawn, Position(3, 4)) board.add(Color.black, Pawn, Position(6, 6)) self.assertEqual(Color.white, board.at(Position(3, 4)).color) self.assertEqual(Pawn, type(board.at(Position(3, 4)))) self.assertEqual(Color.black, board.at(Position(6, 6)).color) self.assertEqual(Pawn, type(board.at(Position(6, 6)))) self.assertEqual(2, len(list(board.pieces()))) for position in board.positions(): if position not in (Position(3, 4), Position(6, 6)): self.assertIsNone(board.at(position))
def setUp(self): self.positions = [[Position(row, col) for col in range(8)] for row in range(8)] self.flat_positions = set(itertools.chain(*self.positions)) self.notations = [["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1"], ["A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2"], ["A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3"], ["A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4"], ["A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5"], ["A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6"], ["A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7"], ["A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8"]]
def test_possible_moves(self): upper_moves = self.upper_rook.possible_moves self.assertEqual(10, len(upper_moves)) self.assertIn(Position(0, 1), upper_moves) # move up self.assertIn(Position(1, 0), upper_moves) # move left self.assertContainsAll(upper_moves, [Position(2, 1), Position(3, 1)]) # moves down self.assertContainsAll(upper_moves, [Position(1, col) for col in range(2, 8)]) # moves right left_moves = self.left_rook.possible_moves self.assertEqual(7, len(left_moves)) self.assertContainsAll(left_moves, [Position(3, 1), Position(2, 1)]) # moves up self.assertIn(Position(4, 0), left_moves) # moves left self.assertContainsAll(left_moves, [Position(row, 1) for row in range(5, 8)]) # moves down self.assertIn(Position(4, 2), left_moves) # moves right right_moves = self.right_rook.possible_moves self.assertEqual(10, len(right_moves)) self.assertContainsAll(right_moves, [Position(row, 5) for row in range(4)]) # moves up self.assertIn(Position(4, 4), right_moves) # moves left self.assertContainsAll(right_moves, [Position(row, 5) for row in range(5, 8)]) # moves down self.assertContainsAll(right_moves, [Position(4, col) for col in range(6, 8)]) # moves right middle_moves = self.middle_rook.possible_moves self.assertEqual(9, len(middle_moves)) self.assertContainsAll(middle_moves, [Position(row, 3) for row in range(4)]) # moves up self.assertIn(Position(4, 2), middle_moves) # moves left self.assertIn(Position(4, 4), middle_moves) # moves right self.assertContainsAll(middle_moves, [Position(row, 3) for row in range(5, 8)]) # moves down lower_moves = self.lower_rook.possible_moves self.assertEqual(14, len(lower_moves)) self.assertContainsAll(lower_moves, [Position(row, 7) for row in range(6)]) # moves up self.assertContainsAll(lower_moves, [Position(6, col) for col in range(7)]) # moves left self.assertIn(Position(7, 7), lower_moves) # moves down
def move(self, begin, end): begin_position = Position.from_notation(str(begin)) end_position = Position.from_notation(str(end)) self._board.at(begin_position).move_to(end_position) return self.turn()
def position(self): return Position(self.row, self.col)
def setUp(self): self.board = Board() self.wpawn = self.board.add(Color.white, Pawn, Position(1, 4)) self.bpawn = self.board.add(Color.black, Pawn, Position(2, 3))
def test_possible_moves(self): self.assertListEqual(self.wpawn.possible_moves, [Position(2, 4), Position(3, 4)]) self.assertListEqual(self.bpawn.possible_moves, [Position(1, 3)])
class ChessLibGameEngine(object): def __init__(self): self.castles = { ('K', 'e1', 'g1'): Move.from_uci('h1f1'), ('K', 'e1', 'c1'): Move.from_uci('a1d1'), ('k', 'e8', 'g8'): Move.from_uci('h8f8'), ('k', 'e8', 'c8'): Move.from_uci('a8d8'), } self.castles_reversed = { ('K', 'g1', 'e1'): Move.from_uci('f1h1'), ('K', 'c1', 'e1'): Move.from_uci('d1a1'), ('k', 'g8', 'e8'): Move.from_uci('f8h8'), ('k', 'c8', 'e8'): Move.from_uci('d8a8'), } self.position = None self._start_name = None @property def fen(self): return self.position and str(self.position.fen) def newGame(self, fen=None): #if we have a boardstring if fen: turn = self.position.fen.turn self.position = Position(fen) self._start_name = "Custom" # else let it set to the default position else: self.position = Position() self._start_name = "Start" def makeMove(self, move): san = SanNotation(self.position, move) piece = self.position[move.source] captured = self.position[move.target] movenum = self.position.fen.full_move iswhite = self.position.fen.turn == 'w' before = self.fen self.position.make_move(move) after = self.fen game_move = GameMove( str(san), movenum, iswhite, before, after, move.source, move.target, captured, piece ) return game_move def makeMoveFromSan(self, san): move = SanNotation.to_move(self.position, san) return self.makeMove(move) def sanToMove(self, san): return SanNotation.to_move(self.position, san) def validateMove(self, move): for m in self.position.get_legal_moves(): if m == move: return True return False def castleRookMove(self, move): '''Returns the corresponding rook move if a given move is a castling one.''' piece, start, end = [str(move.piece), str(move.source), str(move.target)] for castle in [self.castles, self.castles_reversed]: if castle.has_key((piece, start, end)): return castle[(piece, start, end)] return None def toBoardstring(self): return BoardString(str(self.position.fen)) def initialMove(self): '''Special game move to hold starting position.''' # FIXME: use iswhite start = GameMove( self._start_name, self.position.fen.full_move, self.position.fen.turn == 'w', None, self.fen, None, None, None, None ) return start
def test_from_notation(self): for notation_row, position_row in zip(self.notations, self.positions): for notation, position in zip(notation_row, position_row): self.assertEqual(position, Position.from_notation(notation))