Пример #1
0
 def test_glider(self):
     print("")
     print("Glider Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x == 1 and y == 2) or
             (x == 2 and y == 3) or (x == 3 and y in [1, 2, 3]) else False)
         for x in range(6)
     ] for y in range(6)])
     print(grid)
     initial_glider = GridWindow(grid=grid,
                                 x_offset=0,
                                 y_offset=0,
                                 x_max=5,
                                 y_max=5)
     initial_glider_str = str(Grid(cells=initial_glider.contents))
     generation = Generation(grid)
     for i in range(3):
         print(generation)
         generation = Generation(generation.grid,
                                 generation.generation_number)
     print(generation)
     final_glider = GridWindow(grid=generation.grid,
                               x_offset=1,
                               y_offset=1,
                               x_max=6,
                               y_max=6)
     final_glider_str = str(Grid(cells=final_glider.contents))
     print("")
     print(self.initial_state_header)
     print(initial_glider)
     print("")
     print(self.final_state_header)
     print(final_glider)
     self.assertEqual(initial_glider_str, final_glider_str)
Пример #2
0
 def test_file_read(self):
     print("")
     print("Basic File Read Test")
     GameIOTestUtils().create_file(path=self.first_file,
                                   data=self.test_data)
     read_grid = read_state(in_file=self.first_file)
     print(read_grid)
     print("=============")
     print(Grid(cells=self.test_data))
     print("")
     self.assertEqual(str(read_grid), str(Grid(cells=self.test_data)))
Пример #3
0
 def test_file_read_comments(self):
     print("")
     print("Basic File with Comment Read Test")
     data = deepcopy(self.test_data)
     data = ["# Hello, this is a comment"] + data[:2] + ["#..*.."
                                                         ] + data[2:]
     GameIOTestUtils().create_file(path=self.first_file, data=data)
     read_grid = read_state(in_file=self.first_file)
     print(read_grid)
     print("=============")
     print(Grid(cells=self.test_data))
     print("")
     self.assertEqual(str(read_grid), str(Grid(cells=self.test_data)))
Пример #4
0
def read_state(in_file: str, **kwargs) -> Grid:
    cell_matrix = []

    def build_cell_matrix(file) -> List[List[Cell]]:
        row_number = 0
        cell_mtrx = []
        for row in file:
            col_number = 0
            cell_mtrx.append([])
            for char in row:
                if char == COMMENT_CHAR or char == '\n':
                    break
                cell_mtrx[len(cell_mtrx) - 1].append(
                    Cell(Coordinates(col_number, row_number),
                         char == ALIVE_CHAR))
                col_number += 1
            row_number += 1
        cell_mtrx = list(filter(lambda row: len(row) != 0, cell_mtrx))
        return cell_mtrx

    if in_file == 'sys.stdin':
        cell_matrix = build_cell_matrix(file=sys.stdin)
    else:
        if type(in_file) == list:
            in_file = in_file[0]
        with open(in_file, 'r') as read_file:
            cell_matrix = build_cell_matrix(file=read_file)
    return Grid(cells=cell_matrix)
Пример #5
0
 def create_file(self, path: str, data: List[Union[List[Cell],
                                                   str]]) -> None:
     with open(path, 'w') as file:
         for line in deepcopy(data):
             if type(line) == str:
                 file.write("{0}\n".format(line))
             else:
                 file.write("{0}\n".format(str(Grid(cells=[line]))))
Пример #6
0
 def test_file_write_then_read(self):
     print("")
     print("Basic File Write and Read Back Test")
     generation = Generation(grid=Grid(cells=self.test_data), steps=0)
     write_state(final_state=generation, out_file=self.first_file)
     read_grid = read_state(in_file=self.first_file)
     print(read_grid)
     print("=============")
     print(generation)
     print("")
     self.assertEqual(str(read_grid), str(generation.grid))
Пример #7
0
 def test_block(self):
     print("")
     print("Block Test")
     grid = Grid(cells=[[
         Cell(Coordinates(x, y), True if (
             x in [1, 2] and y in [1, 2]) else False) for x in range(4)
     ] for y in range(4)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     self.assertEqual(str(grid), str(generation.grid))
Пример #8
0
 def test_tub(self):
     print("")
     print("Tub Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x in [1, 3] and y in [2]) or (
                 y in [1, 3] and x in [2]) else False) for x in range(5)
     ] for y in range(5)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     self.assertEqual(str(grid), str(generation.grid))
Пример #9
0
 def test_boat(self):
     print("")
     print("Boat Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x in [1, 2] and y in [1]) or
             (x in [1, 3] and y in [2]) or (x == 2 and y == 3) else False)
         for x in range(5)
     ] for y in range(5)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     self.assertEqual(str(grid), str(generation.grid))
Пример #10
0
 def test_beacon(self):
     print("")
     print("Beacon Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (y in [1, 2] and x in [1, 2]) or (
                 y in [3, 4] and x in [3, 4]) else False) for x in range(12)
     ] for y in range(6)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     generation2 = Generation(generation.grid, generation.generation_number)
     print(generation2)
     self.assertEqual(str(grid), str(generation2.grid))
