def on_draw(self): self.clear() self.__gui.draw_board(self.__game.get_board()) if self.__current_pos is not None: possible_move = self.__game.get_board().get_possible_move(Position(*self.__current_pos)) if possible_move is not None: self.__gui.draw_possible_move(possible_move)
def show_possible_move(self, possible_move): str = '' for row in range(self.height): for piece in range(self.width): if Position(row, piece) in possible_move: str += 'x' str += self.pieces[row][piece].__str__ + ', ' str += '\n' return str
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int): i, j = self.__gui.get_cell_index_from_position(x, y) if self.__game.get_board().is_in_bound(Position(i, j)): if self.__current_pos is None: piece_color = self.__game.get_board().get_at_xy(i, j).get_color() current_color = Color.BLACK if self.__game.get_turn() % 2 == 0 else Color.WHITE if piece_color == current_color: self.__current_pos = i, j else: if self.__game.get_board().is_legal_move(Position(self.__current_pos[0], self.__current_pos[1]), Position(i, j)): if self.__game.get_board().get_at_xy(*self.__current_pos).get_piece_type() == PieceType.PAWN: self.__game.get_board().get_at_xy(*self.__current_pos).move() self.__game.get_board().move_piece(Position(self.__current_pos[0], self.__current_pos[1]), Position(i, j)) self.__game.increment_turn() self.__current_pos = None
def build_board(length, width, content): board = Board(length, width) current_line = 0 current_row = 0 for line in content: pieces = line.split(',') for piece in pieces: color = Color.get_color(piece[0]) # TODO hardcoded if len(piece) == 3: board.add_piece(Position(current_line, current_row), PieceFactory.build_used_pawn(color)) else: board.add_piece(Position(current_line, current_row), PieceFactory.build_piece(piece[1], color)) current_row += 1 current_row = 0 current_line += 1 return board
def test_get_legal_move(self): board = BoardFactory.build_board(8, 8, BoardFactory.DEFAULT_BOARD) assert board.get_possible_move(Position( 0, 0)) == [], 'Rook no move failed' # TODO test for special pawn case assert sorted(board.get_possible_move(Position(1, 1))) == sorted( [Position(2, 1), Position(3, 1)]), 'pawn first move failed' assert sorted(board.get_possible_move(Position(6, 1))) == sorted( [Position(5, 1), Position(4, 1)]), 'opposite pawn first move failed' assert sorted(board.get_possible_move(Position(0, 1))) == sorted( [Position(2, 0), Position(2, 2)]), 'Knight over moved failed' board.move_piece(Position(0, 0), Position(4, 4)) assert sorted(board.get_possible_move(Position(4, 4))) == sorted([ Position(4, 0), Position(4, 1), Position(4, 2), Position(4, 3), Position(4, 5), Position(4, 6), Position(4, 7), Position(2, 4), Position(3, 4), Position(5, 4), Position(6, 4) ]), "Rook all side move failed" board.move_piece(Position(0, 1), Position(4, 4)) board.move_piece(Position(1, 3), Position(2, 3)) assert sorted(board.get_possible_move(Position(4, 4))) == sorted([ Position(2, 5), Position(3, 6), Position(5, 6), Position(6, 5), Position(6, 3), Position(5, 2), Position(3, 2) ]), 'Knight all side move failed' board.move_piece(Position(2, 3), Position(1, 3)) board.move_piece(Position(0, 2), Position(4, 4)) assert sorted(board.get_possible_move(Position(4, 4))) == sorted([ Position(2, 2), Position(3, 3), Position(5, 5), Position(6, 6), Position(5, 3), Position(2, 6), Position(6, 2), Position(3, 5) ]), 'Bishop all side move failed' board.move_piece(Position(0, 3), Position(4, 4)) assert sorted(board.get_possible_move(Position(4, 4))) == sorted([ Position(4, 0), Position(4, 1), Position(4, 2), Position(4, 3), Position(4, 5), Position(4, 6), Position(4, 7), Position(2, 4), Position(3, 4), Position(5, 4), Position(6, 4), Position(2, 2), Position(3, 3), Position(5, 5), Position(6, 6), Position(2, 6), Position(3, 5), Position(5, 3), Position(6, 2) ]), 'Queen all side move failed' board.move_piece(Position(0, 4), Position(4, 4)) assert sorted(board.get_possible_move(Position(4, 4))) == sorted([ Position(3, 3), Position(4, 3), Position(5, 3), Position(5, 4), Position(5, 5), Position(4, 5), Position(3, 5), Position(3, 4) ]), 'King all side move failed' board.move_piece(Position(1, 1), Position(5, 1)) assert sorted(board.get_possible_move(Position(5, 1))) == sorted( [Position(6, 1), Position(6, 2), Position(6, 0)]), 'Pawn capture failed' board.move_piece(Position(6, 5), Position(2, 5)) assert sorted(board.get_possible_move(Position(2, 5))) == sorted( [Position(1, 5), Position(1, 4), Position(1, 6)]), 'Pawn capture failed' assert sorted(board.get_possible_move(Position(1,0))) == sorted([Position(2,0), Position(3,0)]), \ 'left edge pawn failed' assert sorted(board.get_possible_move(Position(1, 7))) == sorted( [Position(2, 7), Position(3, 7)]), 'right edge pawn failed'
for piece in range(self.width): if Position(row, piece) in possible_move: str += 'x' str += self.pieces[row][piece].__str__ + ', ' str += '\n' return str def get_size(self): return self.width, self.height def __str__(self): str = ' ' for row in range(self.height): for piece in range(self.width): str += self.pieces[row][piece].__str__ + ', ' str += '\n' return str if __name__ == '__main__': board = Board(8, 8) test = board.get_possible_move(Position(1, 1)) print(test) for position in test: print(position) print(board.show_possible_move(test), '\n')
def test_lt(self): position1 = Position(0, 0) position2 = Position(1, 0) position3 = Position(0, 1) position4 = Position(1, 1) assert not position1.__lt__(position1) assert position1.__lt__(position2) assert position1.__lt__(position3) assert position1.__lt__(position4) assert not position2.__lt__(position1) assert not position2.__lt__(position2) assert not position2.__lt__(position3) assert position2.__lt__(position4) assert not position3.__lt__(position1) assert position3.__lt__(position2) assert not position3.__lt__(position3) assert position3.__lt__(position4) assert not position4.__lt__(position1) assert not position4.__lt__(position2) assert not position4.__lt__(position3) assert not position4.__lt__(position4)