def test_add_element_live(self):
     """
     Test for setting a live cell
     """
     matrix = SparseMatrix()
     matrix.add_element((2, 3), 1)
     resulting_dict = {(2, 3): 1}
     self.assertEqual(matrix.get_iterable(), resulting_dict)
    def test_check_one_surrounding_dead_cell(self):
        """
        Test for dead cell surrounded by one live cell
        """
        matrix = SparseMatrix()
        matrix.add_element((4, 6), 1)

        self.assertEqual(matrix.check_surrounding((4, 5)), 1)
 def test_add_element_dead(self):
     """
     Test for setting a dead cell
     """
     matrix = SparseMatrix()
     matrix.add_element((2, 3), 0)
     resulting_dict = {(2, 3): 0}
     self.assertEqual(matrix.get_iterable(), resulting_dict)
Beispiel #4
0
 def test_alone_cell(self):
     """
     test for only one live cell
     """
     matrix = SparseMatrix()
     matrix.add_element((4, 5), 1)
     resulting_matrix = apply_game_rules(matrix)
     self.assertEqual(resulting_matrix.get_iterable(), {})
Beispiel #5
0
 def test_two_neighbours(self):
     """
     test for only two live cells which are neighbours of each other
     """
     matrix = SparseMatrix()
     matrix.add_element((4, 5), 1)
     matrix.add_element((5, 5), 1)
     resulting_matrix = apply_game_rules(matrix)
     self.assertEqual(resulting_matrix.get_iterable(), {})
Beispiel #6
0
def apply_game_rules(matrix):
    """
    Rules of the game
    1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
    2. Any live cell with two or three live neighbours lives on to the next generation.
    3. Any live cell with more than three live neighbours dies, as if by overpopulation.
    4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    """
    new_matrix = SparseMatrix()
    iterable_matrix = matrix.get_iterable()
    for coordinates in iterable_matrix:
        # import pdb;pdb.set_trace()
        surroundings = matrix.check_surrounding(coordinates)
        if surroundings >= 2:
            # this means there will be a newborn around this
            newborn_coordinates = get_newborn(matrix, coordinates)
            if newborn_coordinates != None:
                for item in newborn_coordinates:
                    new_matrix.add_element(item, 1)
        if surroundings < 2:
            new_matrix.add_element(coordinates, 0)
        if surroundings > 3:
            new_matrix.add_element(coordinates, 0)

    matrix.add_new_elements(new_matrix)
    matrix.cleanup()
    return matrix
 def test_check_surrounding_one(self):
     """
     Test for cell with one live neighbour
     """
     matrix = SparseMatrix()
     matrix.add_element((4, 6), 1)
     matrix.add_element((5, 6), 1)
     matrix.add_element((6, 6), 1)
     self.assertEqual(matrix.check_surrounding((4, 6)), 1)
Beispiel #8
0
def main():
    if (len(sys.argv) != 2):
        print("usage: python3 solveitup.py <problem description>")
        return

    numvars, clauses = getproblem(sys.argv[1])
    grid = buildgrid(numvars, clauses)

    dlx = DLX(SparseMatrix(grid))
    results = dlx.search()
    printresults(results)
Beispiel #9
0
    def test_get_newborn_cell(self):
        """
        test for cell with three live neighbours
        """
        matrix = SparseMatrix()
        matrix.add_element((4, 5), 1)
        matrix.add_element((5, 5), 1)
        matrix.add_element((6, 5), 1)
        matrix.add_element((6, 4), 1)
        matrix.add_element((4, 4), 1)
        matrix.add_element((5, 6), 1)
        result = get_newborn(matrix, (5, 5))

        self.assertEqual(set(result), set([(4, 6), (6, 6)]))
Beispiel #10
0
 def test_get_newborn_three_neighbours(self):
     matrix = SparseMatrix()
     matrix.add_element((4, 5), 1)
     matrix.add_element((5, 5), 1)
     matrix.add_element((6, 5), 1)
     result = get_newborn(matrix, (5, 5))
     self.assertEqual(set(result), set([(5, 6), (5, 4)]))
Beispiel #11
0
    def test_six_neighbours(self):
        matrix = SparseMatrix()
        matrix.add_element((4, 5), 1)
        matrix.add_element((5, 5), 1)
        matrix.add_element((6, 5), 1)
        matrix.add_element((6, 4), 1)
        matrix.add_element((4, 4), 1)
        matrix.add_element((5, 6), 1)

        resulting_matrix = apply_game_rules(matrix)

        expected_result = dict()
        expected_result.update({(5, 6):1, (6, 5):1, (6, 4):1, (4, 6):1,
                                (4, 5):1, (4, 4):1, (6, 6):1})
        self.assertEqual(resulting_matrix.get_iterable(), expected_result)
