Identifying the win status
This commit is contained in:
21
game/game.py
21
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user