forked from amalkhatib90/Project01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executive.py
203 lines (187 loc) · 7.5 KB
/
executive.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
## @file executive.py
# Source file for the UI class
#
# Project: Minesweeper
# Author: Ethan Lefert
# Created: 09/08/18
from board import Board
## @class Executive
# @brief Handles user input and gameplay
class Executive:
## Constructor; initializes class variables
# @author: Ethan
def __init__(self):
## @var size
# stores the size of the board
self.size = 0
## @var mines
# stores the number of mines
self.mines = 0
## @var num_flags
# stores the number of flags
self.num_flags = 0
## @var game_over
# flag for game status
self.game_over = False
## @var grid
# empty grid
self.grid = [0][0]
## @var myBoard
# instance of the board class
self.myBoard = Board()
## Recursively calls reveal_adjacent() to uncover squares
# @authors: Ethan, Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def reveal(self, x, y):
if self.grid[x][y].num_adj_mines == 0:
self.reveal_adjacent(x - 1, y - 1)
self.reveal_adjacent(x - 1, y)
self.reveal_adjacent(x - 1, y + 1)
self.reveal_adjacent(x + 1, y)
self.reveal_adjacent(x, y - 1)
self.reveal_adjacent(x, y + 1)
self.reveal_adjacent(x + 1, y - 1)
self.reveal_adjacent(x + 1, y + 1)
else:
self.grid[x][y].is_revealed = True
## Checks that coordinates are within bounds of board
# @author: Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def is_valid_cell(self, x, y):
if 0 <= x < self.size and 0 <= y < self.size:
return True
return False
## Reveals cells that aren't mines; Called by reveal()
# @authors: Ethan, Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def reveal_adjacent(self, x, y):
if not self.is_valid_cell(x, y):
return
if self.grid[x][y].is_revealed or self.grid[x][y].is_flagged:
return
if self.grid[x][y].num_adj_mines == 0:
self.grid[x][y].is_revealed = True
self.reveal(x, y)
else:
if not self.grid[x][y].is_mine:
self.grid[x][y].is_revealed = True
return
## Checks that coordinates are within bounds of board
# @author: Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def is_valid_cell(self, x, y):
if 0 <= x < self.size and 0 <= y < self.size:
return True
return False
## Checks if all mines are flagged
# @author: Ethan
# @post: game_over flag has been updated
def check_win(self):
flag_on_mine = 0
for i in range(0, self.size):
for x in range(0, self.size):
if self.grid[i][x].is_mine and self.grid[i][x].is_flagged:
flag_on_mine += 1
if flag_on_mine == self.mines:
print("You Win!")
self.game_over = True
else:
return 0
## Generates board with user input for mines and size
# @author: Ethan
# @post: Board is generated based on user input
def setup(self):
while True:
try:
board_size_select = int(input("Please enter the board size between 2 and 15: "))
except ValueError:
print("That\'s not a number!")
else:
if 2 <= board_size_select <= 15:
self.size = board_size_select
break
else:
print('Not a valid board size. Try again')
max_mines = self.size * self.size - 1
while True:
try:
mine_num_select = int(
input("Enter the number of mines, it should be between 1 and " + str(max_mines) + ": "))
except ValueError:
print("That\'s not a number!")
else:
if 1 <= mine_num_select <= max_mines:
self.mines = mine_num_select
break
else:
print('Not a valid amount of mines. Try again')
self.num_flags = self.mines
self.grid = self.myBoard.make_grid(self.size)
self.myBoard.generate_mines(self.mines, self.size, self.grid)
self.myBoard.mine_check(self.size, self.grid)
## Takes coordinates from user and handles input
# @pre: Board has been setup
# @author: Ethan
def play(self):
while not self.game_over:
self.myBoard.print_board(self.size, self.grid)
print("Number of flags: %s" % self.num_flags)
while True:
try:
x = int(input("Enter a Y coordinate: "))
if x < 0 or x > self.size - 1:
print("Invalid input. Try again.")
else:
break
except ValueError:
print("That\'s not an integer. Try again.")
while True:
try:
y = int(input("Enter an X coordinate: "))
if y < 0 or y > self.size - 1:
print("Invalid input. Try again.")
else:
break
except ValueError:
print("That\'s not an integer. Try again.")
choice = input("Enter an action flag [f], reveal [r], unflag [n]: ")
if x > self.size - 1 or y > self.size - 1:
print("Invalid try again")
elif choice != "f" and choice != "n" and choice != "r":
print("Invalid choice try again")
elif not self.grid[x][y].is_flagged and choice == "n":
print("Invalid try again")
elif not self.grid[x][y].is_flagged and self.num_flags == 0 and choice == "f":
print("Out of flags. Try again.")
elif self.grid[x][y].is_flagged and choice == "f":
print("Space is already flagged. Try again.")
elif self.grid[x][y].is_revealed and choice == "f":
print("You can't flag a revealed space. Try again.")
elif self.grid[x][y].is_revealed and choice == "n":
print("You can't unflag a revealed space. Try again.")
elif self.grid[x][y].is_flagged and choice == "n":
self.grid[x][y].is_flagged = False
self.num_flags += 1
elif not self.grid[x][y].is_flagged and choice == "f":
self.grid[x][y].is_flagged = True
self.num_flags -= 1
self.check_win()
#Testing to see if is_revealed is being switched to true
elif self.grid[x][y].is_revealed and not self.grid[x][y].is_mine and choice == "r":
print("Space is already revealed. Try again.")
elif self.grid[x][y].is_flagged and choice == "r":
print("You can't reveal a flagged space. Unflag before guessing this space or guess a different space.")
elif self.grid[x][y].is_mine and choice == "r":
print("Game Over")
self.game_over = True
else:
self.reveal(x, y)
for i in range(0, self.size):
for j in range(0, self.size):
self.grid[i][j].is_revealed = True
self.grid[i][j].num_adj_mines = False
self.myBoard.print_board(self.size, self.grid)