示例#1
0
文件: player.py 项目: 80085/chomp
 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
示例#2
0
文件: game.py 项目: 80085/chomp
 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)
示例#3
0
文件: test_board.py 项目: 80085/chomp
 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())
示例#4
0
文件: test_board.py 项目: 80085/chomp
 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))
示例#5
0
文件: test_board.py 项目: 80085/chomp
 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))
示例#6
0
文件: test_board.py 项目: 80085/chomp
 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()))
示例#7
0
文件: player.py 项目: 80085/chomp
 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)
示例#8
0
文件: player.py 项目: 80085/chomp
 def _copy_board_and_get_new_hash(action, board: Board):
     new_board = board.copy()
     new_board.pick(action)
     return new_board.hash