-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
151 lines (132 loc) · 4.23 KB
/
util.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import random
def dotProduct(v1, v2):
sum = 0;
for x in v1:
sum += v1[x] * v2[x]
return sum;
def incrementSparseVector(v1, scale, v2):
for x in v2:
v1[x] += v2[x] * scale;
def multiplySparseVector(v1, scale):
for x in v1:
v1[x] *= scale;
def getOppIndex(playerIndex):
return abs(playerIndex - 1);
def printSimpleBoard(board):
for i in range(3):
print board[i][0], board[i][1], board[i][2];
def printBoard(board):
for i in range(3):
curGrid1 = board[i*3].grid
curGrid2 = board[i*3 + 1].grid
curGrid3 = board[i*3 + 2].grid
for k in range(3):
for j in range(3):
print curGrid1[k][j],
for j in range(3):
print curGrid2[k][j],
for j in range(3):
print curGrid3[k][j],
print
def printBoardStatuses(board):
for i in range(3):
printString = "";
for q in range(3):
printString += str(board[i * 3 + q].status);
print printString;
print;
def getGridWins(state): # returns tuple of (agentWins, oppWins)
agentWins = 0
oppWins = 0
for grid in state[0]: #state[0] = board
if(grid.status == 1):
agentWins += 1
if(grid.status == 2):
oppWins += 1
return (agentWins, oppWins)
def posFromRowCol(row, col):
return 3*row + col
def rowColFromPos(pos):
row = pos/3
col = pos % 3
return (row, col)
def randomMax(successors):
maximumValue = max(successors)[0];
possibleSuccessors = [successor for successor in successors if successor[0] == maximumValue];
return random.choice(possibleSuccessors)[1];
def countCenterMoves(state, player):
centerMoves = 0
board = state[0]
for grid in board:
if(grid.grid[1][1] == player + 1):
centerMoves += 1
return centerMoves
def countCornerMoves(state, player):
cornerMoves = 0
board = state[0]
for grid in board:
if(grid.grid[0][0] == player + 1):
cornerMoves += 1
if(grid.grid[0][2] == player + 1):
cornerMoves += 1
if(grid.grid[2][0] == player + 1):
cornerMoves += 1
if(grid.grid[2][2] == player + 1):
cornerMoves += 1
return cornerMoves
def simpleCountCornerMoves(state, player):
cornerMoves = 0;
board = state[0];
if(board[0][0] == player + 1):
cornerMoves += 1
if(board[0][2] == player + 1):
cornerMoves += 1
if(board[2][0] == player + 1):
cornerMoves += 1
if(board[2][2] == player + 1):
cornerMoves += 1
return cornerMoves;
def countAdjacentMoves(state, player):
adjMoves = 0
board = state[0]
for grid in board:
adjMoves += gridAdjacentMoves(grid, player)
return adjMoves;
def countAdjacentGrids(state):
# printBoard(state[0]);
grids = [0, 0]
board = state[0];
for row in range(3):
for col in range(3):
if board[row * 3 + col].status == 1 or board[row * 3 + col].status == 2:
status = board[row * 3 + col].status;
for dx in range(-1, 2):
for dy in range(-1, 2):
if not (dy == 0 and dx == 0) and row + dx > -1 and row + dx < 3 and col + dy > -1 and col + dy < 3:
if board[(row + dx) * 3 + (col + dy)].status == status:
grids[status - 1] += 1;
return grids;
def gridAdjacentMoves(grid, player):
adjMoves = 0
grid = grid.grid
for x in range(3):
for y in range(3):
if(grid[x][y] == player + 1):
adjMoves += adjacentToSpace(grid, x, y)
return adjMoves
def simpleAdjacentMoves(state, player):
adjMoves = 0;
grid = state[0];
for x in range(3):
for y in range(3):
if(grid[x][y] == player + 1):
adjMoves += adjacentToSpace(grid, x, y)
return adjMoves
def adjacentToSpace(grid,x, y):
adjMoves = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
if((x + dx > -1 and x + dx < 3) and (y + dy > -1 and y + dy < 3)): ## in bounds
if(grid[x][y] == grid[x+dx][y+dy] and (not (dx == 0 and dy == 0))):
adjMoves += 1
return adjMoves