Added signal for identify what is on the click location
This commit is contained in:
16
game/game.py
16
game/game.py
@@ -65,12 +65,26 @@ class Minesweeper:
|
|||||||
self.increment_safe_point(mine_position_row - 1, mine_position_col - 1)
|
self.increment_safe_point(mine_position_row - 1, mine_position_col - 1)
|
||||||
|
|
||||||
def is_mine(self, row, col):
|
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:
|
try:
|
||||||
return self.board[row][col] == -1
|
return self.board[row][col] == -1
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return False
|
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):
|
def is_point_in_board(self, row, col):
|
||||||
""" Checks whether the location is inside board """
|
""" Checks whether the location is inside board """
|
||||||
if row in range(0, self.rows) and col in range(0, self.cols):
|
if row in range(0, self.rows) and col in range(0, self.cols):
|
||||||
|
|||||||
@@ -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 django.dispatch import receiver
|
||||||
|
|
||||||
from .models import Game, GameEvent, EventTypes
|
from .models import Game, GameEvent, EventTypes
|
||||||
|
from .game import Minesweeper
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Game)
|
@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 """
|
""" If the game was just created, insert the first event START_GAME """
|
||||||
|
|
||||||
GameEvent.objects.get_or_create(game=instance, type=EventTypes.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
|
||||||
|
|||||||
Reference in New Issue
Block a user