-
Notifications
You must be signed in to change notification settings - Fork 0
/
python minesweeper.py
98 lines (94 loc) · 3.29 KB
/
python 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
import random
def GenerateMineSweeperMap(n, k):
arr = [[0 for row in range(n)] for column in range(n)]
for num in range(k):
x = random.randint(0,n-1)
y = random.randint(0,n-1)
arr[y][x] = 'X'
if (x >=0 and x <= n-2) and (y >= 0 and y <= n-1):
if arr[y][x+1] != 'X':
arr[y][x+1] += 1 # center right
if (x >=1 and x <= n-1) and (y >= 0 and y <= n-1):
if arr[y][x-1] != 'X':
arr[y][x-1] += 1 # center left
if (x >= 1 and x <= n-1) and (y >= 1 and y <= n-1):
if arr[y-1][x-1] != 'X':
arr[y-1][x-1] += 1 # top left
if (x >= 0 and x <= n-2) and (y >= 1 and y <= n-1):
if arr[y-1][x+1] != 'X':
arr[y-1][x+1] += 1 # top right
if (x >= 0 and x <= n-1) and (y >= 1 and y <= n-1):
if arr[y-1][x] != 'X':
arr[y-1][x] += 1 # top center
if (x >=0 and x <= n-2) and (y >= 0 and y <= n-2):
if arr[y+1][x+1] != 'X':
arr[y+1][x+1] += 1 # bottom right
if (x >= 1 and x <= n-1) and (y >= 0 and y <= n-2):
if arr[y+1][x-1] != 'X':
arr[y+1][x-1] += 1 # bottom left
if (x >= 0 and x <= n-1) and (y >= 0 and y <= n-2):
if arr[y+1][x] != 'X':
arr[y+1][x] += 1 # bottom center
return arr
def GeneratePlayerMap(n):
arr = [['-' for row in range(n)] for column in range(n)]
return arr
def DisplayMap(map):
for row in map:
print(" ".join(str(cell) for cell in row))
print("")
def CheckWon(map):
for row in map:
for cell in row:
if cell == '-':
return False
return True
def CheckContinueGame(score):
print("Your score: ", score)
isContinue = input("Do you want to try again? (y/n) :")
if isContinue == 'n':
return False
return True
def Game():
GameStatus = True
while GameStatus:
difficulty = input("Select your difficulty (b, i, h):")
if difficulty.lower() == 'b':
n = 5
k = 3
elif difficulty.lower() == 'i':
n = 6
k = 8
else:
n = 8
k = 20
minesweeper_map = GenerateMineSweeperMap(n, k)
player_map = GeneratePlayerMap(n)
score = 0
while True:
if CheckWon(player_map) == False:
print("Enter your cell you want to open :")
x = input("X (1 to 5) :")
y = input("Y (1 to 5) :")
x = int(x) - 1 # 0 based indexing
y = int(y) - 1 # 0 based indexing
if (minesweeper_map[y][x] == 'X'):
print("Game Over!")
DisplayMap(minesweeper_map)
GameStatus = CheckContinueGame(score)
break
else:
player_map[y][x] = minesweeper_map[y][x]
DisplayMap(player_map)
score += 1
else:
DisplayMap(player_map)
print("You have Won!")
GameStatus = CheckContinueGame(score)
break
# Start of Program
if __name__ == "__main__":
try:
Game()
except KeyboardInterrupt:
print('\nEnd of Game. Bye Bye!')