Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Last active June 2, 2017 20:47
Show Gist options
  • Save tyler-austin/88370d0fb36dca9df68f60645da269b9 to your computer and use it in GitHub Desktop.
Save tyler-austin/88370d0fb36dca9df68f60645da269b9 to your computer and use it in GitHub Desktop.
Code Fights - minesweeper

In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

Example

For

matrix = [[true, false, false],
          [false, true, false],
          [false, false, false]]

the output should be

minesweeper(matrix) = [[1, 2, 1],
                       [2, 1, 1],
                       [1, 1, 1]]       

Input/Output

  • [time limit] 4000ms (py3)

  • [input] array.array.boolean matrix

    A non-empty rectangular matrix consisting of boolean values - true if the corresponding cell contains a mine, false otherwise.

    Guaranteed constraints:

    2 ≤ matrix.length ≤ 5,
    2 ≤ matrix[0].length ≤ 5.
    
  • [output] array.array.integer

    Rectangular matrix of the same size as matrix each cell of which contains an integer equal to the number of mines in the neighboring cells. Two cells are called neighboring if they share at least one corner.

from typing import List
class Minesweeper:
@classmethod
def minesweeper(cls, matrix: List[List[bool]]) -> List[List[int]]:
result = []
for x, row in enumerate(matrix):
new_row = []
for y, col in enumerate(row):
num_mines = cls._num_neighbor_mines(matrix, x, y)
new_row.append(num_mines)
result.append(new_row)
return result
@classmethod
def _num_neighbor_mines(cls, matrix: List[List[bool]], x: int, y: int) -> int:
up = cls._is_mine(matrix, x - 1, y)
up_right = cls._is_mine(matrix, x - 1, y + 1)
right = cls._is_mine(matrix, x, y + 1)
down_right = cls._is_mine(matrix, x + 1, y + 1)
down = cls._is_mine(matrix, x + 1, y)
down_left = cls._is_mine(matrix, x + 1, y - 1)
left = cls._is_mine(matrix, x, y - 1)
up_left = cls._is_mine(matrix, x - 1, y - 1)
mines = sum([up, up_right, right, down_right, down, down_left, left, up_left])
return mines
@classmethod
def _is_mine(cls, matrix: List[List[bool]], x: int, y: int) -> bool:
try:
if x >= 0 and y >= 0:
return matrix[x][y]
else:
return False
except IndexError:
return False
def minesweeper(matrix: list) -> list:
result = []
for x, row in enumerate(matrix):
new_row = []
for y, col in enumerate(row):
num_mines = _num_neighbor_mines(matrix, x, y)
new_row.append(num_mines)
result.append(new_row)
return result
def _num_neighbor_mines(matrix: list, x: int, y: int) -> int:
up = _is_mine(matrix, x - 1, y)
up_right = _is_mine(matrix, x - 1, y + 1)
right = _is_mine(matrix, x, y + 1)
down_right = _is_mine(matrix, x + 1, y + 1)
down = _is_mine(matrix, x + 1, y)
down_left = _is_mine(matrix, x + 1, y - 1)
left = _is_mine(matrix, x, y - 1)
up_left = _is_mine(matrix, x - 1, y - 1)
mines = sum([up, up_right, right, down_right, down, down_left, left, up_left])
return mines
def _is_mine(matrix: list, x: int, y: int) -> bool:
try:
if x >= 0 and y >= 0:
return matrix[x][y]
else:
return False
except IndexError:
return False
import unittest
from minesweeper import Minesweeper
class TestMinesweeper(unittest.TestCase):
def test_1(self):
matrix = [[True, False, False],
[False, True, False],
[False, False, False]]
solution = [[1, 2, 1],
[2, 1, 1],
[1, 1, 1]]
result = Minesweeper.minesweeper(matrix)
self.assertEqual(result, solution)
def test_2(self):
matrix = [[False, False, False],
[False, False, False]]
solution = [[0, 0, 0],
[0, 0, 0]]
result = Minesweeper.minesweeper(matrix)
self.assertEqual(result, solution)
def test_3(self):
matrix = [[True, False, False, True],
[False, False, True, False],
[True, True, False, True]]
solution = [[0, 2, 2, 1],
[3, 4, 3, 3],
[1, 2, 3, 1]]
result = Minesweeper.minesweeper(matrix)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment