-
Notifications
You must be signed in to change notification settings - Fork 0
/
B5CSI14_Minesweeper.py
173 lines (133 loc) · 5.35 KB
/
B5CSI14_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
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
import random
import re
class Board:
def __init__(self, boardSize, boardBombs):
self.boardSize = boardSize
self.boardBombs = boardBombs
self.board = self.make_new_board()
self.assign_values_to_board()
self.dug = set()
def make_new_board(self):
board = [[None for _ in range(self.boardSize)] for _ in range(self.boardSize)]
bombsPlanted = 0
while bombsPlanted < self.boardBombs:
loc = random.randint(0, self.boardSize ** 2 - 1)
row = loc // self.boardSize
col = loc % self.boardSize
if board[row][col] == '*':
continue
board[row][col] = '*'
bombsPlanted += 1
return board
def assign_values_to_board(self):
for r in range(self.boardSize):
for c in range(self.boardSize):
if self.board[r][c] == '*':
continue
self.board[r][c] = self.get_num_neighbouring_bombs(r, c)
def get_num_neighbouring_bombs(self, row, col):
num_neighbouring_bombs = 0
for r in range(max(0, row - 1), min(self.boardSize - 1, row + 1) + 1):
for c in range(max(0, col - 1), min(self.boardSize - 1, col + 1) + 1):
if r == row and c == col:
continue
if self.board[r][c] == '*':
num_neighbouring_bombs += 1
return num_neighbouring_bombs
def dig(self, row, col):
self.dug.add((row, col))
if self.board[row][col] == '*':
return False
elif self.board[row][col] > 0:
return True
for r in range(max(0, row - 1), min(self.boardSize - 1, row + 1) + 1):
for c in range(max(0, col - 1), min(self.boardSize - 1, col + 1) + 1):
if (r, c) in self.dug:
continue
self.dig(r, c)
return True
def __str__(self):
visible_board = [[None for _ in range(self.boardSize)] for _ in range(self.boardSize)]
for row in range(self.boardSize):
for col in range(self.boardSize):
if (row, col) in self.dug:
visible_board[row][col] = str(self.board[row][col])
else:
visible_board[row][col] = ' '
# The code below is for appearance of the board
string_rep = ''
widths = []
for idx in range(self.boardSize):
columns = map(lambda x: x[idx], visible_board)
widths.append(
len(
max(columns, key=len)
)
)
indices = [i for i in range(self.boardSize)]
indices_row = ' '
cells = []
for idx, col in enumerate(indices):
format = '%-' + str(widths[idx]) + "s"
cells.append(format % (col))
indices_row += ' '.join(cells)
indices_row += ' \n'
for i in range(len(visible_board)):
row = visible_board[i]
string_rep += f'{i} |'
cells = []
for idx, col in enumerate(row):
format = '%-' + str(widths[idx]) + "s"
cells.append(format % (col))
string_rep += ' |'.join(cells)
string_rep += ' |\n'
str_len = int(len(string_rep) / self.boardSize)
string_rep = indices_row + '-' * str_len + '\n' + string_rep + '-' * str_len
return string_rep
# Code for game functionality
def play(boardSize=10, boardBombs=10): # Right over here you can change the board's total size and number of bombs on the board
board = Board(boardSize, boardBombs)
safe = True
while len(board.dug) < board.boardSize ** 2 - boardBombs:
print(board)
user_input = re.split(',(\\s)*',
input("Where would you like to dig ? Input as row,column: "))
row, col = int(user_input[0]), int(user_input[-1])
if row < 0 or row >= board.boardSize or col < 0 or col >= boardSize:
print("Invalid location. Try again.")
continue
safe = board.dig(row, col)
if not safe:
break
if safe:
print("\nCONGRATULAIONS!!! You avoided every single landmine!")
else:
board.dug = [(r, c) for r in range(board.boardSize) for c in range(board.boardSize)]
print(board)
print("\t|| GAME OVER ||")
# Added instructions 11/9/21 according to suggestion by a mentor
def rules():
print('''\t\t|| MINESWEEPER ||\n
- Your objective is to clear the board in front of you
without triggering any landmines(*) with the help from
clues about the number of neighbouring mines in each field.
- For example if a tile shows the number "2", it means that
tile is in contact with two landmines (in 3x3 field around the tile).
- Play the game yourselves to get the hang of it''')
userInput = input("\tPress |S| to Start the game or |Q| to Quit\n")
if userInput == "S":
play()
elif userInput == "Q":
print("Thankyou !")
else:
print("Invalid choice, press only |P| or |Q|")
print("1. Start Game\n2. What exactly are you supposed to do ?\n3. Quit Game\n")
userInput = int(input("Enter your choice below\n"))
if userInput == 1:
play()
elif userInput == 2:
rules()
elif userInput == 3:
print("Thankyou !")
else:
print("Enter a valid option")