Compare commits

..

No commits in common. "3d3f757f417bba93dc4f8501a0990b5300296d0e" and "e76cfdb75394b40640e7fffe03ebac759f4da3ac" have entirely different histories.

2 changed files with 40 additions and 47 deletions

View file

@ -20,7 +20,7 @@ class PlayerEngine:
self.options = options self.options = options
self.interrupt_search = False self.interrupt_search = False
self.logger.info("Init engine {}, options:{}".format( self.logger.info("Init engine {}, options:{}".format(
self._get_class_name(), self.__class__.__name__,
self.options self.options
)) ))
@ -30,8 +30,8 @@ class PlayerEngine:
""" """
def get_move(self, board): def get_move(self, board):
self.logger.info("engine: {} - player:{}".format( self.logger.info("engine: {} - player:{}".format(
self._get_class_name(), self.__class__.__name__,
self._get_player_name(self.player) self.get_player_name(self.player)
)) ))
""" """
@ -50,25 +50,10 @@ class PlayerEngine:
@param player: int @param player: int
@return: string @return: string
""" """
def _get_player_name(self, player): @staticmethod
return 'White (O)' if self.player == 2 else 'Black (X)' def get_player_name(player):
return 'White (O)' if 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 Random game engine
@ -171,12 +156,19 @@ class MinmaxPlayerEngine(PlayerEngine):
if v > value: if v > value:
value = v value = v
move = m move = m
self._show_better_move(move, value) self.logger.debug("\tfound a better move: {} (heuristic:{})".format(
move,
value
))
nodes += n nodes += n
leafs += l leafs += l
board.pop() board.pop()
self._show_stats_info(depth, nodes, leafs, value) self.logger.info("Tree statistics:\n\tnodes:{}\n\tleafs:{}\n\theuristic:{}".format(
nodes,
leafs,
value
))
return move, value return move, value
""" """
@ -250,9 +242,16 @@ class AlphabetaPlayerEngine(PlayerEngine):
if value >= alpha: if value >= alpha:
alpha = value alpha = value
move = m move = m
self._show_stats_info(move, alpha) self.logger.debug("\t-> found a better move: {} | heuristic:{})".format(
move,
alpha
))
self._show_stats_info(depth, nodes, leafs, value) self.logger.info("Tree statistics:\n\tnodes:{}\n\tleafs:{}\n\theuristic:{}".format(
nodes,
leafs,
alpha
))
return move, alpha return move, alpha
""" """
@ -330,15 +329,12 @@ class MinmaxDeepeningPlayerEngine(MinmaxPlayerEngine):
# return the current move onli if heuristic is better than previous # return the current move onli if heuristic is better than previous
# iteration # iteration
if current_heuristic > heuristic: if current_heuristic > heuristic:
heuristic = current_heuristic
move = current_move move = current_move
depth = depth + 1 depth = depth + 1
self.logger.debug("id_minmax - depth reached: {} | max depth : {}".format(
self.logger.info("Iterative Minmax - depth: {}/{} | heuristic: {}".format(
depth - 1, depth - 1,
max_depth, max_depth
heuristic
)) ))
return move return move
@ -378,14 +374,12 @@ class AlphaBetaDeepeningPlayerEngine(AlphabetaPlayerEngine):
# return the current move only if heuristic is better than previous # return the current move only if heuristic is better than previous
# iteration can be possible id iteration is stopped by timer # iteration can be possible id iteration is stopped by timer
if current_heuristic > heuristic: if current_heuristic > heuristic:
heuristic = current_heuristic
move = current_move move = current_move
depth = depth + 1 depth = depth + 1
self.logger.info("Iterative Alphabeta - depth: {}/{} | heuristic: {}".format( self.logger.debug("id_minmax - depth reached: {} | max depth : {}".format(
depth - 1, depth - 1,
max_depth, max_depth
heuristic
)) ))
return move return move

View file

@ -182,11 +182,10 @@ if __name__ == '__main__':
move = bplayer.get_move(game) move = bplayer.get_move(game)
else: else:
move = wplayer.get_move(game) move = wplayer.get_move(game)
print("Player {} move: {},{}".format( print("Player {} move: {}".format(
"Black (X)" if move[0] == 2 else "White (O)", game._nextPlayer,
move[1], move
move[2]
)) ))
game.push(move) game.push(move)
print("Game end - score black:{}, white:{}\n".format(game._nbBLACK, game._nbWHITE)) print("Game end - score black:{}, white:{}".format(game.heuristique(1), game.heuristique(2)))
print(game.show_board()) print(game.__str__)