Bonjour,

Je suis en BTS ISO en alternance afin de devenir administrateur système linux. Afin d'être " opérationnel " et pour commencer à faire des choses intéressantes, mon tuteur me fait apprendre le bash. D'abord via de simple exerce, puis maintenant via un Morpion avec un tableau à multi-dimenssion afin de finir sur le jeu de la vie de John Horton Conway.


Je suis actuellement à l'étape du jeu de la vie, puisque j'ai terminé le morpion. Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/bin/bash
 
declare -A tab
 
num_lines=3
num_columns=3
 
 
########## ARRAY FUNCTION ##########
init_array_function()
{
for ((x=1;x<=num_lines;x++)) 
do
        for ((y=1;y<=num_columns;y++))
	do 	tab[$x,$y]="";
 
        done
done
 
}
 
 
 
########## END OR RESTART FUNCTION  ##########
end_restart_function()
{
display_function
echo ""
echo " + What do you want to do ? "
echo " ---------------------------- "
echo ""
echo " [1] - Leave "
echo " [2] - Start again "
echo ""
echo -n " > "
read CHOIX
 
case $CHOIX in
	1) exit ;;
	2) start_function ;;
	*) echo " Error ! Retry ! "
	end_restart_function ;;
esac
}
 
########## GAME START FUNCTION  ##########
start_function()
{
init_array_function
echo -n " + Player 1 name : "
read P1
echo -e " > The first player is : $P1 ! Good Luck !\n "
 
echo -n " + Player 2 name : "
read P2
 
if [[ "$P2" = "$P1"  ]]
then
	echo " + Error ! Retry ! "
	echo -n " + Player 2 name : > "
	read P2
 	echo " > The second player is : $P2 ! Good Luck ! "	   
else
echo -e " > The second player is : $P2 ! Good Luck !\n "
fi
display_function
player_choice_function
}
 
########## GAMER CHOICE ##########
player_choice_function()
{
if [[ $Player < 2 ]]
then	
	P1_function
else
	P2_function
fi
}
 
########## DISPLAY FUNCTION ##########
display_function()
{
 
f1="%$((${#num_lines}+1))s"
f2="%9s"
 
printf "$f1"
for ((x=1;x<=num_lines;x++))
do
	printf "$f2" $x
done
echo
 
for ((y=1;y<=num_columns;y++)) 
do
	printf "$f1" $y
	for ((x=1;x<=num_lines;x++)) 
	do
		printf "$f2" ${tab[$x,$y]}
	done
	echo
done
}
 