Пример #11
0
 def test_loaf(self):
     print("")
     print("Beehive Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x in [2, 3] and y in [1]) or (
                 x in [4] and y in [2, 3]) or ((x == 1 and y == 2) or (
                     x == 2 and y == 3) or (x == 3 and y == 4)) else False)
         for x in range(6)
     ] for y in range(6)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     self.assertEqual(str(grid), str(generation.grid))
Пример #12
0
 def test_blinkers(self):
     print("")
     print("Blinker Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x == 2 and y > 0 and y < 4) or (
                 y == 2 and x > 5 and x < 9) else False) for x in range(10)
     ] for y in range(5)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     generation2 = Generation(generation.grid, generation.generation_number)
     print(generation2)
     self.assertEqual(str(grid), str(generation2.grid))
Пример #13
0
 def test_heavyweight(self):
     print("")
     print("Heavyweight Spaceship Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (x in [3, 4] and y == 1) or (
                 x in [1, 6] and y == 2) or (x == 7 and y in [3, 4, 5]) or (
                     x == 1 and y == 4) or (
                         y == 5 and x in [2, 3, 4, 5, 6]) else False)
         for x in range(11)
     ] for y in range(9)])
     print(grid)
     initial_hwss = GridWindow(grid=grid,
                               x_offset=0,
                               y_offset=0,
                               x_max=9,
                               y_max=7)
     initial_hwss_str = str(Grid(cells=initial_hwss.contents))
     generation = Generation(grid)
     for i in range(3):
         print(generation)
         generation = Generation(generation.grid,
                                 generation.generation_number)
     print(generation)
     final_hwss = GridWindow(grid=generation.grid,
                             x_offset=2,
                             y_offset=0,
                             x_max=11,
                             y_max=7)
     final_hwss_str = str(Grid(cells=final_hwss.contents))
     print("")
     print(self.initial_state_header)
     print(initial_hwss)
     print("")
     print(self.final_state_header)
     print(final_hwss)
     self.assertEqual(initial_hwss_str, final_hwss_str)
Пример #14
0
 def test_toads(self):
     print("")
     print("Toad Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (y == 2 and x > 1 and x < 5) or
             (y == 3 and x > 0 and x < 4) or (x == 8 and y > 1 and y < 5) or
             (x == 9 and y > 2 and y < 6) else False) for x in range(12)
     ] for y in range(7)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     generation2 = Generation(generation.grid, generation.generation_number)
     print(generation2)
     self.assertEqual(str(grid), str(generation2.grid))
Пример #15
0
class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.button_start = QtWidgets.QPushButton("Initialize Grid")
        self.button_play = QtWidgets.QPushButton("Play")
        self.text = QtWidgets.QLabel("Click Play")

        font = QtGui.QFont("Monospace")
        self.text.setFont(font)

        self.setWindowTitle("Conway's Game of Life")
        self.text.setAlignment(QtCore.Qt.AlignCenter)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.button_start)
        self.layout.addWidget(self.button_play)
        self.setLayout(self.layout)

        self.button_start.clicked.connect(self.start)
        self.button_play.clicked.connect(self.simulate)

    def start(self):

        self.grid = Grid(40)
        self.text.setText(self.grid.grid_to_string())
        self.text.repaint()

    def simulate(self):
        print("sim")
        for gen in range(40):
            self.grid.simulate()
            self.text.setText(self.grid.grid_to_string())
            self.text.repaint()
            print(gen)
Пример #16
0
 def test_i_column(self):
     print("")
     print("I-Column Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (
                 y in [3, 6, 8, 9, 11, 14] and x in [4, 5, 6]) or (
                     y in [4, 5, 12, 13] and x in [5]) else False)
         for x in range(11)
     ] for y in range(18)])
     print(grid)
     generation = Generation(grid)
     for i in range(14):
         print(generation)
         generation = Generation(generation.grid,
                                 generation.generation_number)
     print(generation)
     self.assertEqual(str(grid), str(generation.grid))
Пример #17
0
 def test_pulsar(self):
     print("")
     print("Pulsar Test")
     grid = Grid(cells=[[
         Cell(
             Coordinates(x, y), True if (
                 y in [2, 7, 9, 14] and x in [4, 5, 6, 10, 11, 12]) or
             (y in [4, 5, 6, 10, 11, 12] and x in [2, 7, 9, 14]) else False)
         for x in range(17)
     ] for y in range(17)])
     print(grid)
     generation = Generation(grid)
     print(generation)
     generation2 = Generation(generation.grid, generation.generation_number)
     print(generation2)
     generation3 = Generation(generation2.grid,
                              generation2.generation_number)
     print(generation3)
     self.assertEqual(str(grid), str(generation3.grid))
Пример #18
0
    def start(self):

        self.grid = Grid(40)
        self.text.setText(self.grid.grid_to_string())
        self.text.repaint()
Пример #19
0
def getGrid(width, height, density):
    global grid
    if grid is None:
        grid = Grid(width, height, density)
    return grid
Пример #20
0
from sys import stdout
from random import seed
from time import sleep

from life import Grid

seed(0)
g = Grid(40, 30, 0.2)
while True:
    g.nextGeneration()
    print g
    stdout.flush()
    sleep(0.2)