-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgol.tcl
More file actions
executable file
·79 lines (70 loc) · 2.12 KB
/
cgol.tcl
File metadata and controls
executable file
·79 lines (70 loc) · 2.12 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
#!/usr/bin/env tclsh
# Constants for grid size and density
set width 50
set height 30
set density 0.2
# Global array for the grid
array set grid {}
# Initialize the grid with random alive or dead cells
proc initialize_grid {} {
global grid width height density
for {set y 0} {$y < $height} {incr y} {
for {set x 0} {$x < $width} {incr x} {
set grid($y,$x) [expr {rand() < $density ? 1 : 0}]
}
}
}
# Print the grid to the console
proc print_grid {} {
global grid width height
# ANSI escape code to clear screen and move cursor to top-left
puts -nonewline "\033\[H\033\[2J"
for {set y 0} {$y < $height} {incr y} {
for {set x 0} {$x < $width} {incr x} {
if {$grid($y,$x) == 1} {
puts -nonewline "█"
} else {
puts -nonewline " "
}
}
puts ""
}
flush stdout
}
# Count alive neighbors for a cell at (y, x) with toroidal wrapping
proc count_neighbors {y x} {
global grid width height
set count 0
for {set dy -1} {$dy <= 1} {incr dy} {
for {set dx -1} {$dx <= 1} {incr dx} {
if {$dx == 0 && $dy == 0} continue
set ny [expr {($y + $dy + $height) % $height}]
set nx [expr {($x + $dx + $width) % $width}]
if {$grid($ny,$nx)} { incr count }
}
}
return $count
}
# --- Main execution loop ---
initialize_grid
while {1} {
print_grid
# Compute the next generation into a temporary array
array set new_grid {}
for {set y 0} {$y < $height} {incr y} {
for {set x 0} {$x < $width} {incr x} {
set alive $grid($y,$x)
set neighbors [count_neighbors $y $x]
if {($alive && ($neighbors == 2 || $neighbors == 3)) || (!$alive && $neighbors == 3)} {
set new_grid($y,$x) 1
} else {
set new_grid($y,$x) 0
}
}
}
# Update grid for the next iteration
array set grid [array get new_grid]
# Pause between generations (100ms).
# 'exec' is used for portability on Unix-like systems.
exec sleep 0.1
}