def move(self, state): print("Robot's beurt(" + self.color + ")") # Voer minimax-algoritme uit m = Minimax(state) best_move, value = m.best_move(self.difficulty, state, self.color) return best_move
def move(self, state): print(f"{self.name}'s turn. {self.name} is {self.color}") # time.sleep(random.randrange(8, 17, 1) / 10.0) # return random.randint(0, 6) m = Minimax(state) best_move, value = m.best_move(state=state, depth=self.difficulty, alpha=-math.inf, beta=math.inf, maximizing_player=True) return best_move
""" Benchmarks for the minimax algorithm. We use two implementantions: a) Minimax (immutable flag) b) MinimaxMutable (mutable flag) a) uses immutable operations on the board, while b) uses make and unmake move and it mutates the argument board. """ import sys from common import TTT from minimax import Counter, Minimax, MinimaxMutable if __name__ == "__main__": board = TTT() Counter.reset() depth = 8 if len(sys.argv) > 1 and sys.argv[1] == "immutable": best_move = Minimax.best_move(board, depth) elif len(sys.argv) > 1 and sys.argv[1] == "mutable": best_move = MinimaxMutable.best_move(board, depth) else: print("no arguments given!", file=sys.stderr) sys.exit(1) #print("Best move: ", best_move) print(f"total terminal states: {Counter.total}")
class ApiTester: def __init__(self, loop, player_num): self._api_url = 'http://localhost:8081' self._game = Game() self._session = aiohttp.ClientSession() self._player_num = player_num self._loop = loop self._last_move = [] async def _prepare_player(self, name): async with self._session.post(f'{self._api_url}/game', params={'team_name': name}) as resp: res = (await resp.json())['data'] self._player_num = 1 if res['color'] == 'RED' else 2 self._player = {'color': res['color'], 'token': res['token']} self.minimax = Minimax(self._player_num) print("PLAYER_NUM", self._player_num) async def _make_move(self, move): json = {'move': move} headers = {'Authorization': f'Token {self._player["token"]}'} async with self._session.post(f'{self._api_url}/move', json=json, headers=headers) as resp: print(await resp.text()) resp = (await resp.json())['data'] logging.info(f'Made move {move}, response: {resp}') async def _get_game(self): async with self._session.get(f'{self._api_url}/game') as resp: return (await resp.json())['data'] async def _play_game(self): current_game_progress = await self._get_game() is_finished = current_game_progress['is_finished'] is_started = current_game_progress['is_started'] while is_started and not is_finished: logging.info(f"Move of {current_game_progress['whose_turn']}") if current_game_progress['last_move'] is not None and \ current_game_progress['last_move']['last_moves'] != self._last_move: last_move = current_game_progress['last_move']['last_moves'] logging.info(f"Move from server: {last_move}") moves = [] for m in last_move: if m not in self._last_move: moves.append(m) for move in moves: self._game.move(move) self._last_move = last_move if self._player['color'] == current_game_progress['whose_turn']: start = time.time() move = self.heuristic() print("TOTAL TIME FOR TURN: ", time.time() - start) logging.info(f"New move: {move}") await self._make_move(move) current_game_progress = await self._get_game() is_finished = current_game_progress['is_finished'] is_started = current_game_progress['is_started'] await asyncio.sleep(0.2) async def start(self): logging.info('API Tester initialized, test will start in 2 secs') await self._prepare_player(self._player_num) logging.info('Game started, players initialized') logging.info(f'Players: {self._player}') await asyncio.sleep(0.5) await self._play_game() logging.info('Game finished') last_game_progress = await self._get_game() logging.info(str(last_game_progress)) await self._session.close() def heuristic(self): return self.minimax.best_move(self._game, 5)
def move(self, current_game): minimax = Minimax(current_game, self.player_num) move = minimax.best_move() return move