コード例 #1
0
def test_reproduction():
    game = Life()
    game.set_living((0, 0))
    game.set_living((0, 1))
    game.set_living((0, -1))

    game.tick()
    assert game.is_alive((1, 0))
    assert game.is_alive((-1, 0))
コード例 #2
0
def test_set_dead():
    game = Life()
    game.set_dead((0, 0))
    assert not game.is_alive((0, 0))

    game.set_living((0, 0))
    assert game.is_alive((0, 0))
    game.set_dead((0, 0))
    assert not game.is_alive((0, 0))
コード例 #3
0
def test_over_population_death():
    for count in range(4, 8):
        game = Life()
        game.set_living((0, 0))

        neighbour = game.neighbours((0, 0))
        for i in range(0, count):
            game.set_living(neighbour.next())

        assert game.is_alive((0, 0))
        game.tick()
        assert not game.is_alive((0, 0))
コード例 #4
0
def test_reproduction_stable():
    game = Life()
    game.set_living((0, 0))
    game.set_living((-1, 0))
    game.set_living((-1, -1))

    for i in range(0, 100):
        game.tick()
        assert game.is_alive((0, 0))
        assert game.is_alive((-1, 0))
        assert game.is_alive((-1, -1))

        assert game.is_alive((0, -1))
コード例 #5
0
def test_isolated_under_population():
    game = Life()
    game.set_living((0, 0))
    game.set_living((2, 2))
    game.set_living((2, 3))
    game.set_living((2, 4))

    assert len(list(game.living)) == 4

    game.tick()

    # In this case, we need to check specific spaces, since the number alive will change once the reproduction rule is implemented
    assert not game.is_alive((0, 0))
    assert not game.is_alive((2, 2))
    assert not game.is_alive((2, 4))
    assert game.is_alive((2, 3))
コード例 #6
0
def test_spinning():
    game = Life()
    game.set_living((0, 0))
    game.set_living((-1, 0))
    game.set_living((1, 0))

    for i in range(0, 100):
        game.tick()
        assert game.is_alive((0, 0))
        assert game.is_alive((0, 1))
        assert game.is_alive((0, -1))
        assert len(list(game.living)) == 3

        game.tick()
        assert game.is_alive((0, 0))
        assert game.is_alive((-1, 0))
        assert game.is_alive((1, 0))
        assert len(list(game.living)) == 3
コード例 #7
0
def test_over_population_isolated():
    game = Life()
    game.set_living((0, 0))
    game.set_living((0, 1))
    game.set_living((-1, 0))
    game.set_living((1, 0))
    game.set_living((-1, -1))

    game.set_living((10, 10))
    game.set_living((11, 10))
    game.set_living((9, 10))
    game.set_living((10, 11))
    game.set_living((10, 9))

    assert len(list(game.living)) == 10
    game.tick()
    assert not game.is_alive((0, 0))
    assert not game.is_alive((10, 10))
コード例 #8
0
def test_under_population_death():
    game = Life()
    game.set_living((0, 0))
    assert game.is_alive((0, 0))

    game.tick()
    assert len(list(game.living)) == 0
    game.tick()
    assert len(list(game.living)) == 0
コード例 #9
0
def test_known_final_result():
    """Using "small exploder" pattern:
    X10
    111
    101
    010

    Known to produce the pattern
    00000010000000
    00000101000000
    00000101000000
    00000010000000
    00000000000000
    01100X00000110
    10010000001001
    01100000000110
    00000000000000
    00000010000000
    00000101000000
    00000101000000
    00000010000000

    after 16 iterations. X used to mark reference point.
    """

    game = Life()
    # Initial pattern
    game.set_living((1, 0))
    game.set_living((0, 1))
    game.set_living((1, 1))
    game.set_living((2, 1))
    game.set_living((0, 2))
    game.set_living((2, 2))
    game.set_living((1, 3))

    for i in range(0, 17):
        game.tick()

    # Top pattern
    assert game.is_alive((1, -5))
    assert game.is_alive((0, -4))
    assert game.is_alive((2, -4))
    assert game.is_alive((0, -3))
    assert game.is_alive((2, -3))
    assert game.is_alive((1, -2))

    # Left pattern
    assert game.is_alive((-3, 0))
    assert game.is_alive((-4, 0))
    assert game.is_alive((-2, 1))
    assert game.is_alive((-5, 1))
    assert game.is_alive((-3, 2))
    assert game.is_alive((-4, 2))

    # Right pattern
    assert game.is_alive((5, 0))
    assert game.is_alive((6, 0))
    assert game.is_alive((4, 1))
    assert game.is_alive((7, 1))
    assert game.is_alive((5, 2))
    assert game.is_alive((6, 2))

    # Bottom pattern
    assert game.is_alive((1, 4))
    assert game.is_alive((0, 5))
    assert game.is_alive((2, 5))
    assert game.is_alive((0, 6))
    assert game.is_alive((2, 6))
    assert game.is_alive((1, 7))

    assert len(list(game.living)) == 24