Creating structure for the game events (#3)
* Adding the GameVent model and migration * Initializing the GameEvent endpoint * GameEvent listing endpoint * Adding CLICK_NAIVE event * Added signal for identify what is on the click location * Using row and col integer is better than metadata with jsonfield * Create event for the same position is not allowed * Better signals control * Adding a board progress to Game model * Identifying the win status * Hide generated board from client
This commit is contained in:
40
game/game.py
40
game/game.py
@@ -4,14 +4,23 @@ import random
|
||||
class Minesweeper:
|
||||
board = []
|
||||
|
||||
def __init__(self, rows=10, cols=10, mines=5):
|
||||
def __init__(self, rows=10, cols=10, mines=5, board=None, board_progress=None):
|
||||
self.rows = rows
|
||||
self.cols = cols
|
||||
self.mines = mines
|
||||
|
||||
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.
|
||||
@@ -62,12 +71,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):
|
||||
@@ -87,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
|
||||
|
||||
Reference in New Issue
Block a user