-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.py
executable file
·141 lines (112 loc) · 3.93 KB
/
minesweeper.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
#!/usr/bin/env python3
import random
MINE = '*'
BLOCK = ' '
BLANK = '\u2588' # █
MARKED = '\u2691' # ⚑
class MineSweeper:
def __init__(self, w=8, h=8, n_mines=10):
if w < 8 or w > 30 or h < 8 or h > 24:
raise ValueError(
'Board size out of range: (width: 8-30, height: 8-24')
if n_mines > (w-1)*(h-1):
raise ValueError(
'Too many bombs: (max number of bombs: (width-1)*(height-1)')
self._gameover = False
self._w = w
self._h = h
self._n_mines = n_mines
self._board = [[BLANK]*self._w for y in range(self._h)]
self._mines = None
self._fill_mines()
def __str__(self):
column_nums = ' ' + ''.join('{:2}'.format(x+1)
for x in range(self._w))
board_string = column_nums
for y in range(self._h):
board_string += '\n{:2}|'.format(y+1) + \
'|'.join(self._board[y]) + '|' + \
'{:<2}'.format(y+1)
board_string += '\n' + column_nums
if self._gameover:
return board_string
else:
return board_string.replace(MINE, BLANK)
def _set(self, x, y, target):
self._board[y][x] = target
def _fill_mines(self):
mines = random.sample(range(self._h*self._w), self._n_mines)
self._mines = set((mine % self._w, mine // self._w) for mine in mines)
for x, y in self._mines:
self._set(x, y, MINE)
def out_of_bound(self, x, y):
return x < 0 or x >= self._w or y < 0 or y >= self._h
def click(self, x, y):
if self.is_mine(x, y):
self._gameover = True
else:
count = 0
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if not self.out_of_bound(i, j) and self.is_mine(i, j):
count += 1
if count == 0:
self._set(x, y, BLOCK)
for i in range(x-1, x+2):
for j in range(y-2, y+2):
if not self.out_of_bound(i, j) and self.is_blank(i, j):
self.click(i, j)
else:
self._set(x, y, str(count))
def mark(self, x, y):
if self.is_mine(x, y):
self._set(x, y, MARKED)
self._n_mines -= 1
elif self.is_marked(x, y):
self._set(x, y, MARKED)
elif self.is_marked(x, y):
if (x, y) in self._mines:
self._set(x, y, MINE)
self._n_mines += 1
else:
self._set(x, y, BLANK)
def is_blank(self, x, y):
return self._board[y][x] == BLANK
def is_marked(self, x, y):
return self._board[y][x] == MARKED
def is_mine(self, x, y):
return (x, y) in self._mines
def is_gameover(self):
return self._n_mines == 0 or self._gameover
def main():
difficulty = ((8, 8, 10), (16, 16, 40), (24, 24, 99), (30, 24, 200))
while True:
try:
level = int(input(
'Enter difficulty (1: Beginner, 2: Intermediate, 3: Expert, 4: Insane): '))-1
game = MineSweeper(*difficulty[level])
break
except (IndexError, ValueError):
print('Invalid input! ', end='')
continue
print(game)
while not game.is_gameover():
try:
action, y, x = input(
'c for click, m for mark, [c/m row column]: ').split()
i, j = int(x)-1, int(y)-1
if action == 'c':
game.click(i, j)
print(game)
elif action == 'm':
game.mark(i, j)
print(game)
else:
continue
except (IndexError, ValueError):
print('Invalid input! ', end='')
continue
print('Game Over!')
print(game)
if __name__ == '__main__':
main()