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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| #!/bin/bash
Generation=0
num_lines=0
num_columns=0
CELL=0
declare -A tab
declare -A tabfutur #Tempory array that will receive the future stat of each cell.
####################### HEADER #########################
# This function displays the header.
header_function()
{
echo ""
echo " ═══════════════════ Conway's Game of Life ═══════════════════ "
echo ""
}
#################### ARRAY FUNCTION #####################
# This function initalizes the array and fill it with randomly 0 and 1 or with nothing.
init_array_function()
{
for ((x=1;x<=num_lines;x++))
do
for ((y=1;y<=num_columns;y++))
do
if [ $cell == 'a' ]
then
tab[$x,$y]=$((RANDOM %2))
else
tab[$x,$y]=
fi
done
done
}
#################### DISPLAY FUNCTION ####################
# This function displays the array.
display_function()
{
#header_function
clear
echo ""
f1="%$((${#num_lines}+1))s"
f2="%3s"
printf "$f1"
for ((x=1;x<=num_columns;x++)) # -1
do
#printf "$f2" "$x" #Comment this line to delete the display of the abscissa.
toto=1 #This line is only here to avoid errors in the for loop when the abscissa is disabled.
done
echo
for ((y=1;y<=num_lines;y++))
do
#printf "$f1" $y #Comment this line to delete the display of the ordinate.
for ((x=1;x<=num_columns;x++))
do
printf "$f2" "${tab[$x,$y]}"
done
echo
done
echo ""
echo " > Génération : $generation "
((generation++))
}
#################### ASK COORDINATES ####################
# This function asks the user to enter the coordinates of the cell ad type 's' to run the game.
coord_function()
{
echo ""
echo ""
while [ "$coord" != "s" ]
do
echo -n " + Place your cell using the array coordinates ( like '1,1' ) or press 's' to start : "
read coord
if [[ "$coord" =~ ^[0-9]+,[0-9]+$ ]] ; then x=${coord%, *} ; y=${coord#*, } ; #2 crochets
tab[$coord]="${CELL}"
display_function
echo ""
fi
done
start_function
}
################################ ARRAY ANALIZE ################################
# This function analyze the array and calculates the different neighbours.
array_analyze_function()
{
tput civis
for ((x=1;x<=num_columns;x++))
do
for ((y=1;y<=num_lines;y++))
do
if [ $x -eq 1 ]
then
xg=$num_columns
# echo "$xg"
else
xg=$(($x-1))
# echo "$xg"
fi
if [ $x -eq $num_columns ]
then
xd=1
# echo "$xd"
else
xd=$(($x+1))
# echo "$xd"
fi
if [ $y -eq 1 ]
then
yh=$num_lines
# echo "$yh"
else
yh=$(($y-1))
# echo "$yh"
fi
if [ $y -eq $num_lines ]
then
yb=1
# echo "$yb"
else
yb=$(($y+1))
# echo "$yb"
fi
Counter=0
# Neighbour (xg,yb)
[[ "${tab[$((xg)),$((yb))]}" == $CELL ]] && ((Counter++)) #(x-1),(y-1)
# Neighbour (x, yb)
[[ "${tab[$((x)),$((yb))]}" == $CELL ]] && ((Counter++)) #(x),(y-1)
# Neighbour (xd,yb)
[[ "${tab[$((xd)),$((yb))]}" == $CELL ]] && ((Counter++)) # (x+1), (y-1)
# Neighbour (xg,y)
[[ "${tab[$((xg)),$((y))]}" == $CELL ]] && ((Counter++)) # (x-1), (y)
# Neighbour (xd,y)
[[ "${tab[$((xd)),$((y))]}" == $CELL ]] && ((Counter++)) # (x+1), (y)
# Neighbour (xg,yb)
[[ "${tab[$((xg)),$((yh))]}" == $CELL ]] && ((Counter++)) # (x-1), (y+1)
# Neighbour (x,yh)
[[ "${tab[$((x)),$((yh))]}" == $CELL ]] && ((Counter++)) # (x), (y+1)
# Neighbour (xd,yh)
[[ "${tab[$((xd)),$((yh))]}" == $CELL ]] && ((Counter++)) # (x+1), (y+1)
# If the cell is alive and with two neighbours, she survives.
[ ${Counter} -eq 2 ] && tabfutur[${x},${y}]=${tab[$x,$y]}
# If the cell has 3 neighbours, either it survives or a cell is born.
[ ${Counter} -eq 3 ] && tabfutur[${x},${y}]=$CELL
# If th cell is alive with maximum one neighbourd, she dies.
if [ $cell == 'a' ]
then
[ ${Counter} -le 1 ] && tabfutur[${x},${y}]=" "
else
[ ${Counter} -le 1 ] && tabfutur[${x},${y}]=""
fi
# If a cell has between four and eight neighbours, she dies.
if [ $cell == 'a' ]
then
[ ${Counter} -ge 4 ] && [ ${Counter} -le 8 ] && tabfutur[${x},${y}]=" "
else
[ ${Counter} -ge 4 ] && [ ${Counter} -le 8 ] && tabfutur[${x},${y}]=""
fi
######################### TEST BORDER #########################
################################################################
done
done
for ((x=1;x<=num_lines;x++))
do
for ((y=1;y<=num_columns;y++))
do
tab[$x,$y]=${tabfutur[$x,$y]}
done
done
}
#################### HOMEPAGE ####################
# This function asks the user to enter the numbers of lines, of columns, the form of the cell or type 'a' to fill in the array randomly.
homepage_function()
{
header_function
echo " How many lines ? "
echo -n " > "
read Lines
until [[ $Lines =~ ^[0-9]+$ ]]
do
echo -e "\033[91m > Error ! Retry ! <\033[0m "
echo ""
echo " How many lines ? "
echo -n " > "
read -r Lines
done
echo ""
echo " How many columns ? "
echo -n " > "
read Columns
until [[ $Columns =~ ^[0-9]+$ ]]
do
echo -e "\033[91m > Error ! Retry ! <\033[0m "
echo ""
echo " How many Columns ? "
echo -n " > "
read -r Columns
done
echo ""
echo " Put the cell's form or press 'a' to fill randomly the array "
echo -n " > "
read -r cell
while [ -z "$cell" ]
do
echo -e "\033[91m > Error ! Retry ! <\033[0m "
echo ""
echo " Cell's form ? "
echo -n " > "
read -r cell
done
Generation=0
num_lines=$Lines
num_columns=$Columns
CELL=$CELL
}
#################### RUN & START FUNCTION ####################
# This function starts the analysis of the array.
start_function()
{
while true
do
display_function
array_analyze_function
done
}
# This function is the basic function. It allows you to launch all the other functions.
run()
{
homepage_function
init_array_function
display_function
coord_function
}
################################ MAIN ################################
clear
echo ""
echo "╔═══════════════════════ Conway's Game of Life ═══════════════════════╗"
echo "║ ║"
echo "║ This is the Bash script version of John Conway's Game of Life. ║"
echo "║ Life is a simple implementation of cellular automata. ║"
echo "║ ║"
echo "╠═══════════════════════════════════════════════════════════════════════╣"
echo "║ ║"
echo "║ Rules : ║"
echo "║ ║"
echo "║ [1] A living cell with either 2 or 3 living neighbors remains alive. ║"
echo "║ ║"
echo "║ [2] A dead cell with 3 living neighbors comes alive. ║"
echo "║ ║"
echo "║ [2] All other cases result in a dead cell for the next generation. ║"
echo "║ ║"
echo "╚═══════════════════════════════════════════════════════════════════════╝"
echo ""
echo ""
read -p "> Press any key to continue ... "
clear
run |