Initial commit
This commit is contained in:
		
							
								
								
									
										61
									
								
								src/conway.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								src/conway.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
GLIDER = [[False, False, False, False, False, False, False, False],
 | 
			
		||||
          [False, False, False, False, True, False, False, False],
 | 
			
		||||
          [False, False, True, False, True, False, False, False],
 | 
			
		||||
          [False, False, False, True, True, False, False, False],
 | 
			
		||||
          [False, False, False, False, False, False, False, False],
 | 
			
		||||
          [False, False, False, False, False, False, False, False],
 | 
			
		||||
          [False, False, False, False, False, False, False, False],
 | 
			
		||||
          [False, False, False, False, False, False, False, False]]
 | 
			
		||||
 | 
			
		||||
OFF = (0, 0, 0)
 | 
			
		||||
ON = (100, 100, 80)
 | 
			
		||||
 | 
			
		||||
def draw_universe(trellis, universe):
 | 
			
		||||
    for x in range(8):
 | 
			
		||||
        for y in range(8):
 | 
			
		||||
            #activate rising edge events on all keys
 | 
			
		||||
            if universe[x][y]:
 | 
			
		||||
                trellis.color(x, y, ON)
 | 
			
		||||
            else:
 | 
			
		||||
                trellis.color(x, y, OFF)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def extend_universe(universe):
 | 
			
		||||
    """Extend an 8x8 universe to 10x10 with first and last columns and rows as in an donut-shaped universe"""
 | 
			
		||||
    ext_universe = [[False for x_ext in range(10)] for y_ext in range(10)]
 | 
			
		||||
 | 
			
		||||
    # Copy universe in an extended donut-shaped universe
 | 
			
		||||
    for x_ext in range(10):
 | 
			
		||||
        for y_ext in range(10):
 | 
			
		||||
            if x_ext == 0:
 | 
			
		||||
                loc_x = 7
 | 
			
		||||
            elif x_ext == 9:
 | 
			
		||||
                loc_x = 0
 | 
			
		||||
            else:
 | 
			
		||||
                loc_x = x_ext-1
 | 
			
		||||
            if y_ext == 0:
 | 
			
		||||
                loc_y = 7
 | 
			
		||||
            elif y_ext == 9:
 | 
			
		||||
                loc_y = 0
 | 
			
		||||
            else:
 | 
			
		||||
                loc_y = y_ext-1
 | 
			
		||||
 | 
			
		||||
            ext_universe[x_ext][y_ext] = universe[loc_x][loc_y]
 | 
			
		||||
    return ext_universe
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def living_neighbour_count(xcoord, ycoord, ext_universe):
 | 
			
		||||
    """Count living cells around xcoord, ycoord"""
 | 
			
		||||
 | 
			
		||||
    living_neighbours = []
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord][ycoord])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord][ycoord+1])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord][ycoord+2])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord+1][ycoord])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord+1][ycoord+2])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord+2][ycoord])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord+2][ycoord+1])
 | 
			
		||||
    living_neighbours.append(ext_universe[xcoord+2][ycoord+2])
 | 
			
		||||
    # print(living_neighbours.count(True), living_neighbours)
 | 
			
		||||
    return living_neighbours.count(True)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user