-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
333 lines (280 loc) · 11.6 KB
/
main.py
File metadata and controls
333 lines (280 loc) · 11.6 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
PyWordExplorer - Jeu de mots mêlés
Point d'entrée principal
"""
import sys
import os
import tkinter as tk
from src.solo.gui import WordSearchGUI
from src.solo.game_logic import GameLogic
from src.solo.save_manager import SaveManager
from src.word_generator import get_word_generator
def main():
"""Lance l'application avec l'interface graphique."""
try:
root = tk.Tk()
app = WordSearchGUI(root)
root.mainloop()
except KeyboardInterrupt:
print("\n\n👋 Au revoir!\n")
sys.exit(0)
except Exception as e:
print(f"Erreur: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
# =============================================================================
# ANCIENNE VERSION CONSOLE (conservée pour référence)
# =============================================================================
class ConsoleUI_OLD:
"""Interface utilisateur en mode console."""
def __init__(self):
word_gen = get_word_generator()
self.game = GameLogic(word_gen.get_words())
self.save_manager = SaveManager()
self.running = True
def clear_screen(self):
"""Efface l'écran de la console."""
os.system('cls' if os.name == 'nt' else 'clear')
def display_grid(self):
"""Affiche la grille de jeu."""
if not self.game.grid:
return
print("\n ", end="")
for i in range(len(self.game.grid[0])):
print(f" {i:2}", end="")
print("\n " + "---" * len(self.game.grid[0]))
for i, row in enumerate(self.game.grid):
print(f"{i:2} |", end="")
for cell in row:
print(f" {cell} ", end="")
print("|")
print(" " + "---" * len(self.game.grid[0]))
def display_game_info(self):
"""Affiche les informations de la partie."""
if not self.game.current_level:
return
print(f"\n{'='*60}")
print(f" NIVEAU {self.game.current_level.number} | Seed: {self.game.seed}")
print(f"{'='*60}")
remaining = self.game.get_remaining_time()
minutes = int(remaining // 60)
seconds = int(remaining % 60)
if remaining > 0:
print(f" ⏱️ Temps restant: {minutes:02d}:{seconds:02d}")
else:
print(f" ⏱️ TEMPS ÉCOULÉ!")
print(f" ✓ Mots trouvés: {len(self.game.found_words)}/{len(self.game.words_to_find)}")
print(f" 🎯 Score: {self.game.get_score()}")
print(f"{'='*60}\n")
def display_words_list(self):
"""Affiche la liste des mots à trouver."""
print("\nMots à trouver:")
print("-" * 40)
words_per_line = 4
words = [w['word'] for w in self.game.words_to_find]
for i in range(0, len(words), words_per_line):
line_words = words[i:i+words_per_line]
for word in line_words:
status = "✓" if word in self.game.found_words else " "
print(f" [{status}] {word:12}", end="")
print()
print()
def main_menu(self):
"""Affiche le menu principal."""
self.clear_screen()
print("\n" + "="*60)
print(" 🔤 PYWORDEXPLORER - Jeu de Mots Mêlés 🔤")
print("="*60)
print("\n1. Nouveau jeu")
print("2. Continuer la dernière partie")
print("3. Charger une partie")
print("4. Rejouer avec un seed")
print("5. Quitter")
print()
choice = input("Votre choix: ").strip()
if choice == "1":
self.new_game()
elif choice == "2":
self.continue_game()
elif choice == "3":
self.load_game_menu()
elif choice == "4":
self.replay_with_seed()
elif choice == "5":
self.running = False
else:
print("Choix invalide!")
input("\nAppuyez sur Entrée pour continuer...")
def new_game(self):
"""Démarre une nouvelle partie."""
self.clear_screen()
print("\n" + "="*60)
print(" NOUVEAU JEU")
print("="*60)
print("\nChoisissez un niveau (1-5):")
print(" 1. Débutant (8×8, 5 mots, 3 min)")
print(" 2. Facile (10×10, 7 mots, 4 min)")
print(" 3. Moyen (12×12, 9 mots, 5 min)")
print(" 4. Difficile (14×14, 11 mots, 6 min)")
print(" 5. Expert (16×16, 14 mots, 8 min)")
print()
try:
level = int(input("Niveau: ").strip())
info = self.game.start_level(level)
print(f"\nNiveau {level} démarré! (Seed: {info['seed']})")
input("\nAppuyez sur Entrée pour commencer...")
self.play_game()
except (ValueError, Exception) as e:
print(f"Erreur: {e}")
input("\nAppuyez sur Entrée pour continuer...")
def continue_game(self):
"""Continue la dernière partie sauvegardée."""
if not self.save_manager.has_autosave():
print("\nAucune sauvegarde automatique trouvée.")
input("\nAppuyez sur Entrée pour continuer...")
return
state = self.save_manager.load_game("autosave")
if state:
self.game.load_game_state(state)
print(f"\nPartie chargée! Niveau {state['level']}")
input("\nAppuyez sur Entrée pour continuer...")
self.play_game()
def load_game_menu(self):
"""Affiche le menu de chargement."""
self.clear_screen()
saves = self.save_manager.list_saves()
if not saves:
print("\nAucune sauvegarde trouvée.")
input("\nAppuyez sur Entrée pour continuer...")
return
print("\n" + "="*60)
print(" SAUVEGARDES DISPONIBLES")
print("="*60)
for i, save in enumerate(saves, 1):
print(f"\n{i}. {save['name']}")
print(f" Niveau: {save['level']} | Date: {save['timestamp'][:19]}")
print("\n0. Retour")
print()
try:
choice = int(input("Choisir une sauvegarde: ").strip())
if choice > 0 and choice <= len(saves):
save_name = saves[choice - 1]['name']
state = self.save_manager.load_game(save_name)
if state:
self.game.load_game_state(state)
print(f"\nPartie '{save_name}' chargée!")
input("\nAppuyez sur Entrée pour continuer...")
self.play_game()
except (ValueError, IndexError):
print("Choix invalide!")
input("\nAppuyez sur Entrée pour continuer...")
def replay_with_seed(self):
"""Rejoue une partie avec un seed spécifique."""
self.clear_screen()
print("\n" + "="*60)
print(" REJOUER AVEC UN SEED")
print("="*60)
try:
seed = int(input("\nEntrez le seed: ").strip())
level = int(input("Niveau (1-5): ").strip())
info = self.game.start_level(level, seed)
print(f"\nNiveau {level} démarré avec le seed {seed}!")
input("\nAppuyez sur Entrée pour commencer...")
self.play_game()
except (ValueError, Exception) as e:
print(f"Erreur: {e}")
input("\nAppuyez sur Entrée pour continuer...")
def play_game(self):
"""Boucle principale du jeu."""
while self.running and self.game.current_level:
self.clear_screen()
self.display_game_info()
self.display_grid()
self.display_words_list()
# Vérifier les conditions de fin
if self.game.is_level_complete():
self.level_complete()
return
if self.game.is_time_up():
self.game_over()
return
print("\nCommandes: [mot] pour trouver un mot | 'pause' | 'menu' | 'save'")
command = input(">>> ").strip().upper()
if command == "MENU":
self.save_manager.save_game(self.game.get_game_state(), "autosave")
print("\nPartie sauvegardée automatiquement.")
input("\nAppuyez sur Entrée pour continuer...")
return
elif command == "PAUSE":
self.pause_menu()
elif command == "SAVE":
save_name = input("Nom de la sauvegarde: ").strip()
if save_name:
self.save_manager.save_game(self.game.get_game_state(), save_name)
print(f"\nPartie sauvegardée sous '{save_name}'!")
input("\nAppuyez sur Entrée pour continuer...")
elif command:
if self.game.check_word(command):
print(f"\n✓ Bravo! '{command}' trouvé!")
else:
print(f"\n✗ '{command}' n'est pas dans la liste ou déjà trouvé.")
input("\nAppuyez sur Entrée pour continuer...")
def pause_menu(self):
"""Menu de pause."""
self.game.pause()
while True:
self.clear_screen()
print("\n" + "="*60)
print(" JEU EN PAUSE")
print("="*60)
print("\n1. Reprendre")
print("2. Sauvegarder")
print("3. Menu principal")
print()
choice = input("Votre choix: ").strip()
if choice == "1":
self.game.resume()
return
elif choice == "2":
save_name = input("Nom de la sauvegarde: ").strip()
if save_name:
self.save_manager.save_game(self.game.get_game_state(), save_name)
print(f"\nPartie sauvegardée sous '{save_name}'!")
input("\nAppuyez sur Entrée pour continuer...")
elif choice == "3":
self.save_manager.save_game(self.game.get_game_state(), "autosave")
self.game.resume()
return
def level_complete(self):
"""Affiche l'écran de niveau terminé."""
if not self.game.current_level:
return
self.clear_screen()
print("\n" + "="*60)
print(" 🎉 NIVEAU TERMINÉ! 🎉")
print("="*60)
print(f"\n Score final: {self.game.get_score()}")
print(f" Temps utilisé: {int(self.game.elapsed_time)}s")
print(f" Niveau: {self.game.current_level.number}")
print(f" Seed: {self.game.seed}")
if self.game.current_level.number < len(GameLogic.LEVELS):
print(f"\n Niveau suivant débloqué!")
print("\n" + "="*60)
input("\nAppuyez sur Entrée pour continuer...")
def game_over(self):
"""Affiche l'écran de game over."""
self.clear_screen()
print("\n" + "="*60)
print(" ⏱️ TEMPS ÉCOULÉ!")
print("="*60)
print(f"\n Mots trouvés: {len(self.game.found_words)}/{len(self.game.words_to_find)}")
print(f" Score: {self.game.get_score()}")
print(f" Seed: {self.game.seed}")
print("\n" + "="*60)
input("\nAppuyez sur Entrée pour continuer...")
def run(self):
"""Lance l'application."""
while self.running:
self.main_menu()
print("\n👋 Merci d'avoir joué! À bientôt!\n")