-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
249 lines (209 loc) · 7.59 KB
/
game.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import random
from typing import Tuple
import math
import array
from beautifultable import BeautifulTable
import numpy as np
class MinesGame:
def __init__(self, height, width, human=False):
self.HEIGHT = height
self.WIDTH = width
MINES_RATIO = 0.12
self.MINES_AMOUNT = int(round((MINES_RATIO * (self.WIDTH * self.HEIGHT)), 0))
self.mines_location = self.get_mines_location()
self.create_board()
self.render = False
if human:
print(f"MINES: {self.MINES_AMOUNT}")
print(f"MINES LOCATION: {self.mines_location}")
self.print_board()
self.start_game()
def start_game(self):
over = False
while not over:
over = self.ask_for_input()
won = not self.check_if_correct()
if won:
print("You won")
else:
print("You lost")
def render_games(self):
self.render = True
def enter_input(self, mine_location, is_array=False) -> Tuple[list, bool, int]:
if is_array:
mine_location = np.argmax(mine_location)
row = math.floor(mine_location / self.WIDTH)
column = mine_location % self.HEIGHT
over = False
won = False
reward = 1
hit_square = self.board[row][column]
if (
(hit_square == -1)
or (hit_square == 0)
or (self.game_board[row][column] != -2)
):
if hit_square == -1:
self.game_board[row][column] = self.board[row][column]
over = True
reward = 0
else:
self.game_board[row][column] = self.board[row][column]
self.locations_free -= 1
if self.render:
self.print_board()
if self.locations_free == 0:
over = True
won = True
return self.game_board, over, reward, won
def check_if_correct(self):
# Check if the result is correct
wrong = False
mine_count = 0
for mine_location in self.mines_location:
mine_location = {
"row": math.floor(mine_location / self.WIDTH) - 1,
"column": (mine_location % self.HEIGHT) - 1,
}
if self.game_board[mine_location["row"]][column_index] != -3:
wrong = True
break
else:
mine_count += 1
print(f"MINE_COUNT: {mine_count}")
return wrong
def get_mine_location_from_int(self, number):
mine_location = {
"row": math.floor(number / self.WIDTH),
"column": number % self.HEIGHT,
}
return mine_location
def create_and_fill_board(self, item_to_fill=0):
board = []
for column in range(self.WIDTH):
board.append([])
for _row in range(self.HEIGHT):
board[column].append(item_to_fill)
return board
def print_board(self, game=True, board=None):
symbols_map = {
"-2": "🟩",
"0": "🟫",
"1": "𝟣",
"2": "𝟤",
"3": "𝟥",
"4": "𝟦",
"5": "𝟧",
"6": "𝟨",
"7": "𝟩",
"8": "𝟪",
}
if board is None:
board = self.game_board
index = 0
table = BeautifulTable()
for row in board:
fancy_row = []
for column_index in range(len(row)):
fancy_row.append(symbols_map[str(row[column_index])])
table.append_row(fancy_row)
index += 1
print(table)
def create_board(self):
# FIXME: use the create_and_fill_board function
self.board = []
self.game_board = []
# Fill with 0
for column in range(self.WIDTH):
self.board.append([])
self.game_board.append([])
for _row in range(self.HEIGHT):
self.board[column].append(0)
# -2 is equal to not seen
self.game_board[column].append(-2)
# Put in all +1 the ones that touch the mines
for mine_location in self.mines_location:
# Transform from the numbers to column and row
# -1 is becuase it's not 0 indexed
mine_location = {
"row": math.floor(mine_location / self.WIDTH),
"column": (mine_location % self.HEIGHT),
}
row_index = mine_location["row"]
column_index = mine_location["column"]
# +1 To fields next to it
# TOP
if row_index > 0:
# MIDDLE
self.board[row_index - 1][column_index] += 1
# RIGHT
if column_index < self.WIDTH - 1:
self.board[row_index - 1][column_index + 1] += 1
# LEFT
if column_index > 0:
self.board[row_index - 1][column_index - 1] += 1
# MIDDLE
if column_index > 0:
self.board[row_index][column_index - 1] += 1
if column_index < self.WIDTH - 1:
self.board[row_index][column_index + 1] += 1
# BOTTOM
if mine_location["row"] < self.HEIGHT - 1:
# MIDDLE
self.board[row_index + 1][column_index] += 1
# RIGHT
if column_index < self.WIDTH - 1:
self.board[row_index + 1][column_index + 1] += 1
# LEFT
if column_index > 0:
self.board[row_index + 1][column_index - 1] += 1
# Write all the mines
for mine_location in self.mines_location:
mine_location = {
"row": math.floor(mine_location / self.WIDTH),
"column": (mine_location % self.HEIGHT),
}
self.board[mine_location["row"]][mine_location["column"]] = -1
zeros = 0
for row in range(self.WIDTH):
for column in range(self.HEIGHT):
if self.board[row][column] == 0:
self.game_board[row][column] = 0
zeros += 1
self.locations_free = (self.HEIGHT * self.WIDTH) - zeros - self.MINES_AMOUNT
def get_zeros_in_board(self):
zeros_in_board = 0
for row in self.board:
for square in row:
if square == 0:
zeros_in_board += 1
return zeros_in_board
def ask_for_input(self) -> bool:
row = int(input("ROW: ")) - 1
column = int(input("COLUMN: ")) - 1
flag = input("FLAG: ")
finished = input("HAVE YOU FINISHED: ")
if finished == "True":
finished = True
else:
finished = False
print(f"FINISHED: {finished}")
print(f"GUESSED: {self.board[row][column]}")
if flag == "True":
# M is equal to -3
self.game_board[row][column] = -3
else:
self.game_board[row][column] = self.board[row][column]
self.print_board()
return finished
def get_mines_location(self) -> list:
ocupied_numbers = {}
random_numbers = []
for _number in range(self.MINES_AMOUNT):
random_number = random.randint(0, ((self.HEIGHT * self.WIDTH) - 1))
if not random_number in ocupied_numbers:
random_numbers.append(random_number)
ocupied_numbers[random_number] = "ocupied"
# I sorted them so I can find them easier when I create the board
sorted_numbers = sorted(random_numbers)
return sorted_numbers