Optimize game of life implementation to bitwise operations to achieve 450-500 fps for a glider gun, use int instead of math.ceil for positions and fix generation fps reset after file manager

This commit is contained in:
csd4ni3l
2025-06-23 18:56:44 +02:00
parent 6532d18c91
commit c44dde3297
4 changed files with 101 additions and 52 deletions

39
game/game_of_life.py Normal file
View File

@@ -0,0 +1,39 @@
from utils.constants import ROWS, COLS, NEIGHBORS
TOTAL_CELLS = ROWS * COLS
def get_index(row, col):
return row * COLS + col
def get_neighbors(cell_grid, neighbor_mask):
return (cell_grid & neighbor_mask).bit_count()
def unset_bit(number, i):
return number & ~(1 << i)
def set_bit(number, i):
return number | (1 << i)
def get_bit(number, i):
return (number >> i) & 1
def print_bits(n: int, width: int = 8):
print(f"{n:0{width}b}")
def create_zeroed_int(n):
zero_val = 0
bitmask = (1 << n) -1
return zero_val & bitmask
def precompute_neighbor_masks():
masks = [0] * TOTAL_CELLS
for row in range(ROWS):
for col in range(COLS):
index = get_index(row, col)
mask = 0
for dy, dx in NEIGHBORS:
ny, nx = row + dy, col + dx
if 0 <= ny < ROWS and 0 <= nx < COLS:
neighbor_index = get_index(ny, nx)
mask |= 1 << neighbor_index
masks[index] = mask
return masks