Add weight calculation heuristic
This commit is contained in:
parent
45abd8c7eb
commit
1c039106d9
3 changed files with 59 additions and 3 deletions
38
src/classes/Heuristic.py
Normal file
38
src/classes/Heuristic.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
class ReversiHeuristic():
|
||||
def __init__(self, logger, weight = [1, 2, 10, 20]):
|
||||
self.logger = logger
|
||||
self.weight = weight
|
||||
|
||||
def get(self, board):
|
||||
size = board.get_board_size()
|
||||
score = 0
|
||||
weights = self._get_weight(size)
|
||||
for pos_x in range(size):
|
||||
for pos_y in range(size):
|
||||
if board._board[pos_x][pos_y] == board._nextPlayer:
|
||||
score += weights[pos_x][pos_y]
|
||||
else:
|
||||
score -= weights[pos_x][pos_y]
|
||||
return score
|
||||
|
||||
def _get_weight(self, size):
|
||||
w = [[ 0 for _ in range(size)] for _ in range(size)]
|
||||
|
||||
for pos_y in range(size):
|
||||
for pos_x in range(size):
|
||||
|
||||
# Elements in the corner
|
||||
if pos_x in [0, size -1] and pos_y in [0,size - 1]:
|
||||
w[pos_x][pos_y] = self.weight[3]
|
||||
|
||||
# Elements on the border
|
||||
elif pos_x in [0, size -1] or pos_y in [0, size -1]:
|
||||
w[pos_x][pos_y] = self.weight[2]
|
||||
|
||||
# Element the center
|
||||
elif pos_x in range( int(size // 2 - 2), size // 2 + 2) and pos_y in range( size // 2 - 2, size // 2 + 2):
|
||||
w[pos_x][pos_y] = self.weight[1]
|
||||
|
||||
else:
|
||||
w[pos_x][pos_y] = self.weight[0]
|
||||
return w
|
Loading…
Add table
Add a link
Reference in a new issue