-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellular_automata_visualization.py
More file actions
115 lines (88 loc) · 3.74 KB
/
cellular_automata_visualization.py
File metadata and controls
115 lines (88 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import cellular_automata
import pygame
import configparser
import sys
from sys import argv
import random
# Prompt the user to enter the filename
if __name__ == "__main__":
"""
This script reads a configuration file specified by the user, parses it line by line,
and stores the configuration values in a dictionary. It assumes that the configuration file contains key-value
pairs separated by '='. The keys are strings, and the values are expected to be integers.
Usage:
1. Run the script and provide the configuration file's filename when prompted.
2. The script will attempt to open and read the file, extract key-value pairs,
and store them in the 'config_values' dictionary.
3. If the specified file is not found, a 'FileNotFoundError' will be raised, and an error message will be displayed.
4. If any other error occurs during file reading or parsing,
an error message will be displayed with details about the exception.
Example Configuration File Format:
-------------------
key1=42
key2=123
key3=987
-------------------
Note:
- Ensure that the configuration file is in the specified format, with one key-value pair per line.
- The values are assumed to be integers, so any non-integer values will raise an error.
Author: Aldo Canfora
Date: 04/10/2023
"""
config = configparser.ConfigParser()
# Set a deafult configuration file name
default_config_file = 'configuration.txt'
# Check if a command-line argument for the configuration file name has been provided
if len(sys.argv) > 1:
config_file = sys.argv[1]
else:
# If no argument is provided, use the default configuration file name
config_file = default_config_file
config.read(config_file)
# Extract the value of seed_value from the configuration file, if present
seed_value = config.get('settings', 'seed_value', fallback=None)
# Check if seed_value is present in the configuration file
if seed_value is not None:
seed_value = int(seed_value)
else:
# If seed_value is not present, generate a random value
seed_value = random.randint(1, 1000)
WIDTH = config.get('settings', 'WIDTH')
HEIGHT = config.get('settings', 'HEIGHT')
border_type = config.get('settings', 'border_type')
WIDTH = int(WIDTH)
HEIGHT = int(HEIGHT)
seed_value = int(seed_value)
"""
This script demonstrates a simple cellular automaton simulation using pygame for visualization.
It initializes a pygame window and creates an initial grid for the cellular automaton. The game loop allows you to
visualize the automaton's evolution as it updates the grid and displays it on the screen.
Usage:
1. Make sure you have the 'cellular_automata', 'pygame', and 'parameters' modules imported or defined.
2. Run the script, and a pygame window will open with the cellular automaton simulation.
3. You can close the window by clicking the close button.
Requirements:
- pygame: You need to have pygame installed to run this script.
"""
#main part of the code
# Initialize pygame and window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# create initial grid
grid = cellular_automata.initial_state_grid(WIDTH//10, HEIGHT//10, seed_value)
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# draw grid
for i, row in enumerate(grid):
for j, cell in enumerate(row):
color = (255, 255, 255) if cell == 1 else (0, 0, 0)
pygame.draw.rect(screen, color, (j*10, i*10, 10, 10))
# update grid
grid = cellular_automata.update_grid(grid, border_type)
# update screen
pygame.display.update()
# clean and quit pygame
pygame.quit()