-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedenimeur.py
executable file
·254 lines (221 loc) · 7.48 KB
/
dedenimeur.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
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TODO
# facile / dur
import random
import sys
from PyQt5.QtCore import QBasicTimer, QElapsedTimer, Qt
from PyQt5.QtWidgets import (
QAction,
QApplication,
QGridLayout,
QInputDialog,
QLCDNumber,
QMainWindow,
QMessageBox,
QPushButton,
QVBoxLayout,
QWidget,
qApp,
)
COLORS = [
"rgba(0, 0, 0, 0)",
"blue",
"green",
"red",
"DarkBlue",
"black",
"DeepPink",
"violet",
"brown",
]
class Board(QWidget):
def __init__(self, height, width, mines, size, *args, **kwargs):
super(Board, self).__init__(*args, **kwargs)
self.size, self.height, self.width, self.mines = size, height, width, mines
self.grid = QGridLayout()
self.grid.setHorizontalSpacing(0)
self.grid.setVerticalSpacing(0)
self.setLayout(self.grid)
self.buttons = {}
self.init()
def init(self):
for btn in self.buttons.values():
btn.setParent(None)
self.buttons = {}
for x in range(self.width):
for y in range(self.height):
btn = QPushButton(" ")
btn.position = (x, y)
btn.bomb = False
btn.setFixedSize(self.size, self.size)
btn.clicked.connect(self.click)
btn.setContextMenuPolicy(Qt.CustomContextMenu)
btn.customContextMenuRequested.connect(self.right_click)
self.grid.addWidget(btn, y, x)
self.buttons[(x, y)] = btn
for mine in range(self.mines):
while True:
x, y = random.randrange(self.width), random.randrange(self.height)
if not self.buttons[(x, y)].bomb:
self.buttons[(x, y)].bomb = True
break
self.started = False
def click(self):
if not self.started:
self.parent().parent().start_timers()
self.started = True
self.demine(self.sender(), force=self.sender().isFlat())
self.check_victory()
def right_click(self):
btn = self.sender()
if not btn.isFlat():
btn.setText("!" if btn.text() == " " else " ")
def around(self, x, y):
for i in [-1, 0, 1]:
if 0 <= x + i < self.width:
for j in [-1, 0, 1]:
if 0 <= y + j < self.height:
yield x + i, y + j
def demine(self, btn, force=False):
if not force and btn.isFlat() or btn.text() == "!":
return True
if force and btn.isFlat():
n = 0
for x, y in self.around(*btn.position):
if self.buttons[(x, y)].isFlat():
continue
if self.buttons[(x, y)].bomb:
n += 1
if self.buttons[(x, y)].text() == "!":
n -= 1
if n != 0:
return False
btn.setFlat(True)
x, y = btn.position
if btn.bomb:
self.end()
return False
n = 0
for u, v in self.around(x, y):
if self.buttons[(u, v)].bomb:
n += 1
btn.setText(str(n))
btn.setStyleSheet("color: %s;" % COLORS[n])
if n == 0 or force:
for u, v in self.around(x, y):
if not self.demine(self.buttons[(u, v)]):
return False
return True
def check_victory(self):
for btn in self.buttons.values():
if not btn.bomb and not btn.isFlat():
return
for x in range(self.width):
for y in range(self.height):
if self.buttons[(x, y)].bomb:
self.buttons[(x, y)].setText("\\o/")
QMessageBox.question(
self,
"Fin",
"Gagné en %ims!" % self.parent().parent().stop_timers(),
QMessageBox.Ok,
)
self.init()
def end(self):
for x in range(self.width):
for y in range(self.height):
if self.buttons[(x, y)].bomb:
self.buttons[(x, y)].setText("/o\\")
self.parent().parent().stop_timers()
QMessageBox.question(self, "Fin", "Perdu !", QMessageBox.Ok)
self.init()
class DedeNimeur(QMainWindow):
def __init__(self):
super(DedeNimeur, self).__init__()
self.statusBar()
self.size, self.height, self.width, self.mines = 30, 10, 10, 10
self.lcd = QLCDNumber()
self.lcd.setFixedSize(300, 60)
self.board = Board(self.height, self.width, self.mines, self.size)
self.timer = QBasicTimer()
self.real_timer = QElapsedTimer()
vbox = QVBoxLayout()
vbox.addWidget(self.lcd)
vbox.addWidget(self.board)
central = QWidget()
central.setLayout(vbox)
self.setCentralWidget(central)
start = QAction("Start", self)
start.setStatusTip("Start")
start.setShortcut("Ctrl+N")
start.triggered.connect(self.init)
exit = QAction("Exit", self)
exit.setStatusTip("Exit")
exit.setShortcut("Ctrl+Q")
exit.triggered.connect(qApp.quit)
height = QAction("Height", self)
height.setStatusTip("Set board width")
height.triggered.connect(self.set_height)
width = QAction("Width", self)
width.setStatusTip("Set board height")
width.triggered.connect(self.set_width)
mines = QAction("Mines", self)
mines.setStatusTip("Set board mines")
mines.triggered.connect(self.set_mines)
size = QAction("Size", self)
size.setStatusTip("Set button size")
size.triggered.connect(self.set_size)
toolbar = self.addToolBar("Toolbar")
toolbar.addAction(start)
toolbar.addAction(width)
toolbar.addAction(height)
toolbar.addAction(mines)
toolbar.addAction(size)
toolbar.addAction(exit)
self.setWindowTitle("DédéNimeur")
self.show()
def init(self):
if self.mines < self.height * self.width:
self.board.height = self.height
self.board.width = self.width
self.board.mines = self.mines
self.board.size = self.size
self.board.init()
else:
QMessageBox.question(
self, "NOPE", "Va falloir spécifier un truc cohérent…", QMessageBox.Ok
)
def set_height(self):
text, ok = QInputDialog.getText(self, "Settings", "height")
if ok:
self.height = int(text)
self.init()
def set_width(self):
text, ok = QInputDialog.getText(self, "Settings", "width")
if ok:
self.width = int(text)
self.init()
def set_mines(self):
text, ok = QInputDialog.getText(self, "Settings", "mines")
if ok:
self.mines = int(text)
self.init()
def set_size(self):
text, ok = QInputDialog.getText(self, "Settings", "size")
if ok:
self.size = int(text)
self.init()
def start_timers(self):
self.timer.start(100, self)
self.real_timer.start()
self.lcd.display(int(self.real_timer.elapsed() / 1000))
def stop_timers(self):
self.timer.stop()
return self.real_timer.elapsed()
def timerEvent(self, e):
self.lcd.display(int(self.real_timer.elapsed() / 1000))
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = DedeNimeur()
sys.exit(app.exec_())