Skip to content

Commit

Permalink
feat: Add statistics at the bottom of the grid and movement counter
Browse files Browse the repository at this point in the history
  • Loading branch information
minhquangcao committed Nov 20, 2024
1 parent b6d8ffb commit 2abae22
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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 $(git ls-files '*.py')
pylint --max-attributes=8 $(git ls-files '*.py')
doc-build:
runs-on: ubuntu-latest
Expand Down
20 changes: 20 additions & 0 deletions src/demineur.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import json
import os
import time
from statistiques import Statistiques

class Demineur:
Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(self, fichier_sauvegarde='demineur.json', difficulte='moyen'):
self.marques = set()
self.__placer_mines()
self.__calculer_indices()
self.mouvements = 0

def __placer_mines(self):
mines_placees = 0
Expand Down Expand Up @@ -69,6 +71,8 @@ def decouvrir_cases(self, x, y):
if self.grille_visible[y][x] != '■':
return
self.grille_visible[y][x] = self.grille[y][x]
self.mouvements += 1

if self.grille[y][x] == '0':
self.decouvrir_cases(x - 1, y)
self.decouvrir_cases(x + 1, y)
Expand All @@ -81,6 +85,22 @@ def afficher_grille(self):
for idx, ligne in enumerate(self.grille_visible):
print(f"{idx:2}| " + ' '.join(ligne) + " |")

mines_restantes = self.nombre_mines - sum(row.count('M') for row in self.grille_visible)

if self.statistiques.timer_start:
temps_ecoule = int(time.time() - self.statistiques.timer_start)
else:
temps_ecoule = 0

hours, remainder = divmod(temps_ecoule, 3600)
minutes, seconds = divmod(remainder, 60)

print(
f"\nMines restantes: {mines_restantes} | "
f"Mouvements: {self.mouvements} | "
f"Temps: {hours:02}:{minutes:02}:{seconds:02}"
)

def charger_jeu(self):
"""
Load the game state from a JSON file.
Expand Down

0 comments on commit 2abae22

Please sign in to comment.