Pages : 1
#1 Le 17/12/2018, à 15:30
- Zethra
[RESOLU] Aide script BASH
Bonjour,
Je suis en BTS SIO en alternance afin de devenir admnistrateur-système.
Mon tuteur, avant de me lancer dans un gros projet, veut que je sois opérationnel en bash ( où du moins, que je sache me débrouiller un minimum ). Pour cela, il me donne des séries d'exercices. L'étape ultime est d'arriver à faire un jeu de la vie en bash ( pour ceux qui ne connaissent pas => https://www.youtube.com/watch?v=S-W0NX97DB0 ). Là, je suis arrivé à l'étape juste avant le jeu de la vie et je dois faire un Morpion en utilisant des tableaux.
Je m'excuse d'avance pour la longueur des codes ! Voilà où j'en étais il y a quelques jours :
#!/bin/bash
declare -A Tableau=([1,1]='' [1,2]='' [1,3]='' [2,1]='' [2,2]='' [2,3]='' [3,1]='' [3,2]='' [3,2]='' [3,3]='')
################# QUITTER OU RECOMMENCER UNE PARTIE ###################
fonction_recommencer()
{
echo ""
echo " ================================================= "
echo " Que voulez-vous faire ? "
echo ""
echo " [1] - Recommencer une partie "
echo " [2] - Quitter "
echo -n " > "
read CHOIX
case $CHOIX in
1) fonction_debut ;;
2) exit ;;
esac
}
##################### ENTRER LE NOM DES JOUEURS ########################
fonction_debut()
{
echo -n " + Veuillez entrer le nom du Joueur 1 : "
read J1
echo -e " > Le joueur 1 est $J1 ! Bon jeu !\n "
echo -n " + Veuillez entrer le nom du Joueur 2 : "
read J2
echo -e " > Le joueur 2 est $J3 ! Bon jeu !\n "
fonction_grille
}
############################## TABLEAU ##################################
#declare -A Tableau=([1,1]='' [1,2]='' [1,3]='' [2,1]='' [2,2]='' [2,3]='' [3,1]='' [3,2]='' [3,2]='' [3,3]='')
############################## GRILLE ####################################
fonction_grille()
{
echo ""
echo " 1 2 3 "
echo " =========|=========|========= "
echo " A ${Tableau[1,1]} | ${Tableau[1,2]} | ${Tableau[1,3]} "
echo " =========|=========|========= "
echo " B ${Tableau[2,1]} | ${Tableau[2,2]} | ${Tableau[2,3]} "
echo " =========|=========|========= "
echo " C ${Tableau[3,1]} | ${Tableau[3,2]} | ${Tableau[3,3]} "
echo " =========|=========|========= "
echo ""
fonction_choix
}
############################## FONCTION CHOIX JOUEURS ####################################
fonction_choix ()
{
if [[ $Joueur < 2 ]] ; then fonction_joueur_1 ; else fonction_joueur_2 ; fi
}
fonction_calcul_gagnant_J1 ()
{
if [ "${Tableau[1,1]}" = "x" ] && [ "${Tableau[1,2]}" = "x" ] && [ "${Tableau[1,3]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[2,1]}" = "x" ] && [ "${Tableau[2,2]}" = "x" ] && [ "${Tableau[2,3]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[3,1]}" = "x" ] && [ "${Tableau[3,2]}" = "x" ] && [ "${Tableau[3,3]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[1,1]}" = "x" ] && [ "${Tableau[2,1]}" = "x" ] && [ "${Tableau[3,1]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[1,1]}" = "x" ] && [ "${Tableau[2,2]}" = "x" ] && [ "${Tableau[3,3]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[3,1]}" = "x" ] && [ "${Tableau[2,2]}" = "x" ] && [ "${Tableau[1,3]}" = "x" ] ; then echo "" ; echo " $J1 a gagné la partie ! " ; fonction_recommencer ; fi
fonction_grille
}
fonction_calcul_gagnant_J2 ()
{
if [ "${Tableau[1,1]}" = "o" ] && [ "${Tableau[1,2]}" = "o" ] && [ "${Tableau[1,3]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[2,1]}" = "o" ] && [ "${Tableau[2,2]}" = "o" ] && [ "${Tableau[2,3]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[3,1]}" = "o" ] && [ "${Tableau[3,2]}" = "o" ] && [ "${Tableau[3,3]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[1,1]}" = "o" ] && [ "${Tableau[2,1]}" = "o" ] && [ "${Tableau[3,1]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[1,1]}" = "o" ] && [ "${Tableau[2,2]}" = "o" ] && [ "${Tableau[3,3]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
if [ "${Tableau[3,1]}" = "o" ] && [ "${Tableau[2,2]}" = "o" ] && [ "${Tableau[1,3]}" = "o" ] ; then echo "" ; echo " $J2 a gagné la partie ! " ; fonction_recommencer ; fi
fonction_grille
############################ FONCTION JOUEUR #############################
################ J1 #################
fonction_joueur_1()
{
Joueur=2
echo " + $J1 : C'est votre tour ! Où voulez-vous jouer ? "
echo -n " > "
read VALEUR1
case $VALEUR1 in
A1) Tableau[1,1]="x" ;;
A2) Tableau[1,2]="x" ;;
A3) Tableau[1,3]="x" ;;
B1) Tableau[2,1]="x" ;;
B2) Tableau[2,2]="x" ;;
B3) Tableau[2,3]="x" ;;
C1) Tableau[3,1]="x" ;;
C2) Tableau[3,2]="x" ;;
C3) Tableau[3,3]="x" ;;
esac
fonction_calcul_gagnant_J1
fonction_calcul_gagnant_J2
}
############ J2 ############
fonction_joueur_2()
{
Joueur=1
echo " + $J2 : C'est votre tour ! Où voulez-vous jouer ? "
echo -n " > "
read VALEUR2
case $VALEUR2 in
A1) Tableau[1,1]="o" ;;
A2) Tableau[1,2]="o" ;;
A3) Tableau[1,3]="o" ;;
B1) Tableau[2,1]="o" ;;
B2) Tableau[2,2]="o" ;;
B3) Tableau[2,3]="o" ;;
C1) Tableau[3,1]="o" ;;
C2) Tableau[3,2]="o" ;;
C3) Tableau[3,3]="o" ;;
esac
fonction_calcul_gagnant_J2
echo ""
echo " + --------------------------------------------- + "
echo " + <<< MORPION >>> + "
echo " + --------------------------------------------- + "
echo
echo
fonction_debut
Mon code n'est clairement pas parfait, mais ils fonctionne. Le problème, c'est que mes tuteurs ne voulaient pas ce genre de tableau. Ils me demandaient de créer des tableaux à multidimensions, donc en gros, un tableau dans un tableau. Voici ce que j'ai pu récupérer et qui est exactement le type de tableau qu'on me demande :
#!/bin/bash
2 declare -A matrix
3 num_rows=4
4 num_columns=5
5
6 for ((i=1;i<=num_rows;i++)) do
7 for ((j=1;j<=num_columns;j++)) do
8 matrix[$i,$j]=NULL
9 done
10 done
11
12 f1="%$((${#num_rows}+1))s"
13 f2=" %9s"
14
15 printf "$f1" ''
16 for ((i=1;i<=num_rows;i++)) do
17 printf "$f2" $i
18 done
19 echo
20
21 for ((j=1;j<=num_columns;j++)) do
22 printf "$f1" $j
23 for ((i=1;i<=num_rows;i++)) do
24 printf "$f2" ${matrix[$i,$j]}
25 done
26 echo
27 done
De ce fait, voilà ce que j'ai maintenant :
#!/bin/bash
declare -A tab
num_lines=3
num_columns=3
########## ARRAY FUNCTION ##########
array_function()
{
for ((x=1;x<=num_lines;x++)) do
for ((y=1;y<=num_columns;y++)) do
tab[$x,$y]=+
done
done
display_function
gamer_choice_function
}
########## END OR RESTART FUNCTION ##########
end_restart_function()
{
echo " + Que voulez-vous faire ? "
echo " --------------------------- "
echo " [1] - Quitter "
echo " [2] - Recommencer "
echo -n " > "
read CHOIX
case $CHOIX in
1) exit ;;
2) start_function ;;
esac
########## GAME START FUNCTION ##########
start_function()
{
echo -n " + Veuillez entrer le nom de Joueur 1 : "
read J1
echo -e " > Le premier joueur est : $J1 ! Bonne chance !\n "
echo -n " + Veuillez entrer le nom de Joueur 2 : "
read J2
if [[ "$J2" = "$J1" ]]
then
echo " Vous ne pouvez pas avoir le même nom que le joueur 1 ! Veuillez recommecer ! "
echo -n " Veuillez entrer le nom de Joueur 2 : > "
read J2
echo " Le deuxième joueur est : $J2 ! Bonne chance ! "
else
echo -e " > Le deuxième joueur est : $J2 ! Bonne chance !\n "
fi
display_function
array_function
}
########## GAMER CHOICE ##########
gamer_choice_function()
{
if [[ $Joueur < 2 ]]
then
J1_function
else
J2_function
fi
}
########## DISPLAY FUNCTION ##########
display_function()
{
f1="%$((${#num_lines}+1))s"
f2="%5s"
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
gamer_choice_function
array_function
J1_function
J2_function
}
########## DISPLAY FUNCTION ##########
#display_function()
#{
#echo ""
#echo " 1 2 3 "
#echo " =========|=========|========= "
#echo " A ${tab[1,1]} | ${tab[2,1]} | ${tab[3,1]} "
#echo " =========|=========|========= "
#echo " B ${tab[1,2]} | ${tab[2,2]} | ${tab[3,2]} "
#echo " =========|=========|========= "
#echo " C ${tab[1,3]} | ${tab[2,3]} | ${tab[3,3]} "
#echo " =========|=========|========= "
#echo ""
#array_function
#gamer_choice_function
#}
########## GAMERS WIN FUNCTION ##########
########## - J1 - ##########
J1_win_function()
{
if [ "${tab[1,1]}" = "x" ] && [ "${tab[1,2]}" = "x" ] && [ "${tab[1,3]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[2,1]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[2,3]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[3,1]}" = "x" ] && [ "${tab[3,2]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[1,1]}" = "x" ] && [ "${tab[2,1]}" = "x" ] && [ "${tab[3,1]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[1,2]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,2]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[1,3]}" = "x" ] && [ "${tab[2,3]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[1,1]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,3]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
if [ "${tab[1,3]}" = "x" ] && [ "${tab[2,2]}" = "x" ] && [ "${tab[3,1]}" = "x" ] ; then echo "" ; ech " $J1 a gagné la partie ! " ; end_start_function ; fi
array_function
display_function
}
########## GAMERS FUNCTION ##########
########## - J1 - ##########
J1_function()
{
Joueur=2
echo " + $J1, c'est votre tour ! Où voulez-vous jouer ? "
echo -n " > "
read VALUE1
case $VALUE1 in
A1) tab[1,1]="x";;
A2) tab[2,1]="x";;
A3) tab[3,1]="x";;
B1) tab[2,1]="x";;
B2) tab[2,2]="x";;
B3) tab[2,3]="x";;
C1) tab[3,1]="x";;
C2) tab[3,2]="x";;
C3) tab[3,3]="x";;
*) echo " Erreur ! Veuillez recommencer ! "
J1_function ;;
esac
J1_win_function
J2_win_function
}
########## - J2 - ##########
J2_function()
{
Joueur=1
echo " + $J2, c'est votre tour ! Où voulez-vous jouer ? "
echo -n " > "
read VALUE2
case $VALUE2 in
A1) tab[1,1]="o";;
A2) tab[2,1]="o";;
A3) tab[3,1]="o";;
B1) tab[2,1]="o";;
B2) tab[2,2]="o";;
B3) tab[2,3]="o";;
C1) tab[3,1]="o";;
C2) tab[3,2]="o";;
C3) tab[3,3]="o";;
*) echo " Erreur ! Veuillez recommencer ! "
J2_function ;;
esac
J2_win_function
J1_win_function
}
echo ""
echo " + --------------------------------------------- + "
echo " + <<< MORPION >>> + "
echo " + --------------------------------------------- + "
echo ""
start_function
Le problème, c'est que j'arrive pas à " cibler " la case que je veux. Comment faire comprendre que, quand le joueur 1 tape " A1 " c'est la case 1;1 qui doit être remplie d'un X ? Avec un tableau classique, j'y arrive, mais là, le fait que ce soit dans une loop, je n'y arrive pas !
Auriez-vous quelques indications pour m'aiguiller ?
Merci d'avance !
Dernière modification par Zethra (Le 02/01/2019, à 14:02)
Hors ligne
#2 Le 17/12/2018, à 16:18
- kholo
Re : [RESOLU] Aide script BASH
salut,
toutes tes fonctions ne sont pas fermées : array_function
tu as mis des do en fin de ligne sans ";"
for ((x=1;x<=num_lines;x++)) do
=> soit tu mets le do à la ligne ou tu ajoutes le ";"
for ((x=1;x<=num_lines;x++)) ; do
ou
for ((x=1;x<=num_lines;x++))
do
display_function couine !
./jeu_de_la_vie.sh: ligne 103: erreur de syntaxe : expression arithmétique nécessaire
./jeu_de_la_vie.sh: ligne 103: erreur de syntaxe : « ((x=1,x<=num_lines;x++)) »
ici :
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
*************************************************************
EDIT : pour la saisie
c'est fait à l'arrache mais tu sauras arranger :
# echo "entrer x"
read -n 1 -p "entrer x " X
echo
# echo "entrer y"
read -n 1 -p "entrer y " Y
echo
echo "c'est la case $X $Y "
Dernière modification par kholo (Le 17/12/2018, à 16:25)
Hors ligne
#3 Le 17/12/2018, à 18:25
- Hizoka
Re : [RESOLU] Aide script BASH
Salut,
j'ai pas lu ton code, mais perso je ferais un truc du genre :
declare -A Ligne1
declare -A Ligne2
declare -A Ligne3
Ligne1["a"]=" "
Ligne1["b"]=" "
Ligne1["c"]=" "
Ligne2["a"]=" "
Ligne2["b"]=" "
Ligne2["c"]=" "
Ligne3["a"]=" "
Ligne3["b"]=" "
Ligne3["c"]=" "
for ((tour=1; tour < 10; tour++))
do
Lettre="X"
[[ $((tour%2)) -eq 0 ]] && Lettre="O"
read -n 2 -p "Mettre un ${Lettre} dans la case : "
echo
Ligne=${REPLY:1}
Colonne=${REPLY:0:1}
Colonne=${Colonne,,}
case ${Ligne} in
1) Ligne1[${Colonne}]="${Lettre}" ;;
2) Ligne2[${Colonne}]="${Lettre}" ;;
3) Ligne3[${Colonne}]="${Lettre}" ;;
*) echo "Erreur dans la ligne !"; ((tour--));;
esac
done
echo "${Ligne1['a']} | ${Ligne1['b']} | ${Ligne1['c']}
${Ligne2['a']} | ${Ligne2['b']} | ${Ligne2['c']}
${Ligne3['a']} | ${Ligne3['b']} | ${Ligne3['c']}"
Reste plus qu'a vérifier que la colonne est bien a, b ou c et de vérifier que la case voulue est vide.
Et puis vérifier si l'un des joueurs à gagner
En espérant que ça puisse t'inspirer...
EDIT :
Un bon gros IF
if [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne1["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 1ere ligne."
break
elif [[ ${Ligne2["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne2["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 2e ligne."
break
elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne3["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 3e ligne."
break
elif [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne1["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 1ere colonne."
break
elif [[ ${Ligne2["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne2["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 2e colonne."
break
elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne3["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 3e colonne."
break
elif [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} en diagonale."
break
elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]]
then
echo "${Lettre} à gagner en alignant ses ${Lettre} en diagonale."
break
fi
Vérification de l'état de la case
case ${Ligne} in
1)
if [[ ${Ligne1[${Colonne}]} != " " ]]
then
echo "Case déjà prise !"
((tour--))
else
Ligne1[${Colonne}]="${Lettre}"
fi ;;
...
esac
Vérification de la colonne
if [[ ! ${Colonne} =~ [abc] ]]
then
echo "Cette colonne n'existe pas !"
((tour--))
fi
Dernière modification par Hizoka (Le 17/12/2018, à 18:39)
KDE Neon 64bits
Tous mes softs (MKVExtractorQt, HizoSelect, HizoProgress, Qtesseract, Keneric, Services menus...) sont sur github
Hors ligne
#4 Le 18/12/2018, à 09:08
- Zethra
Re : [RESOLU] Aide script BASH
Merci pour vos retours ! Je voir ce que je peux faire avec tout ça !
Salut,
j'ai pas lu ton code, mais perso je ferais un truc du genre :declare -A Ligne1 declare -A Ligne2 declare -A Ligne3 Ligne1["a"]=" " Ligne1["b"]=" " Ligne1["c"]=" " Ligne2["a"]=" " Ligne2["b"]=" " Ligne2["c"]=" " Ligne3["a"]=" " Ligne3["b"]=" " Ligne3["c"]=" " for ((tour=1; tour < 10; tour++)) do Lettre="X" [[ $((tour%2)) -eq 0 ]] && Lettre="O" read -n 2 -p "Mettre un ${Lettre} dans la case : " echo Ligne=${REPLY:1} Colonne=${REPLY:0:1} Colonne=${Colonne,,} case ${Ligne} in 1) Ligne1[${Colonne}]="${Lettre}" ;; 2) Ligne2[${Colonne}]="${Lettre}" ;; 3) Ligne3[${Colonne}]="${Lettre}" ;; *) echo "Erreur dans la ligne !"; ((tour--));; esac done echo "${Ligne1['a']} | ${Ligne1['b']} | ${Ligne1['c']} ${Ligne2['a']} | ${Ligne2['b']} | ${Ligne2['c']} ${Ligne3['a']} | ${Ligne3['b']} | ${Ligne3['c']}"
Reste plus qu'a vérifier que la colonne est bien a, b ou c et de vérifier que la case voulue est vide.
Et puis vérifier si l'un des joueurs à gagnerEn espérant que ça puisse t'inspirer...
EDIT :
Un bon gros IFif [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne1["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 1ere ligne." break elif [[ ${Ligne2["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne2["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 2e ligne." break elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne3["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 3e ligne." break elif [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne1["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 1ere colonne." break elif [[ ${Ligne2["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne2["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 2e colonne." break elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne3["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} sur la 3e colonne." break elif [[ ${Ligne1["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne3["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} en diagonale." break elif [[ ${Ligne3["a"]} == ${Lettre} && ${Ligne2["b"]} == ${Lettre} && ${Ligne1["c"]} == ${Lettre} ]] then echo "${Lettre} à gagner en alignant ses ${Lettre} en diagonale." break fi
Vérification de l'état de la case
case ${Ligne} in 1) if [[ ${Ligne1[${Colonne}]} != " " ]] then echo "Case déjà prise !" ((tour--)) else Ligne1[${Colonne}]="${Lettre}" fi ;; ... esac
Vérification de la colonne
if [[ ! ${Colonne} =~ [abc] ]] then echo "Cette colonne n'existe pas !" ((tour--)) fi
J'étais parti l'a dessus au début... Mais comme le but c'est de pouvoir m'inspirer de mon code pour faire un Jeu de la vie, si je déclare toutes les lignes comme ça, ça risque d'être un peu long ! Je dois pouvoir faire une boucle for avec, à l'intérieur, une autre boucle for, avec deux variables déclarées au préalable permettant de donner le nombre de lignes et de colonnes, histoire de pouvoir faire un tableau aussi grand que nécessaire !
Néanmoins, je vais quand même regarder ton script pour voir ce que je peux modifier dans le miens !
Hors ligne
#5 Le 18/12/2018, à 18:07
- Hizoka
Re : [RESOLU] Aide script BASH
Je vais me faire engueuler avec eval (:p) mais :
for Ligne in 1 2 3
do
declare -A Ligne${Ligne}
for Lettre in a b c
do
eval Ligne${Ligne}[${Lettre}]="${Lettre}${Ligne}"
done
done
for Ligne in 1 2 3
do
for Lettre in a b c
do
Temp="Ligne${Ligne}['${Lettre}']"
echo "${!Temp}"
done
done
a1
b1
c1
a2
b2
c2
a3
b3
c3
Dernière modification par Hizoka (Le 18/12/2018, à 18:08)
KDE Neon 64bits
Tous mes softs (MKVExtractorQt, HizoSelect, HizoProgress, Qtesseract, Keneric, Services menus...) sont sur github
Hors ligne
#6 Le 19/12/2018, à 16:05
- Zethra
Re : [RESOLU] Aide script BASH
Merci pour vos retours !
Je suis arrivé à un résultat plutôt satisfaisant :
#!/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
Petite question : dès qu'un joueur gagne, le dernier tableau s'affiche, ça indique " X won the game " et ça demande ce qu'on veut faire :
J'aimerai que dans l'affichage du dernier tableau, les pions qui forment la ligne soient affiché en rouge. Changer la couleur du texte dans un script bash je sais le faire en utilisant un echo, mais là, comme il n'y en a pas, comment dois-je faire ?
Je suppose que je dois toucher à la fonction " win_function " mais je ne sais pas trop comment faire...
Auriez-vous une idée ?
Dernière modification par Zethra (Le 19/12/2018, à 16:05)
Hors ligne