| 1 | #!/bin/bash |
| 2 | # life.sh: "Life in the Slow Lane" |
| 3 | # Author: Mendel Cooper |
| 4 | # License: GPL3 |
| 5 | |
| 6 | # Version 0.2: Patched by Daniel Albers |
| 7 | #+ to allow non-square grids as input. |
| 8 | # Version 0.2.1: Added 2-second delay between generations. |
| 9 | |
| 10 | # ##################################################################### # |
| 11 | # This is the Bash script version of John Conway's "Game of Life". # |
| 12 | # "Life" is a simple implementation of cellular automata. # |
| 13 | # --------------------------------------------------------------------- # |
| 14 | # On a rectangular grid, let each "cell" be either "living" or "dead." # |
| 15 | # Designate a living cell with a dot, and a dead one with a blank space.# |
| 16 | # Begin with an arbitrarily drawn dot-and-blank grid, # |
| 17 | #+ and let this be the starting generation: generation 0. # |
| 18 | # Determine each successive generation by the following rules: # |
| 19 | # 1) Each cell has 8 neighbors, the adjoining cells # |
| 20 | #+ left, right, top, bottom, and the 4 diagonals. # |
| 21 | # # |
| 22 | # 123 # |
| 23 | # 4*5 The * is the cell under consideration. # |
| 24 | # 678 # |
| 25 | # # |
| 26 | # 2) A living cell with either 2 or 3 living neighbors remains alive. # |
| 27 | SURVIVE=2 # |
| 28 | # 3) A dead cell with 3 living neighbors comes alive, a "birth." # |
| 29 | BIRTH=3 # |
| 30 | # 4) All other cases result in a dead cell for the next generation. # |
| 31 | # ##################################################################### # |
| 32 | |
| 33 | |
| 34 | startfile=gen0 # Read the starting generation from the file "gen0" ... |
| 35 | # Default, if no other file specified when invoking script. |
| 36 | # |
| 37 | if [ -n "$1" ] # Specify another "generation 0" file. |
| 38 | then |
| 39 | startfile="$1" |
| 40 | fi |
| 41 | |
| 42 | ############################################ |
| 43 | # Abort script if "startfile" not specified |
| 44 | #+ and |
| 45 | #+ default file "gen0" not present. |
| 46 | |
| 47 | E_NOSTARTFILE=86 |
| 48 | |
| 49 | if [ ! -e "$startfile" ] |
| 50 | then |
| 51 | echo "Startfile \""$startfile"\" missing!" |
| 52 | exit $E_NOSTARTFILE |
| 53 | fi |
| 54 | ############################################ |
| 55 | |
| 56 | |
| 57 | ALIVE1=. |
| 58 | DEAD1=_ |
| 59 | # Represent living and dead cells in the start-up file. |
| 60 | |
| 61 | # -----------------------------------------------------# |
| 62 | # This script uses a 10 x 10 grid (may be increased, |
| 63 | #+ but a large grid will slow down execution). |
| 64 | ROWS=10 |
| 65 | COLS=10 |
| 66 | # Change above two variables to match desired grid size. |
| 67 | # -----------------------------------------------------# |
| 68 | |
| 69 | GENERATIONS=10 # How many generations to cycle through. |
| 70 | # Adjust this upwards |
| 71 | #+ if you have time on your hands. |
| 72 | |
| 73 | NONE_ALIVE=85 # Exit status on premature bailout, |
| 74 | #+ if no cells left alive. |
| 75 | DELAY=2 # Pause between generations. |
| 76 | TRUE=0 |
| 77 | FALSE=1 |
| 78 | ALIVE=0 |
| 79 | DEAD=1 |
| 80 | |
| 81 | avar= # Global; holds current generation. |
| 82 | generation=0 # Initialize generation count. |
| 83 | |
| 84 | # ================================================================= |
| 85 | |
| 86 | let "cells = $ROWS * $COLS" # How many cells. |
| 87 | |
| 88 | # Arrays containing "cells." |
| 89 | declare -a initial |
| 90 | declare -a current |
| 91 | |
| 92 | display () |
| 93 | { |
| 94 | |
| 95 | alive=0 # How many cells alive at any given time. |
| 96 | # Initially zero. |
| 97 | |
| 98 | declare -a arr |
| 99 | arr=( `echo "$1"` ) # Convert passed arg to array. |
| 100 | |
| 101 | element_count=${#arr[*]} |
| 102 | |
| 103 | local i |
| 104 | local rowcheck |
| 105 | |
| 106 | for ((i=0; i<$element_count; i++)) |
| 107 | do |
| 108 | |
| 109 | # Insert newline at end of each row. |
| 110 | let "rowcheck = $i % COLS" |
| 111 | if [ "$rowcheck" -eq 0 ] |
| 112 | then |
| 113 | echo # Newline. |
| 114 | echo -n " " # Indent. |
| 115 | fi |
| 116 | |
| 117 | cell=${arr[i]} |
| 118 | |
| 119 | if [ "$cell" = . ] |
| 120 | then |
| 121 | let "alive += 1" |
| 122 | fi |
| 123 | |
| 124 | echo -n "$cell" | sed -e 's/_/ /g' |
| 125 | # Print out array, changing underscores to spaces. |
| 126 | done |
| 127 | |
| 128 | return |
| 129 | |
| 130 | } |
| 131 | |
| 132 | IsValid () # Test if cell coordinate valid. |
| 133 | { |
| 134 | |
| 135 | if [ -z "$1" -o -z "$2" ] # Mandatory arguments missing? |
| 136 | then |
| 137 | return $FALSE |
| 138 | fi |
| 139 | |
| 140 | local row |
| 141 | local lower_limit=0 # Disallow negative coordinate. |
| 142 | local upper_limit |
| 143 | local left |
| 144 | local right |
| 145 | |
| 146 | let "upper_limit = $ROWS * $COLS - 1" # Total number of cells. |
| 147 | |
| 148 | |
| 149 | if [ "$1" -lt "$lower_limit" -o "$1" -gt "$upper_limit" ] |
| 150 | then |
| 151 | return $FALSE # Out of array bounds. |
| 152 | fi |
| 153 | |
| 154 | row=$2 |
| 155 | let "left = $row * $COLS" # Left limit. |
| 156 | let "right = $left + $COLS - 1" # Right limit. |
| 157 | |
| 158 | if [ "$1" -lt "$left" -o "$1" -gt "$right" ] |
| 159 | then |
| 160 | return $FALSE # Beyond row boundary. |
| 161 | fi |
| 162 | |
| 163 | return $TRUE # Valid coordinate. |
| 164 | |
| 165 | } |
| 166 | |
| 167 | |
| 168 | IsAlive () # Test whether cell is alive. |
| 169 | # Takes array, cell number, and |
| 170 | { #+ state of cell as arguments. |
| 171 | GetCount "$1" $2 # Get alive cell count in neighborhood. |
| 172 | local nhbd=$? |
| 173 | |
| 174 | if [ "$nhbd" -eq "$BIRTH" ] # Alive in any case. |
| 175 | then |
| 176 | return $ALIVE |
| 177 | fi |
| 178 | |
| 179 | if [ "$3" = "." -a "$nhbd" -eq "$SURVIVE" ] |
| 180 | then # Alive only if previously alive. |
| 181 | return $ALIVE |
| 182 | fi |
| 183 | |
| 184 | return $DEAD # Defaults to dead. |
| 185 | |
| 186 | } |
| 187 | |
| 188 | |
| 189 | GetCount () # Count live cells in passed cell's neighborhood. |
| 190 | # Two arguments needed: |
| 191 | # $1) variable holding array |
| 192 | # $2) cell number |
| 193 | { |
| 194 | local cell_number=$2 |
| 195 | local array |
| 196 | local top |
| 197 | local center |
| 198 | local bottom |
| 199 | local r |
| 200 | local row |
| 201 | local i |
| 202 | local t_top |
| 203 | local t_cen |
| 204 | local t_bot |
| 205 | local count=0 |
| 206 | local ROW_NHBD=3 |
| 207 | |
| 208 | array=( `echo "$1"` ) |
| 209 | |
| 210 | let "top = $cell_number - $COLS - 1" # Set up cell neighborhood. |
| 211 | let "center = $cell_number - 1" |
| 212 | let "bottom = $cell_number + $COLS - 1" |
| 213 | let "r = $cell_number / $COLS" |
| 214 | |
| 215 | for ((i=0; i<$ROW_NHBD; i++)) # Traverse from left to right. |
| 216 | do |
| 217 | let "t_top = $top + $i" |
| 218 | let "t_cen = $center + $i" |
| 219 | let "t_bot = $bottom + $i" |
| 220 | |
| 221 | |
| 222 | let "row = $r" # Count center row. |
| 223 | IsValid $t_cen $row # Valid cell position? |
| 224 | if [ $? -eq "$TRUE" ] |
| 225 | then |
| 226 | if [ ${array[$t_cen]} = "$ALIVE1" ] # Is it alive? |
| 227 | then # If yes, then ... |
| 228 | let "count += 1" # Increment count. |
| 229 | fi |
| 230 | fi |
| 231 | |
| 232 | let "row = $r - 1" # Count top row. |
| 233 | IsValid $t_top $row |
| 234 | if [ $? -eq "$TRUE" ] |
| 235 | then |
| 236 | if [ ${array[$t_top]} = "$ALIVE1" ] # Redundancy here. |
| 237 | then # Can it be optimized? |
| 238 | let "count += 1" |
| 239 | fi |
| 240 | fi |
| 241 | |
| 242 | let "row = $r + 1" # Count bottom row. |
| 243 | IsValid $t_bot $row |
| 244 | if [ $? -eq "$TRUE" ] |
| 245 | then |
| 246 | if [ ${array[$t_bot]} = "$ALIVE1" ] |
| 247 | then |
| 248 | let "count += 1" |
| 249 | fi |
| 250 | fi |
| 251 | |
| 252 | done |
| 253 | |
| 254 | |
| 255 | if [ ${array[$cell_number]} = "$ALIVE1" ] |
| 256 | then |
| 257 | let "count -= 1" # Make sure value of tested cell itself |
| 258 | fi #+ is not counted. |
| 259 | |
| 260 | |
| 261 | return $count |
| 262 | |
| 263 | } |
| 264 | |
| 265 | next_gen () # Update generation array. |
| 266 | { |
| 267 | |
| 268 | local array |
| 269 | local i=0 |
| 270 | |
| 271 | array=( `echo "$1"` ) # Convert passed arg to array. |
| 272 | |
| 273 | while [ "$i" -lt "$cells" ] |
| 274 | do |
| 275 | IsAlive "$1" $i ${array[$i]} # Is the cell alive? |
| 276 | if [ $? -eq "$ALIVE" ] |
| 277 | then # If alive, then |
| 278 | array[$i]=. #+ represent the cell as a period. |
| 279 | else |
| 280 | array[$i]="_" # Otherwise underscore |
| 281 | fi #+ (will later be converted to space). |
| 282 | let "i += 1" |
| 283 | done |
| 284 | |
| 285 | |
| 286 | # let "generation += 1" # Increment generation count. |
| 287 | ### Why was the above line commented out? |
| 288 | |
| 289 | |
| 290 | # Set variable to pass as parameter to "display" function. |
| 291 | avar=`echo ${array[@]}` # Convert array back to string variable. |
| 292 | display "$avar" # Display it. |
| 293 | echo; echo |
| 294 | echo "Generation $generation - $alive alive" |
| 295 | |
| 296 | if [ "$alive" -eq 0 ] |
| 297 | then |
| 298 | echo |
| 299 | echo "Premature exit: no more cells alive!" |
| 300 | exit $NONE_ALIVE # No point in continuing |
| 301 | fi #+ if no live cells. |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |
| 306 | # ========================================================= |
| 307 | |
| 308 | # main () |
| 309 | # { |
| 310 | |
| 311 | # Load initial array with contents of startup file. |
| 312 | initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\ |
| 313 | # Delete lines containing '#' comment character. |
| 314 | sed -e 's/\./\. /g' -e 's/_/_ /g'` ) |
| 315 | # Remove linefeeds and insert space between elements. |
| 316 | |
| 317 | clear # Clear screen. |
| 318 | |
| 319 | echo # Title |
| 320 | setterm -reverse on |
| 321 | echo "=======================" |
| 322 | setterm -reverse off |
| 323 | echo " $GENERATIONS generations" |
| 324 | echo " of" |
| 325 | echo "\"Life in the Slow Lane\"" |
| 326 | setterm -reverse on |
| 327 | echo "=======================" |
| 328 | setterm -reverse off |
| 329 | |
| 330 | sleep $DELAY # Display "splash screen" for 2 seconds. |
| 331 | |
| 332 | |
| 333 | # -------- Display first generation. -------- |
| 334 | Gen0=`echo ${initial[@]}` |
| 335 | display "$Gen0" # Display only. |
| 336 | echo; echo |
| 337 | echo "Generation $generation - $alive alive" |
| 338 | sleep $DELAY |
| 339 | # ------------------------------------------- |
| 340 | |
| 341 | |
| 342 | let "generation += 1" # Bump generation count. |
| 343 | echo |
| 344 | |
| 345 | # ------- Display second generation. ------- |
| 346 | Cur=`echo ${initial[@]}` |
| 347 | next_gen "$Cur" # Update & display. |
| 348 | sleep $DELAY |
| 349 | # ------------------------------------------ |
| 350 | |
| 351 | let "generation += 1" # Increment generation count. |
| 352 | |
| 353 | # ------ Main loop for displaying subsequent generations ------ |
| 354 | while [ "$generation" -le "$GENERATIONS" ] |
| 355 | do |
| 356 | Cur="$avar" |
| 357 | next_gen "$Cur" |
| 358 | let "generation += 1" |
| 359 | sleep $DELAY |
| 360 | done |
| 361 | # ============================================================== |
| 362 | |
| 363 | echo |
| 364 | # } |
| 365 | |
| 366 | exit 0 # CEOF:EOF |
| 367 | |
| 368 | |
| 369 | |
| 370 | # The grid in this script has a "boundary problem." |
| 371 | # The the top, bottom, and sides border on a void of dead cells. |
| 372 | # Exercise: Change the script to have the grid wrap around, |
| 373 | # + so that the left and right sides will "touch," |
| 374 | # + as will the top and bottom. |
| 375 | # |
| 376 | # Exercise: Create a new "gen0" file to seed this script. |
| 377 | # Use a 12 x 16 grid, instead of the original 10 x 10 one. |
| 378 | # Make the necessary changes to the script, |
| 379 | #+ so it will run with the altered file. |
| 380 | # |
| 381 | # Exercise: Modify this script so that it can determine the grid size |
| 382 | #+ from the "gen0" file, and set any variables necessary |
| 383 | #+ for the script to run. |
| 384 | # This would make unnecessary any changes to variables |
| 385 | #+ in the script for an altered grid size. |
| 386 | # |
| 387 | # Exercise: Optimize this script. |
| 388 | # It has redundant code. |