Add classes and method documentation

This commit is contained in:
Yorick Barbanneau 2023-12-21 00:30:08 +01:00
parent d2ada8734f
commit f927aa1462
2 changed files with 169 additions and 10 deletions

View file

@ -1,4 +1,14 @@
"""
Base class for heuristic object
"""
class HeuristicEngine:
"""
Init method
@param logger: logging object (display verbose and debug messages)
@param options: hashtable contains options like board size or heuristic weight
"""
def __init__(self, logger, options):
self.logger = logger
self.options = options
@ -10,22 +20,52 @@ class HeuristicEngine:
def get():
raise NotImplementedError
"""
Score based heuristic class
"""
class ScoreHeuristicEngine(HeuristicEngine):
"""
Get score
@param board: reversi board object
@return score: int
"""
def get(self, board, player):
return board.heuristique(player)
"""
Weight based heuristic class
"""
class WeightHeuristicEngine(HeuristicEngine):
"""
Init method
@param logger: logging object (display verbose and debug messages)
@param options: hashtable contains options like board size or heuristic weight
"""
def __init__(self, logger, options):
super().__init__(logger, options)
self.weights = self._get_weight_array()
self.logger.debug("{}".format(self.show_weights()))
"""
Get score
@param board: reversi board object
@param player: int concerned player
@return int
"""
def get(self, board, player):
score = self.get_weight(board, player)
return score
"""
Get score based on weight based on a weight-table built on object creation
@param board: reversi board object
@param player: int concerned player
@return int
"""
def get_weight(self, board, player):
score = 0
size = self.options['size']
@ -41,7 +81,12 @@ class WeightHeuristicEngine(HeuristicEngine):
score -= self.weights[pos_x][pos_y]
w[pos_x][pos_y] = -self.weights[pos_x][pos_y]
return score
"""
Get weight array calculated with weight given with options
@param : none
@return 2D array
"""
def _get_weight_array(self):
size = self.options['size']
w = [[ 0 for _ in range(size)] for _ in range(size)]
@ -85,7 +130,11 @@ class WeightHeuristicEngine(HeuristicEngine):
return w
"""
Create a "displayable" array of value dor the calculated weight table
@input none
@return string
"""
def show_weights(self):
display = "\n |"
sep = "\n----"
@ -100,8 +149,16 @@ class WeightHeuristicEngine(HeuristicEngine):
display += "\n"
return display
"""
Full heuristic class
"""
class FullHeuristicEngine(WeightHeuristicEngine):
"""
Get score
@param board: reversi board object
@param player: int concerned player
@return int
"""
def get(self, board, player):
return self.get_weight(board, player) + board.heuristique(player)