Beispiel #12
0
def main():
    """
    Main function
    """
    matrix = SparseMatrix()
    # matrix.add_element((4, 6), 1)
    # matrix.add_element((5, 6), 1)
    # matrix.add_element((6, 6), 1)

    matrix.add_element((4, 5), 1)
    matrix.add_element((5, 5), 1)
    matrix.add_element((6, 5), 1)
    matrix.print_matrix()

    # matrix.add_element((4, 5), 1)
    # matrix.add_element((5, 5), 1)
    # matrix.add_element((6, 5), 1)
    # matrix.add_element((6, 4), 1)
    # matrix.add_element((4, 4), 1)
    # matrix.add_element((5, 6), 1)
    """
    TODO: add argeparse for handling number of runs to make
    """
    run_game(matrix, 3)
 def __init__(self, size):
     self._size = size
     self._board = SparseMatrix(size, size)
 def reset(self):
     self._board = SparseMatrix(self._size, self._size)
Beispiel #15
0
def get_newborn(matrix, coordinates):
    """
    Returns the position of newborn cell/s
    after marking a cell as newborn crosscheck that it has 3 cells surrounding, because
    this function will be called when there are 3 or 4 live neighbours too.
    """

    new_matrix = SparseMatrix()
    new_matrix.add_new_elements(matrix)
    x, y = coordinates
    newborn = []

    # left top corner
    if matrix.get_value_at((x + 1, y)) == 1 and matrix.get_value_at(
        (x, y - 1)) == 1:
        if matrix.get_value_at((x + 1, y - 1)) != 1:
            new_matrix.add_element((x + 1, y - 1), 1)
            if new_matrix.check_surrounding((x + 1, y - 1)) == 3:
                newborn.append((x + 1, y - 1))

            new_matrix.add_element((x + 1, y - 1), 0)
            new_matrix.cleanup()

    # right top corner
    if matrix.get_value_at((x - 1, y)) == 1 and matrix.get_value_at(
        (x, y - 1)) == 1:
        if matrix.get_value_at((x - 1, y - 1)) != 1:
            new_matrix.add_element((x - 1, y - 1), 1)
            if new_matrix.check_surrounding((x - 1, y - 1)) == 3:
                newborn.append((x - 1, y - 1))

            new_matrix.add_element((x - 1, y - 1), 0)
            new_matrix.cleanup()

    # left bottom corner
    if matrix.get_value_at((x, y + 1)) == 1 and matrix.get_value_at(
        (x + 1, y)) == 1:
        if matrix.get_value_at((x + 1, y + 1)) != 1:
            new_matrix.add_element((x + 1, y + 1), 1)
            if new_matrix.check_surrounding((x + 1, y + 1)) == 3:
                newborn.append((x + 1, y + 1))
            new_matrix.add_element((x + 1, y + 1), 0)
            new_matrix.cleanup()

    # right bottom corner
    if matrix.get_value_at((x - 1, y)) == 1 and matrix.get_value_at(
        (x, y + 1)) == 1:
        if matrix.get_value_at((x - 1, y + 1)) != 1:
            new_matrix.add_element((x - 1, y + 1), 1)
            if new_matrix.check_surrounding((x - 1, y + 1)) == 3:
                newborn.append((x - 1, y + 1))
            new_matrix.add_element((x - 1, y + 1), 0)
            new_matrix.cleanup()

    # vertical or horizontal line
    if matrix.get_value_at((x - 1, y)) == 1 and matrix.get_value_at(
        (x + 1, y)) == 1:

        if matrix.get_value_at((x, y + 1)) != 1:
            new_matrix.add_element((x, y + 1), 1)
            if new_matrix.check_surrounding((x, y + 1)) == 3:
                newborn.append((x, y + 1))
            new_matrix.add_element((x, y + 1), 0)
            new_matrix.cleanup()
        if matrix.get_value_at((x, y - 1)) != 1:
            new_matrix.add_element((x, y - 1), 1)
            if new_matrix.check_surrounding((x, y - 1)) == 3:
                newborn.append((x, y - 1))
            new_matrix.add_element((x, y - 1), 0)
            new_matrix.cleanup()

    if matrix.get_value_at((x, y + 1)) == 1 and matrix.get_value_at(
        (x, y - 1)) == 1:
        if matrix.get_value_at((x - 1, y)) != 1:
            new_matrix.add_element((x - 1, y), 1)
            if new_matrix.check_surrounding((x - 1, y)) == 3:
                newborn.append((x - 1, y))
            new_matrix.add_element((x - 1, y), 0)
            new_matrix.cleanup()
        if matrix.get_value_at((x + 1, y)) != 1:
            new_matrix.add_element((x + 1, y), 1)
            if new_matrix.check_surrounding((x + 1, y)) == 3:
                newborn.append((x + 1, y))
            new_matrix.add_element((x + 1, y), 0)
            new_matrix.cleanup()

    return newborn