forked from sdlee94/Minesweeper-AI-Reinforcement-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper_env.py
212 lines (164 loc) · 6.5 KB
/
minesweeper_env.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import random
import numpy as np
import pandas as pd
class MinesweeperEnv(object):
def __init__(self, width, height, n_mines,
# based on https://github.com/jakejhansen/minesweeper_solver
rewards={'win':1, 'lose':-1, 'progress':0.3, 'guess':-0.3, 'no_progress' : -0.3}):
self.nrows, self.ncols = width, height
self.ntiles = self.nrows * self.ncols
self.n_mines = n_mines
self.grid = self.init_grid()
self.board = self.get_board()
self.state, self.state_im = self.init_state()
self.n_clicks = 0
self.n_progress = 0
self.n_wins = 0
self.rewards = rewards
def init_grid(self):
board = np.zeros((self.nrows, self.ncols), dtype='object')
mines = self.n_mines
while mines > 0:
row, col = random.randint(0, self.nrows-1), random.randint(0, self.ncols-1)
if board[row][col] != 'B':
board[row][col] = 'B'
mines -= 1
return board
def get_neighbors(self, coord):
x,y = coord[0], coord[1]
neighbors = []
for col in range(y-1, y+2):
for row in range(x-1, x+2):
if ((x != row or y != col) and
(0 <= col < self.ncols) and
(0 <= row < self.nrows)):
neighbors.append(self.grid[row,col])
return np.array(neighbors)
def count_bombs(self, coord):
neighbors = self.get_neighbors(coord)
return np.sum(neighbors=='B')
def get_board(self):
board = self.grid.copy()
coords = []
for x in range(self.nrows):
for y in range(self.ncols):
if self.grid[x,y] != 'B':
coords.append((x,y))
for coord in coords:
board[coord] = self.count_bombs(coord)
return board
def get_state_im(self, state):
'''
Gets the numeric image representation state of the board.
This is what will be the input for the DQN.
'''
state_im = [t['value'] for t in state]
state_im = np.reshape(state_im, (self.nrows, self.ncols, 1)).astype(object)
state_im[state_im=='U'] = -1
state_im[state_im=='B'] = -2
state_im = state_im.astype(np.int8) / 8
state_im = state_im.astype(np.float16)
return state_im
def init_state(self):
unsolved_array = np.full((self.nrows, self.ncols), 'U', dtype='object')
state = []
for (x, y), value in np.ndenumerate(unsolved_array):
state.append({'coord': (x, y), 'value':value})
state_im = self.get_state_im(state)
return state, state_im
def color_state(self, value):
if value == -1:
color = 'white'
elif value == 0:
color = 'slategrey'
elif value == 1:
color = 'blue'
elif value == 2:
color = 'green'
elif value == 3:
color = 'red'
elif value == 4:
color = 'midnightblue'
elif value == 5:
color = 'brown'
elif value == 6:
color = 'aquamarine'
elif value == 7:
color = 'black'
elif value == 8:
color = 'silver'
else:
color = 'magenta'
return f'color: {color}'
def draw_state(self, state_im):
state = state_im * 8.0
state_df = pd.DataFrame(state.reshape((self.nrows, self.ncols)), dtype=np.int8)
display(state_df.style.applymap(self.color_state))
def click(self, action_index):
coord = self.state[action_index]['coord']
value = self.board[coord]
# ensure first move is not a bomb
if (value == 'B') and (self.n_clicks == 0):
grid = self.grid.reshape(1, self.ntiles)
move = np.random.choice(np.nonzero(grid!='B')[1])
coord = self.state[move]['coord']
value = self.board[coord]
self.state[move]['value'] = value
else:
# make state equal to board at given coordinates
self.state[action_index]['value'] = value
# reveal all neighbors if value is 0
if value == 0.0:
self.reveal_neighbors(coord, clicked_tiles=[])
self.n_clicks += 1
def reveal_neighbors(self, coord, clicked_tiles):
processed = clicked_tiles
state_df = pd.DataFrame(self.state)
x,y = coord[0], coord[1]
neighbors = []
for col in range(y-1, y+2):
for row in range(x-1, x+2):
if ((x != row or y != col) and
(0 <= col < self.ncols) and
(0 <= row < self.nrows) and
((row, col) not in processed)):
# prevent redundancy for adjacent zeros
processed.append((row,col))
index = state_df.index[state_df['coord'] == (row,col)].tolist()[0]
self.state[index]['value'] = self.board[row, col]
# recursion in case neighbors are also 0
if self.board[row, col] == 0.0:
self.reveal_neighbors((row, col), clicked_tiles=processed)
def reset(self):
self.n_clicks = 0
self.n_progress = 0
self.grid = self.init_grid()
self.board = self.get_board()
self.state, self.state_im = self.init_state()
def step(self, action_index):
done = False
coords = self.state[action_index]['coord']
current_state = self.state_im
# get neighbors before action
neighbors = self.get_neighbors(coords)
self.click(action_index)
# update state image
new_state_im = self.get_state_im(self.state)
self.state_im = new_state_im
if self.state[action_index]['value']=='B': # if lose
reward = self.rewards['lose']
done = True
elif np.sum(new_state_im==-0.125) == self.n_mines: # if win
reward = self.rewards['win']
done = True
self.n_progress += 1
self.n_wins += 1
elif np.sum(self.state_im == -0.125) == np.sum(current_state == -0.125):
reward = self.rewards['no_progress']
else: # if progress
if all(t==-0.125 for t in neighbors): # if guess (all neighbors are unsolved)
reward = self.rewards['guess']
else:
reward = self.rewards['progress']
self.n_progress += 1 # track n of non-isoloated clicks
return self.state_im, reward, done