Rework Heuristics
This commit is contained in:
parent
437f05c8b2
commit
9a4febaaf4
1 changed files with 50 additions and 12 deletions
|
@ -17,28 +17,41 @@ class ScoreHeuristicEngine(HeuristicEngine):
|
||||||
|
|
||||||
class WeightHeuristicEngine(HeuristicEngine):
|
class WeightHeuristicEngine(HeuristicEngine):
|
||||||
|
|
||||||
|
def __init__(self, logger, options):
|
||||||
|
super().__init__(logger, options)
|
||||||
|
self.weights = self._get_weight_array()
|
||||||
|
self.logger.debug("{}".format(self.show_weights()))
|
||||||
|
|
||||||
def get(self, board, player):
|
def get(self, board, player):
|
||||||
score = self.get_weight(board, player)
|
score = self.get_weight(board, player)
|
||||||
return score
|
return score
|
||||||
|
|
||||||
def get_weight(self, board, player):
|
def get_weight(self, board, player):
|
||||||
score = 0
|
score = 0
|
||||||
size = board.get_board_size()
|
size = self.options['size']
|
||||||
weights = self._get_weight_array(size)
|
w = [[ 0 for _ in range(size)] for _ in range(size)]
|
||||||
for pos_x in range(size):
|
for pos_x in range(self.options['size']):
|
||||||
for pos_y in range(size):
|
for pos_y in range(self.options['size']):
|
||||||
if board._board[pos_x][pos_y] == player:
|
p = board._board[pos_x][pos_y]
|
||||||
score += weights[pos_x][pos_y]
|
if p == player:
|
||||||
else:
|
score += self.weights[pos_x][pos_y]
|
||||||
score -= weights[pos_x][pos_y]
|
w[pos_x][pos_y] = self.weights[pos_x][pos_y]
|
||||||
|
|
||||||
|
elif p != player and p != board._EMPTY:
|
||||||
|
score -= self.weights[pos_x][pos_y]
|
||||||
|
w[pos_x][pos_y] = -self.weights[pos_x][pos_y]
|
||||||
return score
|
return score
|
||||||
|
|
||||||
def _get_weight_array(self, size):
|
def _get_weight_array(self):
|
||||||
|
size = self.options['size']
|
||||||
w = [[ 0 for _ in range(size)] for _ in range(size)]
|
w = [[ 0 for _ in range(size)] for _ in range(size)]
|
||||||
padding = size // 5
|
padding = size // 5
|
||||||
center = size // 2
|
center = size // 2
|
||||||
for pos_y in range(size):
|
full_range = range(self.options['size'])
|
||||||
for pos_x in range(size):
|
center_range = range(center - padding, center + padding)
|
||||||
|
|
||||||
|
for pos_y in full_range:
|
||||||
|
for pos_x in full_range:
|
||||||
|
|
||||||
# Elements in the corner
|
# Elements in the corner
|
||||||
if pos_x in [0, size -1] and pos_y in [0,size - 1]:
|
if pos_x in [0, size -1] and pos_y in [0,size - 1]:
|
||||||
|
@ -49,10 +62,35 @@ class WeightHeuristicEngine(HeuristicEngine):
|
||||||
w[pos_x][pos_y] = self.options['weight'][1]
|
w[pos_x][pos_y] = self.options['weight'][1]
|
||||||
|
|
||||||
# Element the center
|
# Element the center
|
||||||
elif pos_x in range( center - padding, center + padding) and pos_y in range(center - padding, center + padding):
|
elif pos_x in center_range and pos_y in center_range:
|
||||||
w[pos_x][pos_y] = self.options['weight'][0]
|
w[pos_x][pos_y] = self.options['weight'][0]
|
||||||
return w
|
return w
|
||||||
|
|
||||||
|
|
||||||
|
def show_weights(self):
|
||||||
|
display = "\n |"
|
||||||
|
for x in range(self.options['size']):
|
||||||
|
display += "{:^3}|".format(x)
|
||||||
|
display += "\n"
|
||||||
|
for x in range(self.options['size']):
|
||||||
|
display += "{:^3}|".format(str(x))
|
||||||
|
for y in range(self.options['size']):
|
||||||
|
display += "{:^3}|".format(self.weights[x][y])
|
||||||
|
display += "\n"
|
||||||
|
return display
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def show_weight_score(weights, size):
|
||||||
|
display = "\n |"
|
||||||
|
for x in range(size):
|
||||||
|
display += "{:^3}|".format(x)
|
||||||
|
display += "\n"
|
||||||
|
for x in range(size):
|
||||||
|
display += "{:^3}|".format(str(x))
|
||||||
|
for y in range(size):
|
||||||
|
display += "{:^3}|".format(weights[x][y])
|
||||||
|
display += "\n"
|
||||||
|
return display
|
||||||
class FullHeuristicEngine(WeightHeuristicEngine):
|
class FullHeuristicEngine(WeightHeuristicEngine):
|
||||||
|
|
||||||
def get(self, board, player):
|
def get(self, board, player):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue