-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_mechanics.py
More file actions
189 lines (134 loc) · 4.92 KB
/
game_mechanics.py
File metadata and controls
189 lines (134 loc) · 4.92 KB
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
#minesweep round 2
import random
import numpy as np
class Cell:
def __init__(self, mine=False, show=False, score=0, mark=0):
self.mine = mine
self.show = show
self.score = score
self.mark = mark
def __repr__(self):
return f"Cell({self.mine},{self.show},{self.score},{self.mark})"
class Game:
def __init__(self, difficulty):
match difficulty:
case 0:
print("easy")
case 1:
print("medium")
case 2:
print("hard")
case _:
print("default")
self.x_width = 20
self.y_height = 20
self.mine_min = 50
self.mine_max = 100
self.rows = 20
self.cols = 20
self.map_array_1 = [[Cell() for _ in range(self.cols)] for _ in range (self.rows)]
self.mine_count = random.randint(50,100)
def check_neighbors(self, x,y):
score = 0
score += self.local_check((x-1), (y-1))
score += self.local_check(x, (y-1))
score += self.local_check((x+1), (y-1))
score += self.local_check((x-1), y)
score += self.local_check((x+1), y)
score += self.local_check((x-1), (y+1))
score += self.local_check(x, (y+1))
score += self.local_check((x+1), (y+1))
return score
def local_check(self, x,y):
if x < 0 or x > (self.x_width -1):
return False
if y < 0 or y > (self.y_height -1):
return False
if self.map_array_1[x][y].mine == True:
return True
else:
return False
def fill_map(self, mine_count):
i = 0
while i < mine_count:
x_random = random.randint(0,(self.x_width - 1))
y_random = random.randint(0,(self.y_height - 1))
if self.map_array_1[x_random][y_random].mine == False:
self.map_array_1[x_random][y_random].mine = True
i += 1
def setup_maps(self):
print("for map cell array: x ", self.x_width, " y", self.y_height)
mine_count = random.randint(self.mine_min,self.mine_max)
self.fill_map(mine_count)
for j in range(self.y_height):
for i in range(self.x_width):
self.map_array_1[i][j].score = self.check_neighbors(i,j)
def print_map_details(self):
for j in range(self.y_height):
for i in range(self.x_width):
if self.map_array_1[i][j].mine == True:
print("x", end="")
else:
print("-", end="")
print(" |")
print(" finished printing map with locations")
print("")
for j in range(self.y_height):
for i in range(self.x_width):
score = self.check_neighbors(i,j)
self.map_array_1[i][j].score = score
print(score, end="")
print(" |")
def print_map(self):
for j in range(self.y_height):
for i in range(self.x_width):
if self.map_array_1[i][j].show == True:
print(self.map_array_1[i][j].score, end="")
else:
print("-", end="")
print(" |")
def reveal_spread(self, x,y,counter):
if x < 0 or x >= self.x_width:
return
if y < 0 or y >= self.y_height:
return
counter -= 1
if counter <= 0:
return
if self.map_array_1[x][y].show == False:
if self.map_array_1[x][y].mine == False:
self.map_array_1[x][y].show = True
self.reveal_spread(x-1,y, counter)
self.reveal_spread(x+1,y, counter)
self.reveal_spread(x,y-1, counter)
self.reveal_spread(x,y+1, counter)
def game_loop(self):
player_lives = 2
while(player_lives > 0):
self.print_map()
number_valid = False
while number_valid == False:
x_move = int(input("x: "))
y_move = int(input("y: "))
print("x is : ", x_move, "y is :", y_move)
if x_move >= 0 and x_move < self.x_width:
if y_move >= 0 and y_move < self.y_height:
number_valid = True
if self.map_array_1[x_move][y_move].mine == True:
print("player lost")
exit()
else:
#reveal_array[x_move][y_move] = True
print("good choice")
self.reveal_spread(x_move,y_move,4)
def main():
input_check = False
while input_check == False:
user_input = int(input("choose difficulty 0 easy 1 medium 2 hard:"))
if 0 <= user_input <= 2:
input_check = True
new_game = Game(user_input)
new_game.setup_maps()
new_game.print_map_details()
new_game.game_loop()
main()