diff --git a/game/game.py b/game/game.py index 4f65528..adfee36 100644 --- a/game/game.py +++ b/game/game.py @@ -65,12 +65,26 @@ class Minesweeper: self.increment_safe_point(mine_position_row - 1, mine_position_col - 1) def is_mine(self, row, col): - """ Checks whether the given location is a mine or not """ + """ Checks whether the given location have a mine """ try: return self.board[row][col] == -1 except IndexError: return False + def is_empty(self, row, col): + """ Checks whether the given location is empty """ + try: + return self.board[row][col] == 0 + except IndexError: + return False + + def is_point(self, row, col): + """ Checks whether the given location have pontuation """ + try: + return self.board[row][col] > 0 + except IndexError: + return False + def is_point_in_board(self, row, col): """ Checks whether the location is inside board """ if row in range(0, self.rows) and col in range(0, self.cols): diff --git a/game/signals.py b/game/signals.py index b846395..5f83578 100644 --- a/game/signals.py +++ b/game/signals.py @@ -1,7 +1,8 @@ -from django.db.models.signals import post_save +from django.db.models.signals import post_save, pre_save from django.dispatch import receiver from .models import Game, GameEvent, EventTypes +from .game import Minesweeper @receiver(post_save, sender=Game) @@ -9,3 +10,26 @@ def game_start(sender, signal, instance, **kwargs): """ If the game was just created, insert the first event START_GAME """ GameEvent.objects.get_or_create(game=instance, type=EventTypes.START_GAME) + + +@receiver(pre_save, sender=GameEvent) +def identify_click_event(sender, signal, instance, **kwargs): + """ Verify what is on the naive click: mine, point or empty """ + if not instance.type == EventTypes.CLICK_NAIVE: + return + + ms = Minesweeper( + instance.game.rows, + instance.game.cols, + instance.game.mines, + instance.game.board, + ) + + if ms.is_mine(instance.metadata["row"], instance.metadata["col"]): + instance.type = EventTypes.CLICK_MINE + + elif ms.is_empty(instance.metadata["row"], instance.metadata["col"]): + instance.type = EventTypes.CLICK_EMPTY + + elif ms.is_point(instance.metadata["row"], instance.metadata["col"]): + instance.type = EventTypes.CLICK_POINT