/
game.py
82 lines (78 loc) · 3.04 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
from random import sample
class Board:
def __init__(self, width, height, bombs):
self.board = {(x, y): 0 for x in range(width) for y in range(height)}
self.visible = {(x, y): False for x in range(width) for y in range(height)}
self.width = width
self.height = height
self.bombs = bombs
self.state = 'ongoing'
self.new = True
def placeBombs(self, clicked):
covered = []
for box in self.board:
if box != clicked:
covered.append(box)
bombLocations = sample(covered, self.bombs)
for (x, y) in bombLocations:
for dx in range(-1, 2):
for dy in range(-1, 2):
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.width and 0 <= y_ < self.height and isinstance(self.board[(x_, y_)], int):
self.board[(x_, y_)] += 1
self.board[(x, y)] = None
def update2DBoard(self):
self.board2D = []
for y in range(self.height):
row = []
for x in range(self.width):
if self.visible[(x, y)]:
row.append(self.board[(x, y)])
else:
row.append(None)
self.board2D.append(row)
def check(self):
if self.state != 'defeat':
victory = True
for x in range(self.width):
for y in range(self.height):
if (not self.visible[(x, y)]) and (self.board[(x, y)] is not None):
victory = False
break
if not victory:
break
if victory:
self.state = 'victory'
return self.state
def dig(self, x, y):
if self.new:
self.placeBombs((x, y))
self.new = False
if self.state == 'ongoing':
uncovered = set()
if 0 <= x < self.width and 0 <= y < self.height:
if self.visible[(x, y)]:
return uncovered
this = self.board[(x, y)]
if this is None:
self.state = 'defeat'
return None
elif this == 0:
self.visible[(x, y)] = True
uncovered.add((x, y))
for dx in range(-1, 2):
for dy in range(-1, 2):
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.width and 0 <= y_ < self.height and (dx, dy) != (0, 0):
if not self.visible[(x_, y_)]:
uncovered |= self.dig(x_, y_)
return uncovered
else:
self.visible[(x, y)] = True
uncovered.add((x, y))
return uncovered
return uncovered
else:
return None