def from_file(self, file): """Opens up a saved file""" world = [] rows = 0 columns = 0 with open(r'/Users/jacobglines/Desktop/Programming/gameOfLife/{}'.format(file), 'r') as savedWorld: for line in savedWorld: row = [] cells = line.split(',') for char in cells: if char == '1': cell = Cell(rows, columns).live() row.append(cell) elif char == '0': cell = Cell(rows, columns) row.append(cell) elif char == '\n': pass columns += 1 world.append(row) rows += 1 finalcolumns = columns - 1 columns = 0 newWorld = World(rows, finalcolumns) newWorld.cells = world newWorld.name = file return newWorld
def test_str(self): world = World(0, 0) alive = Cell(0, 0) alive.live() dead = Cell(0, 1) world.cells = [[alive, dead], [dead, alive]] self.assertEqual(str(world), u'\u25A0 \u25A1 \n\u25A1 \u25A0 \n')
def test_sub(self): cell1 = Cell(3) cell2 = Cell(1) cell3 = cell1 - cell2 self.assertEqual(cell3.amount, 2) self.assertNotEqual(cell3, cell1) self.assertNotEqual(cell3, cell2)
def test_div(self): cell1 = Cell(4) cell2 = Cell(2) cell3 = cell1 / cell2 self.assertEqual(cell3.amount, 2) self.assertNotEqual(cell3, cell1) self.assertNotEqual(cell3, cell2)
def test_add(self): cell1 = Cell(1) cell2 = Cell(2) cell3 = cell1 + cell2 self.assertEqual(cell3.amount, 3) self.assertNotEqual(cell3, cell1) self.assertNotEqual(cell3, cell2)
def test_mul(self): cell1 = Cell(2) cell2 = Cell(2) cell3 = cell1 * cell2 self.assertEqual(cell3.amount, 4) self.assertNotEqual(cell3, cell1) self.assertNotEqual(cell3, cell2)
def test_create_cell(self): row = 1 column = 2 cell = Cell(row, column) self.assertEqual(cell.alive, False) self.assertEqual(cell.row, row) self.assertEqual(cell.column, column)
def create_world(self, cell_alive_chance): # create empty array with size+2 width as outer rim will be needed to # simulate toroid structure cells = np.empty([self.side_size + 2, self.side_size + 2], dtype=object) # reference inner cells cells_inner = cells[1:-1, 1:-1] # populate inner array with cell objects and maked them alive/dead # based on random chance specified for i, cell_row in enumerate(cells_inner): for j, cell in enumerate(cell_row): cells_inner[i, j] = Cell( self._set_initial_cell_state(cell_alive_chance)) # make mapping of outer cells to simulate toroid structure # e.g. same cell object will on be position 0,0 and 10,10 if 10 was selected as side_size for i in range(self.side_size): # setting top outer row cells[0, i + 1] = cells_inner[-1, i] # setting left outer column cells[i + 1, 0] = cells_inner[i, -1] # setting bottom outer row cells[-1, i + 1] = cells_inner[0, i] # setting right outer column cells[i + 1, -1] = cells_inner[i, 0] # setting corner cells_inner cells[0, 0] = cells_inner[-1, -1] cells[0, -1] = cells_inner[-1, 0] cells[-1, 0] = cells_inner[0, -1] cells[-1, -1] = cells_inner[0, 0] return cells
def populate_field(self): """ Fill the game field with empty terrain :return: """ self.field_size = App.get_running_app().root.cell_field.field_size self.cells_grid.cols = self.field_size self.cells_grid.rows = self.field_size for x in range(self.field_size * self.field_size): c = Cell() App.get_running_app().root.cell_field.append(c) self.cells_grid.add_widget(FieldCell(c))
def create_cell(canvas, grid, positions) -> dict: text_id = canvas.create_text(settings.CELL_CENTER, settings.CELL_CENTER, text='20') x = random.randint(2, 48) y = random.randint(2, 26) while positions.get(grid[y][x]): x = random.randint(2, 48) y = random.randint(2, 26) temp = {'cell': Cell(grid[y][x], x, y, grid, text_id), 'text': text_id} canvas.itemconfig(temp['cell'].get_id(), fill=settings.CELL_COLOR) positions[temp['cell'].get_id()] = 'cell' return temp.copy()
def create_cells(self): """Creates empty cells for the world""" # # The world is a list of lists # cells = [] for row in range(self.rows): line = [] for column in range(self.columns): cell = Cell(row, column) line.append(cell) cells.append(line) return cells
def gameArea(self): # List of game field buttons # to access the state of each button self.list_btns = [] for x in range(self.height_area): row_btns = [] for y in range(self.width_area): value = self.field[x][y] btn = Cell(x, y, value, self.smile_btn, self.timerFunc, self.showMoves, self.checkBTN, self.usedFlag, self.radar, self.changeValue, self) self.grid.addWidget(btn, x, y) row_btns.append(btn) self.list_btns.append(row_btns) # Window geometry if self.CH > 0: self.adjustSize() self.CH += 1
def __init__(self, field): self.field = field self.original_field = [] self.available_numbers = set() self.random_cell = None self.changed = False for i in range(len(self.field)): for j in range(len(self.field[i])): self.field[i][j] = Cell(self.field[i][j], (i, j)) self.available_numbers.add(self.field[i][j].value) self.available_numbers.remove(0) self.squares = [] self.rows = [] self.columns = [] self.initiate_containers()
def background(self): self.screen.fill((100, 100, 100)) x = 14 y = 14 for n in range(0, int(((250 * self.d) - 14) / 14)): for n in range(0, int(((150 * self.d) - 14) / 14)): pygame.draw.rect(self.screen, (50, 50, 50), (x, y, 14, 14), 1) y += 14 x += 14 y = 14 if len(self.cells) == 0: x = 14 y = 14 for n in range(0, int(((250 * self.d) - 14) / 14)): for n in range(0, int(((150 * self.d) - 14) / 14)): self.cells.append(Cell(self.screen, (x, y))) y += 14 x += 14 y = 14
def __init__(self, cells, *args, **kwargs): super(GrabbableGroundGroup, self).__init__(*args, **kwargs) self.starting_pos = None self.cells = cells # Calculating offsets and initially placing widgets self.cell_widgets = shape_copy(self.cells) self.offsets = shape_copy(self.cells) y_midpoint = int(len(self.cells) / 2) for y in range(len(self.cells)): # Makes little sense to recalculate it every turn, but whatever x_midpoint = int(len(cells[y]) / 2) for x in range(len(cells[y])): self.offsets[y][x] = [ 32 * (x - x_midpoint), 32 * (y - y_midpoint) ] if self.cells[y][x]: self.cell_widgets[y][x] = FieldCell( Cell(ground=self.cells[y][x]), center_x=self.center_x + self.offsets[y][x][0], center_y=self.center_y + self.offsets[y][x][1]) self.add_widget(self.cell_widgets[y][x]) self.bind(pos=self.update_cells)
def test_live_cells(self): cell = Cell(0, 0) cell.live() self.assertTrue(cell.alive)
def test_dead_cells(self): cell = Cell(0, 0) cell.die() self.assertFalse(cell.alive)
import pygame import gamefunctions as gf from time import sleep from settings import Settings from cells import Cell #--------------------------------------------------------------------------------------------------- cw_settings = Settings() pygame.init() #Creation of screen and stuff. screen = gf.set_screen(cw_settings) cells = Cell(cw_settings) if __name__ == '__main__': '''Initialize the game''' while True: '''Loop for the game action.''' gf.check_events(cw_settings, cells) gf.cover_screen(cw_settings, screen) gf.update_screen(cw_settings, screen, cells) sleep(cw_settings.timeSleep)
#Axis #Grid #--- SHAPES ------------------------------------------------------------------- Blip('b1') Kapla('k1', color=blue).offset(100,200) Rect('r1', color=(0,1,1,1), w=100, h=300) # or just p2 if p1 = 0 Rect('hslider', color=red, w=100, h=300) # or just p2 if p1 = 0 # CShape(pac, Pacman) # Shape(line (p1,p2)) # pr just p2 if p1 = 0 TODO #s = Ghost(Color.blue).offset(0,0) TODO #--- CELLS -------------------------------------------------------------------- # shapes are added to the scene via an ordered list of Cells # and thus displayed FIFO Cell('c1', shape='b1') Cell('c2', shape='b1') print '' print 'cells.lmnts viewed from c1', lmnts #Cell(c2, cshapes='pac', peg=dk) # possibly give either 2 or 3 coord or peg TODO #--- RULES -------------------------------------------------------------------- # ordrered list of rules. # syntax : Rule(cell(s)=target shape(s), named rule args=value) #Step('c1', dx=200, dy=20) #Spin('c1', av=10) #Bounce('hslider',rect=SCREEN) #r4=Toggle(kwd=r3.bounced, toggle=r2.change_av) #r5= #randomize_color((Kapla_colors,10),[k])
def test_div_by_zero(self): cell = Cell(1) self.assertRaises(ZeroAmount, cell.make_order, 0)
def test_div_round(self): cell = Cell(3) / Cell(2) self.assertEqual(cell.amount, 1)
def test_str(self): cell = Cell(0, 0) cell.live() self.assertEqual(u'\u25A0', str(cell)) cell.die() self.assertEqual(u'\u25A1', str(cell))
import pygame from settings import Setting import functions as lf from cells import Cell from buttons import Button from game_state import GameState setting = Setting() state = GameState() pygame.init() screen = pygame.display.set_mode((setting.screen_width, setting.screen_height), 0, 32) button = Button('./images/play.png', screen) pygame.display.set_caption("GameofLife") cell1 = Cell(setting.screen_width, setting.screen_height, setting.cell_edge) FPSClock = pygame.time.Clock() while True: "更新cells状态" button.blitbutton(state) lf.check_events(setting, button, state, cell1, screen) # 游戏开始后,更新所有细胞状态 if state.active_flag: screen.fill((setting.dead_cell_color, setting.dead_cell_color, setting.dead_cell_color)) cell1.cell = cell1.live_or_dead() lf.change_color(screen, cell1, setting) pygame.display.flip() FPSClock.tick(setting.FPS)
def f(): return Cell(1) / Cell(0)
def f(): return Cell(1) - Cell(2)
#!/usr/bin/python # -*- coding: iso-8859-1 -*- from cells import Cell, set Cell('c1') Cell('c2') print 'set (@c1) ', set
def test_make_order(self): cell = Cell(3) self.assertEqual(cell.make_order(5), '***..') self.assertEqual(cell.make_order(3), '***') self.assertEqual(cell.make_order(2), "**\n*.")