-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmymine.py
154 lines (132 loc) · 4.79 KB
/
mymine.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
import random
MINE_CHANCE = 10
GRID_SIZE = 9
def placeTrap():#Places mine in board, 1 in GRID_SIZE chance
board = []
row = []
for y in range(GRID_SIZE):
row = []
for x in range(GRID_SIZE):
num = random.randint(1,MINE_CHANCE)
if num != MINE_CHANCE:
row.append('')
else:
row.append('x')
board.append(row)
return board
def createBoard():#Makes a 2D list for a board filled with #'s
board = []
row = []
for y in range(GRID_SIZE):
row = []
for x in range(GRID_SIZE):
row.append('#')
board.append(row)
return board
def displayBoard(lis):#Prints out board
output = " |"#Start of the first line
for x in range(GRID_SIZE):
output+=str(x) #Adds the Column numbers
print(output)
print("---"+"-"*GRID_SIZE) #Seperates top numbers from the grid
for x in range(len(lis)):
output2 = ""
output2+=str(x) + "|" #Seperation between columns and rows
for y in range(len(lis[x])):
if lis[x][y] == '':
output2+= " "
elif lis[x][y] == 'x':
output2+= lis[x][y]
else:
output2+= lis[x][y]
print(output2)
def countHiddenSquares(lis):#Counts #'s in board.
total = 0
for x in range(len(lis)):
for y in range(len(lis[x])):
if lis[x][y] == "#":
total+=1
return total
def countAllTraps(lis):#Counts all mines in board.
total = 0
for x in range(len(lis)):
for y in range(len(lis[x])):
if lis[x][y] == "x":
total+=1
else:
continue
return total
def isTrapAt(lis,row,col):#Checks if a mine is at a given cell
try:
if lis[row][col] == 'x':
return True
else:
return False
except:
return False
def countAdjacentTraps(lis,row,col):#Validates input around the given cell to make sure it does not overlap the list and returns a number
total = 0
while isTrapAt(lis,row,col) is False:
if ((row > 0 and col > 0) and lis[row-1][col-1] == 'x'):
total+=1
if (row > 0 and lis[row-1][col] == 'x'):
total+=1
if ((row > 0 and col < len(lis)-1) and lis[row-1][col+1] == 'x'):
total+=1
if (col > 0 and lis[row][col-1] == 'x'):
total+=1
if (col < len(lis)-1 and lis[row][col+1] == 'x'):
total+=1
if ((row < len(lis)-1 and col > 0) and lis[row+1][col-1] == 'x'):
total+=1
if (row < len(lis)-1 and lis[row+1][col] == 'x'):
total+=1
if ((row < len(lis)-1 and col < len(lis)-1) and lis[row+1][col+1] == 'x'):
total+=1
return total
def gameLoop():#Game loop
board = createBoard()
mines = placeTrap()
move = []
displayBoard(board)#Shows first empty board
while True:
while move == []:
try:
move = [int(s) for s in input("Select a cell (row,col) > ").split(",")] #Gets input of 2 integers in form x,y
x = move[0]#Integer 1
y = move[1]#Integer 2
except:
move = []
if isTrapAt(mines,x,y) is True: #If you hit a mine entire board is revealed and you lose
for i in range(len(mines)):
for n in range(len(mines)):
if mines[i][n] == 'x':#Goes through board and replaces hidden mines with an x
board[i][n] = 'x'
displayBoard(board)
print("Game Over!")
break
showCells(board,mines,x,y)
if countAllTraps(mines) == countHiddenSquares(board): #Checks if you won, hidden cells is the same number as mines
print("You Win!!")
break
move = []
displayBoard(board)
def showCells(board,mines,x,y):#reveals cells if they haven't hit a MINE
#input validation to see if x and y are in range
gameboard = board
mineboard = mines
if x < 0 or x > GRID_SIZE - 1 or y > GRID_SIZE - 1 or y < 0:
return
#If a cell is empty, it means its already checked
if gameboard[x][y] == " ":
return
numMines = countAdjacentTraps(mineboard,x,y)#Counts number of mines around the cell
if numMines > 0:#Mines are present
gameboard[x][y] = str(numMines)#Mutates cell to be the numMines
else:#Mines are not present
gameboard[x][y] = ' '#Mutates to an empty cell
showCells(gameboard,mines,x+1,y)
showCells(gameboard,mines,x-1,y)#Goes through the left,right,up and down of the (nest line)
showCells(gameboard,mines,x,y-1)#cell to check for empty spaces/mines
showCells(gameboard,mines,x,y+1)
gameLoop()