1
+ import random
2
+
3
+ # Generate a random map to start the game.
4
+ def randomMap (n , k ):
5
+
6
+ arr = [[0 for row in range (0 ,n )] for column in range (0 ,n )]
7
+
8
+ for num in range (0 ,k ):
9
+ x = random .randint (0 ,n - 1 )
10
+ y = random .randint (0 ,n - 1 )
11
+ arr [y ][x ] = 'X'
12
+
13
+ if (x >= 0 and x <= n - 2 ) and (y >= 0 and y <= n - 1 ):
14
+ if arr [y ][x + 1 ] != 'X' :
15
+ arr [y ][x + 1 ] += 1 # center right
16
+
17
+ if (x >= 1 and x <= n - 1 ) and (y >= 0 and y <= n - 1 ):
18
+ if arr [y ][x - 1 ] != 'X' :
19
+ arr [y ][x - 1 ] += 1 # center left
20
+
21
+ if (x >= 1 and x <= n - 1 ) and (y >= 1 and y <= n - 1 ):
22
+ if arr [y - 1 ][x - 1 ] != 'X' :
23
+ arr [y - 1 ][x - 1 ] += 1 # top left
24
+
25
+ if (x >= 0 and x <= n - 2 ) and (y >= 1 and y <= n - 1 ):
26
+ if arr [y - 1 ][x + 1 ] != 'X' :
27
+ arr [y - 1 ][x + 1 ] += 1 # top right
28
+
29
+ if (x >= 0 and x <= n - 1 ) and (y >= 1 and y <= n - 1 ):
30
+ if arr [y - 1 ][x ] != 'X' :
31
+ arr [y - 1 ][x ] += 1 # top center
32
+
33
+ if (x >= 0 and x <= n - 2 ) and (y >= 0 and y <= n - 2 ):
34
+ if arr [y + 1 ][x + 1 ] != 'X' :
35
+ arr [y + 1 ][x + 1 ] += 1 # bottom right
36
+
37
+ if (x >= 1 and x <= n - 1 ) and (y >= 0 and y <= n - 2 ):
38
+ if arr [y + 1 ][x - 1 ] != 'X' :
39
+ arr [y + 1 ][x - 1 ] += 1 # bottom left
40
+
41
+ if (x >= 0 and x <= n - 1 ) and (y >= 0 and y <= n - 2 ):
42
+ if arr [y + 1 ][x ] != 'X' :
43
+ arr [y + 1 ][x ] += 1 # bottom center
44
+ return arr
45
+
46
+ # Generate the map for the player.
47
+ def playerMap (n ):
48
+ arr = [['-' for row in range (0 ,n )] for column in range (0 ,n )]
49
+ return arr
50
+
51
+ # Display the map on the screen.
52
+ def showMap (map ):
53
+ for row in map :
54
+ print (" " .join (str (cell ) for cell in row ))
55
+ print ("" )
56
+
57
+ # Check if player has won.
58
+ def checkWin (map ):
59
+ for row in map :
60
+ for cell in row :
61
+ if cell == '-' :
62
+ return False
63
+ return True
64
+
65
+ # Check if player is willing to continue the game.
66
+ def checkContinue (score ):
67
+ print ("Your score: " , score )
68
+ isContinue = input ("Do you want to try again? (y/n) :" )
69
+ if isContinue == 'n' :
70
+ return False
71
+ return True
72
+
73
+ # MINESWEEPER GAME
74
+ def Game ():
75
+
76
+ GameStatus = True
77
+ while GameStatus :
78
+ difficulty = input ("Select your difficulty (0,1,2):" )
79
+ if difficulty .lower () == '0' :
80
+ n = 5
81
+ k = 3
82
+ elif difficulty .lower () == '1' :
83
+ n = 6
84
+ k = 8
85
+ else :
86
+ n = 8
87
+ k = 20
88
+
89
+ minesweeper_map = randomMap (n , k )
90
+ player_map = playerMap (n )
91
+ score = 0
92
+
93
+ while True :
94
+
95
+ if checkWin (player_map ) == False :
96
+ print ("Enter the cell you want to open :" )
97
+ x ,y = map (int ,input ().split ())
98
+ x -= 1 # 0 based indexing
99
+ y -= 1 # 0 based indexing
100
+ if (minesweeper_map [y ][x ] == 'X' ):
101
+ print ("Game Over!" )
102
+ showMap (minesweeper_map )
103
+ GameStatus = checkContinue (score )
104
+ break
105
+ else :
106
+ player_map [y ][x ] = minesweeper_map [y ][x ]
107
+ showMap (player_map )
108
+ score += 1
109
+
110
+ else :
111
+ showMap (player_map )
112
+ print ("Congratulation! You Win!" )
113
+ GameStatus = checkContinue (score )
114
+ break
115
+
116
+ # Main Program
117
+ if __name__ == "__main__" :
118
+ try :
119
+ Game ()
120
+ except KeyboardInterrupt :
121
+ print ('\n Goodbye!' )
0 commit comments