-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb_test.py
More file actions
43 lines (36 loc) · 974 Bytes
/
fb_test.py
File metadata and controls
43 lines (36 loc) · 974 Bytes
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
# 1 - Make a minesweeper of size LxW with M amount of mines distributed randomly
# 2 - Where no mine is found give away the sum of mines around the cell.
import random
def try_ex(x,y,grid):
try:
if grid[x][y] == -1 and x>=0 and y>=0:
return 1
else:
return 0
except:
return 0
def mines(L,W,M):
zero_grid = [ [0] * W for j in range(L) ]
while M>0:
random_row = random.randint(0,W-1)
random_col = random.randint(0,L-1)
if zero_grid[random_row][random_col] == 0:
zero_grid[random_row][random_col] = -1
M-=1
grid = zero_grid
for x in range(0,W):
for y in range(0,L):
if grid[x][y] == 0:
_sum = 0
_sum += try_ex(x,y-1,grid)
_sum += try_ex(x,y+1,grid)
_sum += try_ex(x-1,y,grid)
_sum += try_ex(x+1,y,grid)
_sum += try_ex(x-1,y-1,grid)
_sum += try_ex(x+1,y+1,grid)
_sum += try_ex(x+1,y-1,grid)
_sum += try_ex(x-1,y+1,grid)
grid[x][y] = _sum
return grid
if __name__ == '__main__':
print mines(4,4,9)