def test_move_already_taken(self): new_board = Board('x', 'o') result = new_board.move(0, 0, 'x') assert not result with pytest.raises(IllegalMoveException) as e: new_board.move(0, 0, 'o') assert e.value.error == 'This cell is already taken.'
def test_remove_piece(self): board = Board() board.set_piece(Position(1, 2), Pieces.BLACK_BISHOP) assert board.get_piece(Position(1, 2)) == Pieces.BLACK_BISHOP board.remove_piece(Position(1, 2)) assert board.get_piece(Position(1, 2)) is None
def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode(constants.WINSIZE) pygame.display.set_caption(constants.TITLE) # statuses running, playing, is_mouse_down, current_cell = True, False, False, None screen.fill(constants.BLACK) board = Board() board.run(screen) pygame.display.update() # main game loop while running: ev = pygame.event.get() for event in ev: print(event) if event.type == QUIT: running = False break elif event.type == KEYUP and event.key == K_ESCAPE: # reset board.reset(screen) elif event.type == KEYDOWN and event.key == K_SPACE: playing = not playing elif event.type == pygame.MOUSEBUTTONDOWN and event.button == constants.MOUSELEFT: # Draw cell event is_mouse_down = True pos = pygame.mouse.get_pos() current_cell = select_cell(board.get_cell(pos), screen) elif event.type == pygame.MOUSEMOTION and is_mouse_down: # Draw cell event on mousedown pos = pygame.mouse.get_pos() cell = board.get_cell(pos) if cell == current_cell: continue current_cell = select_cell(cell, screen) elif event.type == pygame.MOUSEBUTTONUP and event.button == constants.MOUSELEFT: # Stop cell drawing when mouse up is_mouse_down = False if playing: # Resolve Game of life rules actions = list(gen_cells_action(board)) for cell, action in actions: getattr(cell, action)(screen) pygame.display.update() clock.tick(40)
def test_set_piece(self): board = Board() board.set_piece(Position(4, 4), Pieces.WHITE_ROOK) # TODO(amirov-m): What is the right order for actual and expected? assert board.get_piece(Position(4, 4)) == Pieces.WHITE_ROOK board.set_piece(Position(7, 7), Pieces.BLACK_PAWN) assert board.get_piece(Position(7, 7)) == Pieces.BLACK_PAWN
def test_not_complete(self): new_board = Board('x', 'o') new_board.move(0, 1, 'x') new_board.move(0, 2, 'o') has_winner, winner = new_board.is_complete() assert has_winner is False assert new_board.turn assert winner == ''
def test_collect_money_on_go(): players = [Player('Mack')] board = Board(board=SQUARES) game = MonopolyGame(board, dice_function=lambda: 1) game.add_players(players) go_square = game.board._squares[0] assert players[0].position == game.board.start_square balance = players[0].balance game.take_action(players[0]) assert balance + 200 == players[0].balance
def test_board_creation(self): new_board = Board('x', 'o') expected_string = ('| | | |\n' '|--+--+--|\n' '| | | |\n' '|--+--+--|\n' '| | | |\n' '|--+--+--|\n' '<@x> \'s turn is next') assert new_board.turn == new_board.x_player assert new_board.to_string() == expected_string
def test_o_win(self): new_board = Board('x', 'o') new_board.move(0, 1, 'x') new_board.move(0, 2, 'o') new_board.move(2, 1, 'x') new_board.move(1, 2, 'o') new_board.move(0, 0, 'x') new_board.move(2, 2, 'o') has_winner, winner = new_board.is_complete() assert has_winner is True assert not new_board.turn assert winner == 'o'
def test_get_positions_for_piece(self): board = Board() board.set_piece(Position(1, 0), Pieces.WHITE_KNIGHT) board.set_piece(Position(4, 0), Pieces.WHITE_KNIGHT) board.set_piece(Position(0, 7), Pieces.WHITE_KNIGHT) board.set_piece(Position(7, 7), Pieces.WHITE_KNIGHT) piece_positions = board.get_positions_for_piece(Pieces.WHITE_KNIGHT) assert set(piece_positions) == { Position(1, 0), Position(4, 0), Position(0, 7), Position(7, 7), }
def test_is_mate(self): """Test of is_check() method.""" assert not GameLogic.is_mate(self.game) # Create dummy board board = Board() board.set_piece(Position(0, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(1, 1), Piece(PieceType.PAWN, Colour.WHITE)) board.set_piece(Position(0, 0), Piece(PieceType.KING, Colour.WHITE)) board.set_piece(Position(2, 0), Piece(PieceType.ROOK, Colour.BLACK)) history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] game = Game(board, Colour.WHITE, history_moves) assert GameLogic.is_mate(game)
def test_x_win(self): new_board = Board('x', 'o') result = new_board.move(0, 1, 'x') assert not result result = new_board.move(0, 2, 'o') assert not result result = new_board.move(1, 1, 'x') assert not result result = new_board.move(2, 2, 'o') assert not result result = new_board.move(2, 1, 'x') assert not result has_winner, winner = new_board.is_complete() assert has_winner is True assert not new_board.turn assert winner == 'x'
def test_move_and_buy_property(): players = [Player('Mack')] board = Board(board=SQUARES) game = MonopolyGame(board, dice_function=lambda: 1) game.add_players(players) property_square = game.board._squares[1] assert players[0].position == game.board.start_square assert property_square.property.owner == game.board.bank game.move_player(players[0]) game.take_action(players[0]) assert players[0].position == property_square assert property_square.property.owner == players[0]
def create_new_game(self, channel_id, command_text, o_player): if self.ongoing_games.get(channel_id): return create_response(instruction.get_ongoing_game_exists_text(), public=False) try: x_player = get_user_from_start_command(command_text) except InvalidCommandUseException: return create_response( instruction.get_invalid_command_text(START_COMMAND), public=False) new_board = Board(x_player, o_player) self.ongoing_games[channel_id] = new_board response = '<@' + x_player + '> is X.\n' + new_board.to_string() return create_response(response)
def test_tie_game(self): new_board = Board('x', 'o') new_board.move(0, 0, 'x') new_board.move(0, 2, 'o') new_board.move(0, 1, 'x') new_board.move(1, 0, 'o') new_board.move(1, 1, 'x') new_board.move(2, 1, 'o') new_board.move(1, 2, 'x') new_board.move(2, 2, 'o') new_board.move(2, 0, 'x') is_complete, winner = new_board.is_complete() assert is_complete is True assert not new_board.turn assert winner == ''
def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 1), Pieces.WHITE_BISHOP) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(4, 3), Pieces.BLACK_ROOK) self.board.set_piece(Position(2, 2), Pieces.BLACK_PAWN) # Create game self.game = Game(self.board, Colour.WHITE, [Move(Position(3, 6), Position(3, 4))])
def test_move(self): new_board = Board('x', 'o') result = new_board.move(0, 1, 'x') assert new_board.turn == new_board.o_player assert not result result = new_board.move(0, 2, 'o') assert new_board.turn == new_board.x_player assert not result expected_string = ('| | X | O |\n' '|--+--+--|\n' '| | | |\n' '|--+--+--|\n' '| | | |\n' '|--+--+--|\n' '<@x> \'s turn is next') assert new_board.turn == new_board.x_player assert new_board.to_string() == expected_string
def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 3), Pieces.WHITE_PAWN) self.board.set_piece(Position(3, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(5, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(0, 6), Pieces.WHITE_PAWN) self.board.set_piece(Position(7, 7), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 4), Pieces.WHITE_KING) self.board.set_piece(Position(6, 6), Pieces.WHITE_BISHOP) self.board.set_piece(Position(3, 7), Pieces.WHITE_ROOK) self.board.set_piece(Position(5, 5), Pieces.WHITE_QUEEN) self.board.set_piece(Position(7, 6), Pieces.WHITE_KNIGHT) self.board.set_piece(Position(4, 5), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 7), Pieces.BLACK_ROOK)
def setUp(self) -> None: """Create dummy game.""" # Create dummy board self.board = Board() self.board.set_piece(Position(4, 0), Pieces.WHITE_KING) self.board.set_piece(Position(0, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(0, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(7, 0), Pieces.WHITE_ROOK) self.board.set_piece(Position(7, 1), Pieces.WHITE_PAWN) self.board.set_piece(Position(4, 4), Pieces.WHITE_PAWN) self.board.set_piece(Position(2, 7), Pieces.BLACK_ROOK) self.board.set_piece(Position(3, 4), Pieces.BLACK_PAWN) self.board.set_piece(Position(5, 4), Pieces.BLACK_PAWN) # Create game history_moves = [ Move(Position(4, 3), Position(4, 4)), Move(Position(3, 6), Position(3, 4)), ] self.game = Game(self.board, Colour.WHITE, history_moves)
def test_remove_piece_empty_cell(self): board = Board() board.remove_piece(Position(3, 4)) assert board.get_piece(Position(3, 4)) is None
def test_move_invalid_coordinate(self): new_board = Board('x', 'o') with pytest.raises(IllegalMoveException) as e: new_board.move(0, 10, 'x') assert e.value.error == 'Invalid coordinates.'
def main() -> int: core.init() parser = argparse.ArgumentParser() parser.add_argument( "-l", "--level", help="AI player Level. Default is 0 (Easy). Higher is harder)", default=cfg.LEVEL, type=int) parser.add_argument('-d', '--debug', help="Debug mode", action='store_true') parser.add_argument('-C', '--cache', help="Enable persistent memoize cache", action='store_true') options = parser.parse_args() cfg.LEVEL = options.level cfg.__DEBUG__ = options.debug cfg.CACHE_ENABLED = options.cache log('Quoridor AI game, (C) 2009 by Jose Rodriguez (a.k.a. Boriel)') log('This program is Free') log('Initializing system...') pygame.init() clock = pygame.time.Clock() pygame.display.set_mode((800, 600)) pygame.display.set_caption(cfg.GAME_TITLE) screen = pygame.display.get_surface() screen.fill(Color(255, 255, 255)) board = core.BOARD = Board(screen) board.draw() log('System initialized OK') if cfg.CACHE_ENABLED: if not os.path.exists(cfg.CACHE_DIR): log('Cache directory {} not found. Creating it...'.format( cfg.CACHE_DIR)) os.makedirs(cfg.CACHE_DIR, exist_ok=True) if not os.path.isdir(cfg.CACHE_DIR): log( 'Could not create cache directory {}. Caching disabled'.format( cfg.CACHE_DIR), LogLevel.ERROR) cfg.CACHE_ENABLED = False cont = True while cont: clock.tick(cfg.FRAMERATE) pygame.display.flip() if not board.computing and not board.finished: if board.current_player.AI: board.computing = True thread = threading.Thread(target=board.computer_move) thread.start() cont = dispatch(pygame.event.get(), board) del board.rows pygame.quit() if cfg.NETWORK_ENABLED: board.server.terminate() if cfg.CACHE_ENABLED: for pawn in board.pawns: if pawn.AI is not None: pawn.AI.flush_cache() log('Memoized nodes: %i' % core.MEMOIZED_NODES) log('Memoized nodes hits: %i' % core.MEMOIZED_NODES_HITS) for pawn in board.pawns: log('Memoized distances for [%i]: %i' % (pawn.id, pawn.distances.MEMO_COUNT)) log('Memoized distances hits for [%i]: %i' % (pawn.id, pawn.distances.MEMO_HITS)) log('Exiting. Bye!') return 0
def __init__(self, sizeOfBoard, max_number_of_iteration): self.__ant = Ant() self.__iter_counter = 0 self.__max_iter_number = max_number_of_iteration self.__board = Board(sizeOfBoard * sizeOfBoard)
def test_move_wrong_turn(self): new_board = Board('x', 'o') with pytest.raises(IllegalMoveException) as e: new_board.move(0, 1, 'o') assert e.value.error == 'It is not your turn.'
def test_set_piece_rewrite_piece(self): board = Board() board.set_piece(Position(1, 3), Pieces.WHITE_QUEEN) board.set_piece(Position(1, 3), Pieces.BLACK_KING) assert board.get_piece(Position(1, 3)) == Pieces.BLACK_KING