Basic game creating API (#2)
* Adding the base of our API * Little file and lint adjustments * Adding the lint command to Makefile * Adding the Minesweeper logic for game creation * Adding some tests for the Minesweeper algorithm * Adding some tools command to Makefile like pre-commit and pip-tools * Adding test help text to Makefile * all new user is_staff=True, for development for now * Now we can get the data from specific game Adding game status Adding game status Fixing game models
This commit is contained in:
89
game/game.py
Normal file
89
game/game.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import random
|
||||
|
||||
|
||||
class Minesweeper:
|
||||
board = []
|
||||
|
||||
def __init__(self, rows=10, cols=10, mines=5):
|
||||
self.rows = rows
|
||||
self.cols = cols
|
||||
self.mines = mines
|
||||
|
||||
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)]
|
||||
|
||||
def put_mine(self):
|
||||
"""Put a single mine on the board.
|
||||
The mine have a -1 value just for reference
|
||||
"""
|
||||
mine_position_row = random.randrange(0, self.rows)
|
||||
mine_position_col = random.randrange(0, self.cols)
|
||||
|
||||
if self.is_mine(mine_position_row, mine_position_col):
|
||||
self.put_mine()
|
||||
|
||||
self.board[mine_position_row][mine_position_col] = -1
|
||||
return mine_position_row, mine_position_col
|
||||
|
||||
def put_mines(self):
|
||||
""" Put the desired amount of mines on the board """
|
||||
for mine in range(1, self.mines + 1):
|
||||
mine_position_row, mine_position_col = self.put_mine()
|
||||
|
||||
self.create_mine_points(mine_position_row, mine_position_col)
|
||||
|
||||
def create_mine_points(self, mine_position_row, mine_position_col):
|
||||
"""Populate the board with points that sorrounds the mine.
|
||||
The reference used is the mine that was already placed"""
|
||||
|
||||
# North
|
||||
self.increment_safe_point(mine_position_row - 1, mine_position_col)
|
||||
|
||||
# North-east
|
||||
self.increment_safe_point(mine_position_row - 1, mine_position_col + 1)
|
||||
|
||||
# East
|
||||
self.increment_safe_point(mine_position_row, mine_position_col + 1)
|
||||
|
||||
# South-east
|
||||
self.increment_safe_point(mine_position_row + 1, mine_position_col + 1)
|
||||
|
||||
# South
|
||||
self.increment_safe_point(mine_position_row + 1, mine_position_col)
|
||||
|
||||
# South-west
|
||||
self.increment_safe_point(mine_position_row + 1, mine_position_col - 1)
|
||||
|
||||
# West
|
||||
self.increment_safe_point(mine_position_row, mine_position_col - 1)
|
||||
|
||||
# North-west
|
||||
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 """
|
||||
try:
|
||||
return self.board[row][col] == -1
|
||||
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):
|
||||
return True
|
||||
return False
|
||||
|
||||
def increment_safe_point(self, row, col):
|
||||
""" Creates the mine's pontuation frame """
|
||||
|
||||
# Ignores if the point whether not in the board
|
||||
if not self.is_point_in_board(row, col):
|
||||
return
|
||||
|
||||
# Verify if the position have a mine on it
|
||||
if self.is_mine(row, col):
|
||||
return
|
||||
|
||||
# Increment the value of the position becaus is close to some mine
|
||||
self.board[row][col] += 1
|
||||
Reference in New Issue
Block a user