Compare commits
5 commits
e76cfdb753
...
3d3f757f41
Author | SHA1 | Date | |
---|---|---|---|
3d3f757f41 | |||
5cfd2ce404 | |||
5f69c5ed9f | |||
d480887a1d | |||
8c825506f5 |
2 changed files with 47 additions and 40 deletions
|
@ -20,20 +20,20 @@ class PlayerEngine:
|
|||
self.options = options
|
||||
self.interrupt_search = False
|
||||
self.logger.info("Init engine {}, options:{}".format(
|
||||
self.__class__.__name__,
|
||||
self._get_class_name(),
|
||||
self.options
|
||||
))
|
||||
|
||||
|
||||
"""
|
||||
get move
|
||||
@param board: Board
|
||||
"""
|
||||
def get_move(self, board):
|
||||
self.logger.info("engine: {} - player:{}".format(
|
||||
self.__class__.__name__,
|
||||
self.get_player_name(self.player)
|
||||
self._get_class_name(),
|
||||
self._get_player_name(self.player)
|
||||
))
|
||||
|
||||
|
||||
"""
|
||||
Get possibles player move an apply a Random.shuffle on it (if needed)
|
||||
@param none
|
||||
|
@ -44,16 +44,31 @@ class PlayerEngine:
|
|||
if self.options['randomize_moves'] is True:
|
||||
random.shuffle(moves)
|
||||
return moves
|
||||
|
||||
|
||||
"""
|
||||
Get player name based on his number
|
||||
@param player: int
|
||||
@return: string
|
||||
"""
|
||||
@staticmethod
|
||||
def get_player_name(player):
|
||||
return 'White (O)' if player == 2 else 'Black (X)'
|
||||
def _get_player_name(self, player):
|
||||
return 'White (O)' if self.player == 2 else 'Black (X)'
|
||||
|
||||
def _show_stats_info(self, depth, nodes, leafs, heuristic):
|
||||
self.logger.info(" -> stats: depth:{:.>2} | node:{:.>6} | leafs:{:.>6} | heuristic:{:.>4}".format(
|
||||
depth,
|
||||
nodes,
|
||||
leafs,
|
||||
heuristic
|
||||
))
|
||||
|
||||
def _show_better_move(self, move, heuristic):
|
||||
self.logger.debug(" -> Found a better move: {},{} | heuristic:{}".format(
|
||||
move[1],move[2],
|
||||
heuristic
|
||||
))
|
||||
|
||||
def _get_class_name(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
"""
|
||||
Random game engine
|
||||
|
@ -93,7 +108,7 @@ class HumanPlayerEngine(PlayerEngine):
|
|||
move = self.validate_input(user_input, board)
|
||||
return move
|
||||
|
||||
|
||||
|
||||
"""
|
||||
Validate user input an verify than the move is possible
|
||||
@param input: string
|
||||
|
@ -156,19 +171,12 @@ class MinmaxPlayerEngine(PlayerEngine):
|
|||
if v > value:
|
||||
value = v
|
||||
move = m
|
||||
self.logger.debug("\tfound a better move: {} (heuristic:{})".format(
|
||||
move,
|
||||
value
|
||||
))
|
||||
self._show_better_move(move, value)
|
||||
nodes += n
|
||||
leafs += l
|
||||
board.pop()
|
||||
|
||||
self.logger.info("Tree statistics:\n\tnodes:{}\n\tleafs:{}\n\theuristic:{}".format(
|
||||
nodes,
|
||||
leafs,
|
||||
value
|
||||
))
|
||||
self._show_stats_info(depth, nodes, leafs, value)
|
||||
return move, value
|
||||
|
||||
"""
|
||||
|
@ -185,7 +193,7 @@ class MinmaxPlayerEngine(PlayerEngine):
|
|||
if depth == 0 or board.is_game_over() or self.interrupt_search:
|
||||
leafs +=1
|
||||
return self.heuristic.get(board, self.player), nodes, leafs
|
||||
|
||||
|
||||
if friend_move:
|
||||
value = -math.inf
|
||||
moves = self.get_player_moves(board)
|
||||
|
@ -242,16 +250,9 @@ class AlphabetaPlayerEngine(PlayerEngine):
|
|||
if value >= alpha:
|
||||
alpha = value
|
||||
move = m
|
||||
self.logger.debug("\t-> found a better move: {} | heuristic:{})".format(
|
||||
move,
|
||||
alpha
|
||||
))
|
||||
self._show_stats_info(move, alpha)
|
||||
|
||||
self.logger.info("Tree statistics:\n\tnodes:{}\n\tleafs:{}\n\theuristic:{}".format(
|
||||
nodes,
|
||||
leafs,
|
||||
alpha
|
||||
))
|
||||
self._show_stats_info(depth, nodes, leafs, value)
|
||||
return move, alpha
|
||||
|
||||
"""
|
||||
|
@ -305,7 +306,7 @@ class MinmaxDeepeningPlayerEngine(MinmaxPlayerEngine):
|
|||
def get_move(self, board):
|
||||
super().get_move(board)
|
||||
self.interrupt_search = False
|
||||
|
||||
|
||||
# Get an alarm signal to stop iterations
|
||||
signal.signal(signal.SIGALRM, self.alarm_handler)
|
||||
signal.alarm(self.options['time_limit'])
|
||||
|
@ -329,12 +330,15 @@ class MinmaxDeepeningPlayerEngine(MinmaxPlayerEngine):
|
|||
# return the current move onli if heuristic is better than previous
|
||||
# iteration
|
||||
if current_heuristic > heuristic:
|
||||
heuristic = current_heuristic
|
||||
move = current_move
|
||||
|
||||
depth = depth + 1
|
||||
self.logger.debug("id_minmax - depth reached: {} | max depth : {}".format(
|
||||
|
||||
self.logger.info("Iterative Minmax - depth: {}/{} | heuristic: {}".format(
|
||||
depth - 1,
|
||||
max_depth
|
||||
max_depth,
|
||||
heuristic
|
||||
))
|
||||
return move
|
||||
|
||||
|
@ -353,7 +357,7 @@ class AlphaBetaDeepeningPlayerEngine(AlphabetaPlayerEngine):
|
|||
"""
|
||||
def get_move(self, board):
|
||||
self.interrupt_search = False
|
||||
|
||||
|
||||
# Get an alarm signal to stop iterations
|
||||
signal.signal(signal.SIGALRM, self.alarm_handler)
|
||||
signal.alarm(self.options['time_limit'])
|
||||
|
@ -374,12 +378,14 @@ class AlphaBetaDeepeningPlayerEngine(AlphabetaPlayerEngine):
|
|||
# return the current move only if heuristic is better than previous
|
||||
# iteration can be possible id iteration is stopped by timer
|
||||
if current_heuristic > heuristic:
|
||||
heuristic = current_heuristic
|
||||
move = current_move
|
||||
depth = depth + 1
|
||||
|
||||
self.logger.debug("id_minmax - depth reached: {} | max depth : {}".format(
|
||||
self.logger.info("Iterative Alphabeta - depth: {}/{} | heuristic: {}".format(
|
||||
depth - 1,
|
||||
max_depth
|
||||
max_depth,
|
||||
heuristic
|
||||
))
|
||||
return move
|
||||
|
||||
|
|
11
src/game.py
11
src/game.py
|
@ -182,10 +182,11 @@ if __name__ == '__main__':
|
|||
move = bplayer.get_move(game)
|
||||
else:
|
||||
move = wplayer.get_move(game)
|
||||
print("Player {} move: {}".format(
|
||||
game._nextPlayer,
|
||||
move
|
||||
print("Player {} move: {},{}".format(
|
||||
"Black (X)" if move[0] == 2 else "White (O)",
|
||||
move[1],
|
||||
move[2]
|
||||
))
|
||||
game.push(move)
|
||||
print("Game end - score black:{}, white:{}".format(game.heuristique(1), game.heuristique(2)))
|
||||
print(game.__str__)
|
||||
print("Game end - score black:{}, white:{}\n".format(game._nbBLACK, game._nbWHITE))
|
||||
print(game.show_board())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue