-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (114 loc) · 3.43 KB
/
main.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
# Minesweeper1
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
# test 3
import re
from random import randint
numRows = 10
numCols = 12
grid = []
def initGrid():
for i in range( numRows ):
grid.append( [] )
for row in range(numRows):
curRow = grid[row]
initRow(curRow)
setMines()
def initRow(curRow):
for i in range(numCols):
curRow.append('.')
def setMines():
for row in range( numRows ):
for col in range(numCols):
if randint(0,8) == 8:
setGridItem(row, col, '*')
else:
setGridItem(row, col, '.')
def setGridItem(row, col, s):
grid[row][col] = s
def countMinesAbove(curRow, curCol):
count = 0
if curRow == 0:
return 0
if curCol == 0:
if grid[curRow - 1][curCol] == '*':
count += 1
if grid[curRow - 1][curCol + 1] == '*':
count += 1
else:
if grid[curRow - 1][curCol-1] == '*':
count += 1
if grid[curRow - 1][curCol ] == '*':
count += 1
if curCol < numCols -1 and curRow < numRows - 1 and \
grid[curRow - 1][curCol + 1] == '*':
count += 1
return count
def countMinesBelow(curRow, curCol):
count = 0
if curRow == numRows - 1:
return 0
if curCol == 0:
if grid[curRow + 1][curCol] == '*':
count += 1
if grid[curRow + 1][curCol + 1] == '*':
count += 1
else:
if grid[curRow + 1][curCol-1] == '*':
count += 1
if grid[curRow + 1][curCol ] == '*':
count += 1
if curCol < numCols - 1 and \
curRow < numRows - 1 and \
grid[curRow + 1][curCol + 1] == '*':
count += 1
return count
def countMinesLeft(curRow, curCol):
if curCol == 0: # if we are in the leftmost column then no mines to the left
return 0
else:
if grid[curRow][curCol-1] == '*': # Check to the left
return 1
else:
return 0
def countMinesRight(curRow, curCol):
if curCol == numCols - 1: # if we are in the rightmost column then no mines to the right
return 0
else:
if grid[curRow][curCol+1] == '*': # Check to the right
return 1
else:
return 0
def countNearbyMines(curRow, curCol):
return countMinesAbove(curRow, curCol) + \
countMinesBelow(curRow, curCol) + \
countMinesLeft( curRow, curCol) + \
countMinesRight(curRow, curCol)
def populateGridCounts():
for row in range(numRows):
for col in range(numCols):
if grid[row][col] != '*':
grid[row][col] = countNearbyMines(row, col)
if col == numCols:
grid[row][col+1] = countNearbyMines(row, col)
def printGrid():
for row in grid:
print(row)
def gameControlLoop():
while True:
s = input('Enter grid coordinates. Enter "0 0" to quit:')
match = re.search(r"(\d+)\s(\d+)", s)
if match:
m = int( match.group(1) )
n = int( match.group(2) )
if m == 0 and n == 0:
exit()
if grid[m][n] == '*':
print('You hit a mine!')
exit()
grid[m][n] = 'F'
printGrid()
if __name__ == "__main__":
initGrid()
populateGridCounts()
gameControlLoop()
printGrid()