-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase3.py
241 lines (213 loc) · 8.73 KB
/
phase3.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#En faite c'est encore la phase 2 parce que j'ai eu la flemme lmao
import random
import tkinter as tk
############################### Classes ###############################
class Case:
'''
Cette classe contient 4 attributs :
- mined : booléen indiquant si la case contient une mine ou non
- flagged : booléen indiquant si la case est flaggée ou non
- showed : booléen indiquand si la case est dévoilée ou non
- neighboor : int donnant le nombre de mines voisines à la case courante
'''
def __init__(self, mined, neighboor):
self.mined = mined
self.flagged = False
self.showed = False
if mined:
self.neighboor = 0
else:
self.neighboor = neighboor
def set_neighboor(self, neighboor):
self.neighboor = neighboor
def set_showed(self, showed):
self.showed = showed
class Demineur:
'''
Cette classe contient 4 attributs :
- finish : booleen indiquant si la partie est finie
- level : int indiquant le niveau du démineur (debutant, intermediaire, expert)
- playground: tableau a 2 dimensions de cases ou se passe le jeu
- flagging : booleen indiquant si on creuse ou on place un drapeau
==> False pour creuser ; True pour mettre un drapeau
'''
def __init__(self, level):
self.finish = False
self.level = level
self.playground = create_playground(level)
self.flagging = False
self.play()
def dig(self, coord):
x = coord[0]
y = coord[1]
sizex = len(self.playground[0])
sizey = len(self.playground)
if x>=0 and x<sizex and y>=0 and y<sizey and not self.playground[y][x].showed and not self.playground[y][x].flagged:
self.playground[y][x].set_showed(True)
if self.playground[y][x].mined:
print("You lose!")
self.finish = True
elif self.playground[y][x].neighboor == 0:
for j in range(y-1, y+2):
for i in range(x-1, x+2):
if i!=x or j!=y:
self.dig((i,j))
def flag(self, coord):
x = coord[0]
y = coord[1]
sizex = len(self.playground[0])
sizey = len(self.playground)
if x>=0 and x<sizex and y>=0 and y<sizey and not self.playground[y][x].showed:
self.playground[y][x].flagged = not self.playground[y][x].flagged
def play(self):
while not self.finish:
map = str()
for j in range(len(self.playground)):
line = str()
for i in range(len(self.playground[0])):
if not self.playground[j][i].showed:
if self.playground[j][i].flagged:
line = line + "! "
else:
line = line + ". "
else:
line = line + str(self.playground[j][i].neighboor) + " "
map = map + line + "\n"
print(map)
print()
if self.flagging:
print("Mode : Flagging")
else:
print("Mode : Digging")
ximp = input("Where? x = ")
yimp = input("Where? y = ")
while ximp == "":
ximp = input("Where? x = ")
while yimp == "":
yimp = input("Where? y = ")
if ximp == "d" or ximp == "D" or yimp == "d" or yimp == "D":
self.flagging = False
elif ximp == "f" or ximp == "F" or yimp == "f" or yimp == "F":
self.flagging = True
else:
x = int(ximp)
y = int(yimp)
if not self.flagging:
self.dig((x,y))
win = True
for i in range(len(self.playground)):
for j in range(len(self.playground[0])):
if not self.playground[j][i].mined and not self.playground[j][i].showed:
win = False
if win:
print("You win!")
self.finish = win or self.finish
else:
self.flag((x,y))
map = str()
for j in range(len(self.playground)):
line = str()
for i in range(len(self.playground[0])):
if not self.playground[j][i].showed:
line = line + "X "
else:
line = line + str(self.playground[j][i].neighboor) + " "
map = map + line + "\n"
print(map)
print("Thanks for playing!")
############################### Fonctions ###############################
def create_playground(level):
# Liste qui contiendra les coordonnées des cases minées
coord_mines = []
if level == -1: # Niveau test 5x5 2 mines
sizex = 5
sizey = 5
mines = 2
elif level == 0: # Niveau débutant 10x10 10 mines
sizex = 10
sizey = 10
mines = 10
elif level == 1: # Niveau intermédiaire 16x16 40 mines
sizex = 16
sizey = 16
mines = 40
else: # Niveau expert 16x30 99 mines
sizex = 30
sizey = 16
mines = 99
# Création d'un tableau de cases initialisées à 0 et False : playground[y][x]
playground = [[Case(False, 0) for i in range(sizex)] for j in range(sizey)]
# Détermination des cases minées
for k in range(mines):
x = random.randint(0,sizex-1)
y = random.randint(0,sizey-1)
while (x,y) in coord_mines: # Si on répète deux fois la meme coordonnée
x = random.randint(0,sizex-1) # On recrée un autre couple de coordonnées
y = random.randint(0,sizey-1)
coord_mines.append((x, y)) # On ajoute les coordonnées à la liste
# Placement des cases minées
for coord in coord_mines: # Pour chaque couple de coordonnées
x = coord[0] # On insère une case minée dans le playground
y = coord[1] # aux coordonnées données
playground[y][x] = Case(True, 0)
for j in range(len(playground)):
for i in range(len(playground[0])):
pmines = 0
for x in range(i-1, i+2):
for y in range(j-1, j+2):
if x != i or y != j:
if x>=0 and x<sizex and y>=0 and y<sizey:
if playground[y][x].mined:
pmines += 1
playground[j][i].set_neighboor(pmines)
return playground
def main():
Demineur(-1)
# dd = Demineur(0)
# di = Demineur(1)
# de = Demineur(2)
# print()
# print("Demineur test : ")
# print(" finish : " + str(dt.finish))
# print(" level : " + str(dt.level))
# case = dt.playground[0][0]
# print(" 1ère case : mined : " + str(case.mined))
# print(" 1ère case : flagged : " + str(case.flagged))
# print(" 1ère case : showed : " + str(case.showed))
# print(" 1ère case : neighboor : " + str(case.neighboor))
# print()
# print("Demineur intermediaire : ")
# print(" finish : " + str(di.finish))
# print(" level : " + str(di.level))
# case = di.playground[0][0]
# print(" 1ère case : mined : " + str(case.mined))
# print(" 1ère case : flagged : " + str(case.flagged))
# print(" 1ère case : showed : " + str(case.showed))
# print(" 1ère case : neighboor : " + str(case.neighboor))
# print()
# print("Demineur expert : ")
# print(" finish : " + str(de.finish))
# print(" level : " + str(de.level))
# case = de.playground[0][0]
# print(" 1ère case : mined : " + str(case.mined))
# print(" 1ère case : flagged : " + str(case.flagged))
# print(" 1ère case : showed : " + str(case.showed))
# print(" 1ère case : neighboor : " + str(case.neighboor))
# print()
# print("Demineur test : ")
# for j in range(len(dt.playground)):
# line = str()
# for i in range(len(dt.playground[0])):
# line = line + " " + str(dt.playground[j][i].mined)
# print(line)
# print()
# for j in range(len(dt.playground)):
# line = str()
# for i in range(len(dt.playground[0])):
# if not dt.playground[j][i].mined:
# line = line + " " + str(dt.playground[j][i].neighboor)
# else:
# line = line + " *"
# print(line)
if __name__ == "__main__":
main()