class Play: def __init__(self): self.board = Board() self._stones = None self._moves = None self._logger = NullLogger() def set_stones(self, stones): self._stones = stones def set_moves(self, moves): if type(moves) == list: self._moves = iter(moves) else: self._moves = moves def set_logger(self, logger): self._logger = logger def play(self): if self._stones: self.board.reload(self._stones) while not self.board.is_end(): player = self.board.next_player() next_move = next(self._moves) self._logger.on_sow(self.board.holes, player, next_move) self.board.sow(player, next_move) self._logger.on_end(self.board.get_score(), self.board.winner())
def test_no_more_moves(): b = Board() b.board = [11, 0, 0, 0, 0, 0, 1, 10, 1, 2, 3, 4, 5, 6] assert b.no_more_moves() == False b = Board() b.board = [11, 0, 0, 0, 0, 0, 0, 10, 1, 2, 3, 4, 5, 6] assert b.no_more_moves() == True b = Board() b.board = [11, 0, 0, 0, 0, 0, 1, 10, 0, 0, 0, 0, 0, 0] assert b.no_more_moves() == True
def test_player_points(): b = Board() b.board = [11, 0, 0, 0, 0, 0, 1, 10, 1, 2, 3, 4, 5, 6] assert b.player_points == 10 assert b.opponent_points == 11 b = Board() b.board = [11, 0, 0, 0, 0, 0, 0, 10, 1, 2, 3, 4, 5, 6] assert b.player_points == 10 assert b.opponent_points == 1 + 2 + 3 + 4 + 5 + 6 + 11 b = Board() b.board = [11, 1, 2, 3, 4, 5, 6, 10, 0, 0, 0, 0, 0, 0] assert b.player_points == 1 + 2 + 3 + 4 + 5 + 6 + 10 assert b.opponent_points == 11
def test_possible_player_moves(): b = Board() b.board = [0, 4, 0, 13, 5, 5, 5, 0, 4, 4, 4, 4, 4, 4] assert list(b.possible_player_moves()) == [0, 2, 3, 4, 5]
def test_move(): b = Board() cont = b.make_player_move(0) assert b.board == [0, 0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4] assert cont == False b = Board() cont = b.make_player_move(1) assert b.board == [0, 4, 0, 5, 5, 5, 5, 0, 4, 4, 4, 4, 4, 4] assert cont == False b = Board() cont = b.make_player_move(2) assert b.board == [0, 4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4] assert cont == True b = Board() b.board = [0, 4, 4, 11, 5, 5, 5, 0, 4, 4, 4, 4, 4, 4] cont = b.make_player_move(2) assert b.board == [0, 5, 4, 0, 6, 6, 6, 1, 5, 5, 5, 5, 5, 5] assert cont == False b = Board() b.board = [0, 4, 4, 13, 5, 5, 5, 0, 4, 4, 4, 4, 4, 4] cont = b.make_player_move(2) assert b.board == [0, 5, 5, 0, 6, 6, 6, 7, 5, 5, 5, 0, 5, 5] assert cont == False
def test_heurestic(): b = Board() b.board = [11, 0, 0, 0, 0, 0, 1, 10, 1, 2, 3, 4, 5, 6] assert b.get_heurestic_score() == -1 assert b.get_opponent_board().get_heurestic_score() == -1
def test_get_oponent_board(): b = Board() b.board = [0, 5, 4, 0, 6, 6, 6, 1, 5, 5, 5, 5, 5, 5] assert b.get_opponent_board().board == [ 1, 5, 5, 5, 5, 5, 5, 0, 5, 4, 0, 6, 6, 6 ]
def test_get_moves(): b = Board() moves = [] b.get_player_moves(0, [], moves) assert [x[0] for x in moves] == [[0]] b = Board() moves = [] b.get_player_moves(1, [], moves) assert [x[0] for x in moves] == [[1]] b = Board() moves = [] b.get_player_moves(2, [], moves) assert set((tuple(x[0]) for x in moves)) == {(2, 0), (2, 1), (2, 3), (2, 4), (2, 5)} b = Board() b.board = [0, 0, 0, 0, 0, 2, 1, 0, 4, 4, 4, 4, 4, 4] moves = [] b.get_player_moves(5, [], moves) assert set((tuple(x[0]) for x in moves)) == {(5, 4, 5)} b = Board() b.board = [0, 1, 0, 0, 0, 2, 1, 0, 4, 4, 4, 4, 4, 4] moves = [] b.get_player_moves(5, [], moves) assert set((tuple(x[0]) for x in moves)) == {(5, 4, 5, 0), (5, 0), (5, 4, 0)}
def __init__(self): self.board = Board() self._stones = None self._moves = None self._logger = NullLogger()
def compete(self, brain1: Brain, brain2: Brain): board = Board() brain1.set_board(board) brain2.set_board(board) brain1.set_player_num(1) brain2.set_player_num(2) if self.verbosity > 3: print("{} is playing against {}".format(brain1.getName(), brain2.getName())) winner = None full_game_played = False move_count = 0 while winner == None: (move, confidence) = brain1.predict() if (self.verbosity > 4): print("\n\nP1 move is {} with {}".format(move, confidence)) if not move in board.possible_player_moves(): winner = 2 continue has_move = board.make_player_move(move, 1) if (self.verbosity > 4): board.print() move_count += 1 (move, confidence) = brain2.predict() if (self.verbosity > 4): print("\n\nP2 move is {} with {}".format(move, confidence)) if not move in board.get_opponent_board().possible_player_moves(): winner = 1 continue has_move = board.make_player_move(move, 2) if (self.verbosity > 4): board.print() move_count += 1 if winner == None: full_game_played = True self.full_game_list.append(1) ending = " with a score of {} to {} after a FULL GAME\n FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME FULL GAME".format( board.player_points, board.opponent_points) if board.player_points > board.opponent_points: winner = 1 else: winner = 2 else: self.full_game_list.append(0) ending = "by disqualification after {} moves. Score was {} to {}".format( move_count, board.player_points, board.opponent_points) self.scores.append(board.player_points) self.scores.append(board.opponent_points) self.rounds.append(move_count / 2) if winner == 1: name = brain1.getName() brain1.record_win() brain2.record_loss() else: name = brain2.getName() brain2.record_win() brain1.record_loss() if self.verbosity > 2: print("{} wins {}".format(name, ending)) return winner
def test_get_oponent_board(): b = Board() b.board = [0, 5, 4, 0, 6, 6, 6, 1, 5, 5, 5, 5, 5, 5] assert b.get_opponent_board().board == [1, 5, 5, 5, 5, 5, 5, 0, 5, 4, 0, 6, 6, 6]