From 7e8f65a5b73065134e0b76dcd25d68c7a68d3170 Mon Sep 17 00:00:00 2001 From: Michel Wilhelm Date: Sat, 7 Nov 2020 00:11:05 -0300 Subject: [PATCH] Identifying the win status --- game/game.py | 21 ++++++++++++++++++++- game/models.py | 1 + game/signals.py | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/game/game.py b/game/game.py index adfee36..02b98a5 100644 --- a/game/game.py +++ b/game/game.py @@ -4,7 +4,7 @@ import random class Minesweeper: board = [] - def __init__(self, rows=10, cols=10, mines=5, board=None): + def __init__(self, rows=10, cols=10, mines=5, board=None, board_progress=None): self.rows = rows self.cols = cols self.mines = mines @@ -12,9 +12,15 @@ class Minesweeper: if board is not None: self.board = board + if board_progress is not None: + self.board_progress = board_progress + def create_board(self): """ Creating the board cells with 0 as default value """ self.board = [[0 for col in range(self.cols)] for row in range(self.rows)] + self.board_progress = [ + ["-" for col in range(self.cols)] for row in range(self.rows) + ] def put_mine(self): """Put a single mine on the board. @@ -104,3 +110,16 @@ class Minesweeper: # Increment the value of the position becaus is close to some mine self.board[row][col] += 1 + + def reveal(self, row, col): + self.board_progress[row][col] = self.board[row][col] + + def win(self): + """ Identify if the player won the game """ + unrevealed = 0 + for row in self.board_progress: + for cell in row: + if cell == "-": + unrevealed += 1 + if (unrevealed - self.mines) == 0: + return True diff --git a/game/models.py b/game/models.py index f9726b0..f8f56ce 100644 --- a/game/models.py +++ b/game/models.py @@ -67,6 +67,7 @@ class Game(models.Model): ms.create_board() ms.put_mines() self.board = ms.board + self.progress_board = ms.progress_board super(Game, self).save(*args, **kwargs) diff --git a/game/signals.py b/game/signals.py index f999f41..f1c1d7a 100644 --- a/game/signals.py +++ b/game/signals.py @@ -41,6 +41,25 @@ def identify_click_event(sender, signal, instance, **kwargs): @receiver(post_save, sender=GameEvent) def create_post_save_game_event(sender, signal, instance, **kwargs): + ms = Minesweeper( + instance.game.rows, + instance.game.cols, + instance.game.mines, + instance.game.board, + instance.game.board_progress, + ) + game_changed = False + + reveal_events = [ + EventTypes.CLICK_POINT, + EventTypes.CLICK_EMPTY, + EventTypes.CLICK_MINE, + ] + if instance.type in reveal_events: + ms.reveal(instance.row, instance.col) + instance.game.board_progress = ms.board_progress + game_changed = True + playing_events = [ EventTypes.START_GAME, EventTypes.RESUME, @@ -51,15 +70,23 @@ def create_post_save_game_event(sender, signal, instance, **kwargs): if instance.type in playing_events: instance.game.status = GameStatuses.PLAYING - instance.game.save() + game_changed = True - if instance.type == EventTypes.PAUSE: + elif instance.type == EventTypes.PAUSE: instance.game.status = GameStatuses.PAUSED - instance.game.save() + game_changed = True - if instance.type == EventTypes.CLICK_MINE: + elif instance.type == EventTypes.CLICK_MINE: instance.game.status = GameStatuses.FINISHED instance.game.win = False - instance.game.save() + game_changed = True GameEvent(game=instance.game, type=EventTypes.GAME_OVER).save() + + if ms.win() is True: + instance.game.status = GameStatuses.FINISHED + instance.game.win = True + game_changed = True + + if game_changed is True: + instance.game.save()