def choose_action(self, board: Board): while True: print(board.pretty()) row = input('Row: ') col = input('Col: ') try: board.pick((int(row) - 1, int(col) - 1)) break except ValueError: print(f'Bad action: {row, col}. Try again.') pass
def play(self): board = Board(*self.size) random.shuffle(self.players) players = itertools.cycle(self.players) current_player = None while not board.ended: current_player = next(players) current_player.choose_action(board) current_player.end_round(0) next(players).end_round(1)
def test_board_created_with_all_available_actions(self): rows = 3 cols = 4 self.assertEqual(list(itertools.product(range(rows), range(cols))), Board(rows, cols).available_actions())
def test_pick_shrinks_list_when_whole_row_is_picked(self): board = Board(4, 1) self.assertEqual(4, len(board._grid)) board.pick((2, 0)) self.assertEqual(2, len(board._grid))
def test_pick_same_cell_throws(self): board = Board(2, 2) board.pick((1, 1)) with self.assertRaisesRegex(ValueError, 'Invalid action'): board.pick((1, 1))
def test_pick_removes_cell(self): board = Board(2, 2) self.assertEqual(4, len(board.available_actions())) board.pick((1, 1)) self.assertEqual(3, len(board.available_actions())) board.pick((1, 0)) self.assertEqual(2, len(board.available_actions())) board.pick((0, 1)) self.assertEqual(1, len(board.available_actions()))
def choose_action(self, board: Board): action = self.agent.choose_action( board.available_actions(), lambda x: self._copy_board_and_get_new_hash(x, board) ) board.pick(action)
def _copy_board_and_get_new_hash(action, board: Board): new_board = board.copy() new_board.pick(action) return new_board.hash