Skip to content

Commit

Permalink
Takes care of (literal) corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jonodrew committed Nov 17, 2018
1 parent 5bf9e33 commit 21082b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 14 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
class Grid:
def __init__(self, size):
self.diagram = [[] for i in range(0, size)]
self.length = size
self.diagram = [[Cell() for i in range(0, size)] for i in range(0, size)]

def neighbour_coordinates(self, y_coordinate, x_coordinate):
coords = [
(y_coordinate + y, x_coordinate + x)
for y in range(-1, 2)
for x in range(-1, 2)
if (self.length > y_coordinate + y >= 0) and (self.length > x_coordinate + x >= 0)
]
coords.remove((y_coordinate, x_coordinate))
return coords


class Cell:
def __init__(self):
pass
pass
18 changes: 15 additions & 3 deletions test_game.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import pytest
from game import Grid, Cell

@pytest.fixture
def ten_grid():
return Grid(10)

class TestGrid:
def test_give_size_builds_array_of_length_size(self):
def test_give_size_builds_array_of_length_size(self, ten_grid):
test_size = 10
test_grid = Grid(test_size)
assert len(test_grid.diagram) == test_size
assert len(ten_grid.diagram) == test_size

def test_given_size_builds_array_of_cells(self, ten_grid):
assert isinstance(ten_grid.diagram[2][4], Cell)

def test_given_position_returns_correct_count_of_neighbours(self, ten_grid):
assert len(ten_grid.neighbour_coordinates(1, 1)) == 8
assert len(ten_grid.neighbour_coordinates(0, 0)) == 3
assert len(ten_grid.neighbour_coordinates(9, 9)) == 3
assert len(ten_grid.neighbour_coordinates(0, 9)) == 3
assert len(ten_grid.neighbour_coordinates(9, 0)) == 3

0 comments on commit 21082b9

Please sign in to comment.