I’ve added a new version to my venerable Polyglot Conway project, this time as a good old Bash script, a version bootstrapped with the help of ChatGPT.
It turns out that the latest versions of Bash feature associative arrays, which are perfect for my purposes. The result looks like shown in the asciinema movie below: an exact replica of all the other versions!
One of the many things I’ve learned while making this was how not efficient it is to call bash “functions” using subshells; the original code provided by ChatGPT contained lots of “function calls” and the final script, even though correct, was running painfully slow. To solve this issue, I just inlined those code pieces, and the final result runs as fast as the versions in other programming languages.
Here’s a fragment of the script:
#!/bin/bash
declare -A world
set_value() {
local x=$1
local y=$2
local value=$3
world["${x},${y}"]="$value"
}
SIZE=30
initialize_world() {
for ((y=0; y<SIZE; y++)); do
for ((x=0; x<SIZE; x++)); do
set_value $x $y "dead"
done
done
}
evolve() {
# ...
}
print_grid() {
# ...
}
create_blinker() {
local x=$1
local y=$2
set_value "$x" "$y" "alive"
set_value $((x+1)) "$y" "alive"
set_value $((x+2)) "$y" "alive"
}
create_beacon() {
# ...
}
create_glider() {
# ...
}
create_block() {
# ...
}
create_tub() {
# ...
}
initialize_game() {
initialize_world
create_blinker 0 1
create_beacon 10 10
create_glider 4 5
create_block 1 10
create_block 18 3
create_tub 6 1
}
initialize_game
generation=0
while true; do
clear
print_grid
echo
((generation+=1))
printf "Generation %d" $generation
sleep 0.5
evolve
done