########## PLAYERS WIN FUNCTION ##########
win_function()
{
########## - J1 - ##########
if [ "${tab[1,1]}" = "x" ] && [ "${tab[2,1]}" = "x" ] && [ "${tab[3,1]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,2]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,2]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,3]}" = "x" ] && [ "${tab[2,3]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
 
if [ "${tab[1,1]}" = "x" ] && [ "${tab[1,2]}" = "x" ] && [ "${tab[1,3]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[2,1]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[2,3]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[3,1]}" = "x" ] && [ "${tab[3,2]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
 
if [ "${tab[1,1]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,3]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,1]}" = "x" ] ; then echo "" ; echo "" ; echo " $P1 won the game ! " ; echo ; end_restart_function ; fi
 
########## - J2 - ##########
if [ "${tab[1,1]}" = "o" ] && [ "${tab[2,1]}" = "o" ] && [ "${tab[3,1]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,2]}" = "o" ] && [ "${tab[2,2]}" = "o" ] && [ "${tab[3,2]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,3]}" = "o" ] && [ "${tab[2,3]}" = "o" ] && [ "${tab[3,3]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
 
if [ "${tab[1,1]}" = "o" ] && [ "${tab[1,2]}" = "o" ] && [ "${tab[1,3]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[2,1]}" = "o" ] && [ "${tab[2,2]}" = "o" ] && [ "${tab[2,3]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[3,1]}" = "o" ] && [ "${tab[3,2]}" = "o" ] && [ "${tab[3,3]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
 
if [ "${tab[1,1]}" = "o" ] && [ "${tab[2,2]}" = "o" ] && [ "${tab[3,3]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
if [ "${tab[1,3]}" = "o" ] && [ "${tab[2,2]}" = "o" ] && [ "${tab[3,1]}" = "o" ] ; then echo "" ; echo "" ; echo " $P2 won the game ! " ; echo ; end_restart_function ; fi
display_function
player_choice_function
}
 
########## GAMERS FUNCTION ##########
########## - P1 - ##########
P1_function()
{
Player=2
echo " + $P1, it's your turn ! "
echo -n " > "
read VALUE1
 
case $VALUE1 in
	1,1) if [ "${tab[1,1]}" = "x" ] || [ "${tab[1,1]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[1,1]="x" ; fi ;;
	2,1) if [ "${tab[2,1]}" = "x" ] || [ "${tab[2,1]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[2,1]="x" ; fi ;;
	3,1) if [ "${tab[3,1]}" = "x" ] || [ "${tab[3,1]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[3,1]="x" ; fi ;;
	1,2) if [ "${tab[1,2]}" = "x" ] || [ "${tab[1,2]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[1,2]="x" ; fi ;;
	2,2) if [ "${tab[2,2]}" = "x" ] || [ "${tab[2,2]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[2,2]="x" ; fi ;;
	3,2) if [ "${tab[3,2]}" = "x" ] || [ "${tab[3,2]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[3,2]="x" ; fi ;;
        1,3) if [ "${tab[1,3]}" = "x" ] || [ "${tab[1,3]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[1,3]="x" ; fi ;;
        2,3) if [ "${tab[2,3]}" = "x" ] || [ "${tab[2,3]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[2,3]="x" ; fi ;;
        3,3) if [ "${tab[3,3]}" = "x" ] || [ "${tab[3,3]}" = "o" ] ; then echo " The cell is already used ! Retry ! " ; P1_function ; else  tab[3,3]="x" ; fi ;;
 
	*) echo " Error ! Retry ! " ; P1_function ;;
esac
win_function
}
########## - P2 - ##########
P2_function()
{
Player=1
echo " + $P2, it's your turn ! "
echo -n " > "
read VALUE2
 
case $VALUE2 in
	1,1) if [ "${tab[1,1]}" = "o" ] || [ "${tab[1,1]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[1,1]="o" ; fi ;;
        2,1) if [ "${tab[2,1]}" = "o" ] || [ "${tab[2,1]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[2,1]="o" ; fi ;;
        3,1) if [ "${tab[3,1]}" = "o" ] || [ "${tab[3,1]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[3,1]="o" ; fi ;;
        1,2) if [ "${tab[1,2]}" = "o" ] || [ "${tab[1,2]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[1,2]="o" ; fi ;;
        2,2) if [ "${tab[2,2]}" = "o" ] || [ "${tab[2,2]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[2,2]="o" ; fi ;;
        3,2) if [ "${tab[3,2]}" = "o" ] || [ "${tab[3,2]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[3,2]="o" ; fi ;;
        1,3) if [ "${tab[1,3]}" = "o" ] || [ "${tab[1,3]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[1,3]="o" ; fi ;;
        2,3) if [ "${tab[2,3]}" = "o" ] || [ "${tab[2,3]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[2,3]="o" ; fi ;;
        3,3) if [ "${tab[3,3]}" = "o" ] || [ "${tab[3,3]}" = "x" ] ; then echo " The cell is already used ! Retry ! " ; P2_function ; else  tab[3,3]="o" ; fi ;;
 
	*) echo " Error ! Retry !  " ; P2_function ;;
esac
win_function
}
 
 
echo ""
echo " + --------------------------------------------- + "
echo " +                <<< MORPION >>>                + "
echo " + --------------------------------------------- + "
echo ""
 
start_function



Mon code n'est peut-être pas parfait, mail il est opérationnel. Maintenant, le but est de récupérer quelques fonctions apprises avec le morpion et de les intégrer dans le code du jeu de la vie. Du coup, je compte récupérer les fonctions " init_array_function " et " display_array ".

Le fonctionnement du jeu de la vie fait que :

  • Si une cellule morte est entourée de trois cellules voisines vivantes, cette cellule devient vivante.
  • Une cellule vivante entourée par deux ou trois cellules vivantes reste vivante, sinon elle meurt.


En tenant compte de ces informations, j'imagine le jeu de la manière suivante :

  • Pouvoir changer, dès le lancement du script, la taille du tableau en modifiant les variables num_lines et num_columns du code de mon Morpion, afin d'avoir un tableau aussi grand que souhaité.
  • Remplir un tableau à plusieurs dimensions de manière aléatoire, avec des 0 et des 1. Le 0 étant une cellule morte et le 1 une cellule vivante.
  • Que l'utilisateur puisse déplacer le curseur comme il le souhaite dans le tableau avec les flèches du clavier afin de pouvoir placer des cellules vivantes là où il le souhaite.



De ce fait, voilà où j'en suis :


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/bin/bash
 
echo " How many lines ? "
echo -n " > "
read Lines
 
echo " How many columns ? "
echo -n " > "
read Columns
 
declare -A tab
 
num_lines=$Lines
num_columns=$Columns
 
########## ARRAY FUNCTION ##########
init_array()
{
for ((x=1;x<=num_lines;x++))
do
        for ((y=1;y<=num_columns;y++))
        do
                tab[$x,$y]=$(( RANDOM % 2 ));
        done
done
}
 
######## DISPLAY FUNCTION ##########
display_function()
{
f1="%$((${#num_lines}+1))s"
f2="%3s"
 
printf "$f1"
for ((x=1;x<=num_lines;x++))
do
        printf "$f2" $x
done
echo
 
for ((y=1;y<=num_columns;y++))
do
        printf "$f1" $y
        for ((x=1;x<=num_lines;x++))
        do
                printf "$f2" ${tab[$x,$y]}
        done
        echo
done
}
 
init_array
display_function


Ce qui donne :

Nom : 1545831239-3.png
Affichages : 1188
Taille : 4,8 Ko


Donc super, pour le moment tout fonctionne. Mais je ne sais pas faire certaines choses et c'est là que j'aimerai votre aide :

  • Comment faire en sorte que les 0 et les 1 qui remplissent le tableau soient cachés pour ne laisser apparaître que ce que l'utilisateur va saisir ?
  • Comment faire en sorte que, une fois le script lancé et le nombres de lignes et de colonnes donnés, le curseur soit placé en 1;1 sur le tableau afin de pouvoir le déplacer comme on le veut avec les flèches du clavier ?




Merci d'avance !