-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMidControl.py
More file actions
89 lines (73 loc) · 4.14 KB
/
MidControl.py
File metadata and controls
89 lines (73 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ConsecutivePieces as Cp
import math
import copy
def mid_control_streaks(board, turn):
"""
Uses the 'calculate_streaks' method to work out the heuristic values. Then the heuristic values of the pieces that
are in the middle 5x5 area of the board is doubled. This will bump up the final score as well.
:param board: a Pente game board
:param turn: either 1 or 2, depending on if the 1st or 2nd player is wanting to place a piece
:return: current board, board with heuristic values, and a score that is calculated based on the heuristic
"""
board, heuristics, score = Cp.calculate_streaks(board, turn)
size = len(board)
if size < 6: # No point calculating this heuristic for smaller boards
return board, heuristics, score
mid = math.ceil(size / 2) # Getting the middle point of the board
for i in range(mid - 3, mid + 2):
for j in range(mid - 3, mid + 2):
if heuristics[i][j] > 2: # Doubling the score of the heuristic values that are in
heuristics[i][j] = 2 * heuristics[i][j] # the middle of the board.
score = 0
for i in range(size):
for j in range(size):
if heuristics[i][j] > 2:
score += heuristics[i][j]
return board, heuristics, score
def mid_control_pieces(board, turn):
"""
Calculates a heuristic score based on the placements inside the middle 5x5 area of the board. For every turn's piece
the score is increased by one. For every opponents piece, the score is decreased by one.
Each node adjacent to a turn player's piece, will get a heuristic value of either 3 or 6. If the piece is inside the
middle 5x5 area the value is 6. Otherwise 3.
:param board: a Pente game board
:param turn: either 1 or 2, depending on if the 1st or 2nd player is wanting to place a piece
:return: current board, board with heuristic values, and a score that is calculated based on the heuristic
"""
size = len(board)
heuristics = copy.deepcopy(board)
opponent = 2 if turn == 1 else 1 # Getting the opponent
mid = math.ceil(size / 2) # Getting the middle point of the board
score = 0
# Calculating heuristics of the middle 5x5 area
for i in range(mid - 3, mid + 2):
for j in range(mid - 3, mid + 2):
if board[i][j] == turn:
score += 1
for axis in [(1, 0), (0, 1), (1, 1), (1, -1)]: # Four axes that we consider
if mid - 3 <= i - axis[0] < mid + 2 and mid - 3 <= j - axis[1] < mid + 2:
node = board[i - axis[0]][j - axis[1]]
if node != turn and node != opponent:
heuristics[i - axis[0]][j - axis[1]] = 6
if mid - 3 <= i + axis[0] < mid + 2 and mid - 3 <= j + axis[1] < mid + 2:
node = board[i + axis[0]][j + axis[1]]
if node != turn and node != opponent:
heuristics[i + axis[0]][j + axis[1]] = 6
elif board[i][j] == opponent:
score -= 1
# Calculating heuristics of the rest of the board
for i in range(size):
for j in range(size):
if mid - 3 <= i < mid + 2 or mid - 3 <= j < mid + 2:
continue
if board[i][j] == turn:
for axis in [(1, 0), (0, 1), (1, 1), (1, -1)]: # Four axes that we consider
if 0 <= i - axis[0] < size and 0 <= j - axis[1] < size:
node = board[i - axis[0]][j - axis[1]]
if node != turn and node != opponent:
heuristics[i - axis[0]][j - axis[1]] = 3
if 0 <= i + axis[0] < size and 0 <= j + axis[1] < size:
node = board[i + axis[0]][j + axis[1]]
if node != turn and node != opponent:
heuristics[i + axis[0]][j + axis[1]] = 3
return board, heuristics, score