-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
133 lines (112 loc) · 3.14 KB
/
utils.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
# Utilities for translating AI moves to game and Trainer
#
# Author: Luke Munro
import smtplib
import numpy as np
# ---------- For Neural Net ----------------
def orderMoves(probs): # CONDENSE fix for same prob
moves = []
probs = np.array(probs).tolist()
tProbs = list(probs)
for i in range(len(probs)):
high = max(tProbs)
index = probs.index(high)
probs[index] = None # working fix for same probs bug
moves.append(index)
tProbs.remove(high)
return moves
def find_moves(game_state, grid_dim):
game_state = clean_game_state(game_state)
moves = [x for x in range(len(game_state)) if game_state[x] == 0]
return moves
def makeCommands(grid_dim):
moveCommands = []
for i in range(grid_dim*2+1):
if i%2==0:
for x in range(grid_dim):
moveCommands.append(str(i)+" "+str(x))
else:
for x in range(grid_dim+1):
moveCommands.append(str(i)+" "+str(x))
return moveCommands
def formatMoves(moveOrder, commands): # CONDENSE
fmatMoves = []
for move in moveOrder:
fmatMoves.append(commands[move])
return fmatMoves
def onlyLegal(moves, game_state): # CONDENSE
legal_moves = []
for i in range(len(game_state)):
if game_state[i] == 0:
legal_moves.append(i)
move_order = filter(lambda x: x in legal_moves, moves) #Remove illegal moves
return move_order
def cleanData(raw):
data = [[int(i)] for x in raw for i in x]
data = np.array(data)
return data
# ----------- For minimax algorithm -----------
def clean_game_state(game_state):
return [x for row in game_state for x in row]
def num_best_moves(move_ratings):
possible_best_moves = 0
best_score = max(move_ratings)
for score in move_ratings:
if score == best_score:
possible_best_moves += 1
return possible_best_moves
# ---------------------------------------------
def relive_game(dim, game_record):
for pair in game_record[1:]:
for state in pair:
print ("- Next Move -")
display_game(dim, state)
def relive_game_from_file(dim, game_record):
for state in game_record:
print ("- Next Move -")
display_game(dim, state)
def assemble_state(dim, game_record):
game_state = []
i = 0
for x in range(dim*2+1):
if x%2==0:
game_state.append(game_record[i:i+3])
i += 3
else:
game_state.append(game_record[i:i+4])
i += 4
return game_state
def display_game(dim, game_state):
buffer = [] #what is this
hLine = "+---"
hEmpty = "+ "
vLine = "| "
vEmpty = " "
# Top row
for i in range(dim):
if game_state[0][i] == 1:
buffer.append(hLine)
else: buffer.append(hEmpty)
buffer.append("+\n")
# Middle rows
for i in range(1, dim*2, 2):
# Make horizontal passes
for j in range(dim+1):
if game_state[i][j] == 1:
buffer.append(vLine)
else: buffer.append(vEmpty)
buffer.append("\n")
# Vertical passes
for j in range(dim):
if game_state[i+1][j] == 1:
buffer.append(hLine)
else: buffer.append(hEmpty)
buffer.append("+\n")
print ("".join(buffer))
# ---------------------------------------------------------------
def send_mail(msg):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("[email protected]", "wussgood$$")
server.sendmail("[email protected]", "[email protected]", msg)
server.quit()