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
| #!/bin/bash
#//déclaration des cellules
#//déclarer le tableau
echo -e "\033[96mgame of life"
declare -A tab
declare -A tabfutur
echo "choisir la cellule"
read CELL
echo "combien de ligne"
read line
echo "combien de colonne"
read colonne
######## FONCTION D'INITIALISATION TABLEAU #######
fonction_init()
{
for((x=1;x<=line;x++)); do
for((y=1;y<=colonne;y++)); do
tab[$x,$y]="."
done
done
}
tabfutur_function()
{
for ((x=1;x<=line;x++)); do
for ((y=1;y<=colonne;y++)); do
tabfutur[$x,$y]=" ";
done
done
}
######## FONCTION D'AFFICHER TABLEAU #########
fonction_afficher()
{
clear
echo ""
echo -e "\033[96mgame of life"
f1="%$((${#line}+1))s"
f2="%3s"
for((x=1;x<=line;x++)); do
printf "$f2"
done
echo " "
for((y=1;y<=colonne;y++)); do
printf "$f1"
for((x=1;x<=line;x++)); do
printf "$f2" ${tab[$x,$y]}
done
echo " "
done
}
############### DEMANDE COORDONNEE #############
fonction_demande()
{
fonction_init
echo ""
echo ""
while :
do
echo -n " Entre les coordonnées ex: [1,1] : "
echo ">"
read -r coord
if [ "$coord" == "n" ]; then
fonction_calcul
fonction_afficher
else
if [[ "$coord" =~ ^[0-9]+,[0-9]+$ ]] ; then x=${coord%, *} ; y=${coord#*, } ;
tab[$coord]="${CELL}"
fonction_afficher
echo ""
else
fonction_demande
fi
fi
done
}
fonction_afficher
############## START ########################
start_function()
{
fonciton_calcul
}
############ CALCUL CELLULE ###############
fonction_calcul()
{
while true; do
for((x=1;x<=line;x++)); do
for((y=1;y<=colonne;y++)); do
Compt=0
# Voisine (x-1,y-1)
[[ "${tab[$(($x-1)),$(($y-1))]}" == $CELL ]] && ((Compt++))
# Voisine (x,y-1)
[[ "${tab[$(($x)),$(($y-1))]}" == $CELL ]] && ((Compt++))
# Voisine (x+1,y-1)
[[ "${tab[$(($x+1)),$(($y-1))]}" == $CELL ]] && ((Compt++))
# Voisine (x-1,y)
[[ "${tab[$(($x-1)),$(($y))]}" == $CELL ]] && ((Compt++))
# Voisine (x+1,y)
[[ "${tab[$(($x+1)),$(($y))]}" == $CELL ]] && ((Compt++))
# Voisine (x-1,y+1)
[[ "${tab[$(($x-1)),$(($y+1))]}" == $CELL ]] && ((Compt++))
# Voisine (x, y+1)
[[ "${tab[$(($x));$(($y+1))]}" == $CELL ]] && ((Compt++))
# Voisine (x+1,y+1)
[[ "${tab[$(($x+1)),$(($y+1))]}" == $CELL ]] && ((Compt++))
# Si vivante et deux voisines, elle survit
[[ ${tab[$(($x)),$(($y))]} == $CELL ]] && [[ ${Compt} -eq 2 ]] && tab[{x},${y}]=$CELL
# Si 3 voisines, soit survie, soit naissance
[[ ${Compt} -eq 3 ]] && tab[${x},${y}]=$CELL
# Si entre 4 et 8, mort par surpopulation
[[ ${Compt} -ge 4 ]] && [[ ${Compt} -le 8 ]] && tab[${x},${y}]="."
done
done
for((x=1;x<=line;x++)); do
for((y=1;y<=colonne;y++)); do
tab[x,y]=tabfutur[x,y]
fonction_afficher
done
done
done
}
############################# EXIT ################################
fonction_exit()
{
exit_fonction
}
#####################################################################
fonction_init
fonction_afficher
fonction_demande |
Partager