Skip to content

Commit

Permalink
Feat: Ajout du mode chrono (#73)
Browse files Browse the repository at this point in the history
* Feat: Ajout du mode chrono

* chore: Modif of AUTHORS.md

---------

Co-authored-by: Soumaya Ikhrichi <[email protected]>
  • Loading branch information
IkhrichiSoumaya and Soumaya Ikhrichi authored Nov 28, 2024
1 parent 961036a commit 66a94d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
pip install pylint
- name: Analysing the code with pylint
run: |
pylint --max-attributes=11 --max-statements=56 $(git ls-files '*.py')
pylint --max-attributes=11 --max-statements=59 --max-args=7 --max-branches=13 --max-positional-arguments=7 $(git ls-files '*.py')
doc-build:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
- Sabrina, HASSAIM, [email protected]
- Maily, CIAVALDINI, [email protected]
- Mouheb, Felli, [email protected]
- Soumaya, IKHRICHI, [email protected]
56 changes: 46 additions & 10 deletions src/demineur.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def choisir_difficulte():
)

if niveau_difficulte == 'facile':
return 8, 10 # Taille, Nombre de mines
return 'facile',8, 10 # Taille, Nombre de mines
if niveau_difficulte == 'moyen':
return 10, 20
return 'moyen',10, 20
if niveau_difficulte == 'difficile':
return 16, 40
return 'difficile',16, 40

print("Vous avez choisi le niveau 'custom'.")
while True:
Expand All @@ -46,15 +46,15 @@ def choisir_difficulte():
except ValueError:
print("Erreur : Veuillez entrer un nombre valide.")

return grille_taille, mines_nombre
return niveau_difficulte, grille_taille, mines_nombre

class Demineur:
"""
Classe représentant le jeu de Démineur.
"""
def __init__(self, fichier_sauvegarde='demineur.json',
taille=10, nombre_mines=20,
exploratoire=False):
exploratoire=False,difficulte='moyen',mode_chronometre=False):
"""
Initialize the game with a grid and place mines based on parameters.
:param fichier_sauvegarde: Save file name.
Expand All @@ -66,6 +66,13 @@ def __init__(self, fichier_sauvegarde='demineur.json',
self.fichier_sauvegarde = fichier_sauvegarde
# Initialise l'attribut statistiques
self.statistiques = Statistiques() # Exemple, si `Statistiques` est une classe
self.mode_chronometre = mode_chronometre
self.temps_limite = {
'facile': 180,
'moyen': 300,
'difficile': 600,
'custom': 600 }[difficulte]
self.debut_partie = None
self.grille = [['■' for _ in range(self.taille)] for _ in range(self.taille)]
self.grille_visible = [['■' for _ in range(self.taille)] for _ in range(self.taille)]
self.__placer_mines()
Expand Down Expand Up @@ -132,7 +139,14 @@ def afficher_grille(self):
print(
f"\nMines restantes: {mines_restantes} | "
f"Mouvements: {self.mouvements} | "
f"Temps: {hours:02}:{minutes:02}:{seconds:02}" )
f"Temps: {hours:02}:{minutes:02}:{seconds:02}")

if self.mode_chronometre and self.debut_partie:
temps_restant = max(0, self.temps_limite - (time.time() - self.debut_partie))
minutes, seconds = divmod(int(temps_restant), 60)
print(f"⏳ Temps restant : {minutes:02}:{seconds:02}")
if temps_restant <= 30:
print("⚠️ Attention, il vous reste moins de 30 secondes !")

def charger_jeu(self):
"""Load the game state from a JSON file."""
Expand Down Expand Up @@ -188,6 +202,17 @@ def sauvegarder_jeu(self):
json.dump(data, file, ensure_ascii=False, indent=4)
print(f"Jeu sauvegardé dans {self.fichier_sauvegarde}.")

def verifier_temps(self):
"""
Check if the time limit has been reached.
"""
if self.mode_chronometre and self.debut_partie:
temps_ecoule = time.time() - self.debut_partie
if temps_ecoule >= self.temps_limite:
print("\n Temps écoulé ! Vous avez perdu.")
return True
return False

def traiter_choix(self, choix):
"""Used for player choice inputs"""
if choix[0].lower() == 'save':
Expand All @@ -202,12 +227,22 @@ def jouer(self):
"""A Function to launch the game."""
game_in_progress = True
self.statistiques.start_timer()
self.debut_partie = time.time() if self.mode_chronometre else None

while game_in_progress:
print("\n [ Bienvenue au Démineur ! ] \n")
self.afficher_grille()
print("Tapez 'save' pour sauvegarder la partie,"
"'help' pour une suggestion,"
"ou entrez les coordonnées.")
if self.verifier_temps():
game_in_progress = False
temps_ecoule = self.statistiques.stop_timer() # Arrêter le chronomètre
self.statistiques.record_loss() # Enregistrer la défaite
print("\n⏰ Temps écoulé ! Vous avez perdu. Temps "
f"joué : {temps_ecoule:.2f} secondes.")
break
print("Tapez 'save' pour sauvegarder la partie ou entrez les coordonnées.")
choix = input(
"Entrez 'f x y' pour marquer/démarquer, 'x y' pour découvrir, "
"'help' pour suggérer une case, "
Expand All @@ -229,7 +264,6 @@ def jouer(self):
except ValueError:
print("Coordonnées invalides. Veuillez réessayer.")
continue

if self.grille[y][x] == 'M':
self.grille_visible[y][x] = 'M'
self.afficher_grille()
Expand All @@ -253,7 +287,6 @@ def jouer(self):
break
print(f"Temps écoulé : {temps_ecoule:.2f} secondes")
self.statistiques.display_statistics()

# Demande si le joueur souhaite recommencer une partie
while True:
restart = input("Voulez-vous recommencer une partie ? (oui/non) : ").lower()
Expand All @@ -268,9 +301,12 @@ def jouer(self):

if __name__ == "__main__":
try:
real_taille, nb_mines = choisir_difficulte()
niv_difficulte, real_taille, nb_mines = choisir_difficulte()
mode_exploratoire = input("Activer le mode exploratoire ? (oui/non) : ").lower() == 'oui'
jeu = Demineur(taille=real_taille, nombre_mines=nb_mines,exploratoire=mode_exploratoire)
mode_chrono = input("Voulez-vous activer le mode chronométré ?"
"(oui/non) : ").strip().lower()
jeu = Demineur(taille=real_taille, nombre_mines=nb_mines,exploratoire=mode_exploratoire,
difficulte=niv_difficulte,mode_chronometre=mode_chrono == 'oui')
jeu.charger_jeu()
jeu.jouer()
except ValueError as e:
Expand Down

0 comments on commit 66a94d4

Please sign in to comment.