-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.sh
214 lines (199 loc) · 5.39 KB
/
tic_tac_toe.sh
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
#!/bin/bash
# Author: LMI
# Date: 13/0CT/2021
# Version 1.6
# history: 1.5 removed computer thinking display.
# 1.6 LEVEL 1 done.
# Todo: Level2...
# variables globales.
turn=0
declare -A tableau
#initialise tableau
for i in {1..9}
do
tableau[$i]="-"
done
#affiche tableau
affiche_tableau( ){
#echo ${!tableau[@]}
echo " ___ ___ ___"
echo " | | | |"
echo " | ${tableau[1]} | ${tableau[2]} | ${tableau[3]} |"
echo " |___|___|___|"
echo " | | | |"
echo " | ${tableau[4]} | ${tableau[5]} | ${tableau[6]} |"
echo " |___|___|___|"
echo " | | | |"
echo " | ${tableau[7]} | ${tableau[8]} | ${tableau[9]} |"
echo " |___|___|___|"
echo
echo " 1 2 3"
echo " 4 5 6"
echo " 7 8 9"
echo
return
}
# ordi joue au (hasard ou calculé)
ordijoue(){
# 123
# 456
# 789
# todo: intelligence à définir. (4 niveaux)
# premier à jouer ?
# si oui
if [ $turn -eq 1 ];then
echo "tour 1"
# choisir une case du jeu
# 1) 5
# 2) 1379
# 3) 2468
# 4) 1234456789
fi
case $LEVEL in
0)
# LEVEL 0
# ordi choisi au hazard parmi 9 choix
valid=0
while [ $valid == 0 ]
do
dest=$((1 + $RANDOM % 9))
# test si vide ?
test_coup_valide $dest ordi
done
;;
1)
# LEVEL 1
# choix dans l'ordre: le centre, les coins, les bords
# soit 5,1,3,7,9,2,4,6,8
valid=0
while [ $valid == 0 ]
do
bestplay=5,1,3,7,9,2,4,6,8
for i in ${bestplay//,/ }
do
dest=$i
test_coup_valide $dest ordi
if [ $valid -eq 1 ];then
break
fi
done
done
;;
esac
tableau[$dest]="O"
echo ordi joue : $dest
# TODO: LEVEL 2
# si non contrer le jeu en fonction des 8 solutions
# odds: + + - - - - _ _
# 1 7 1 1 7 3 4 2
# 5 5 4 2 8 6 5 5
# 9 3 7 3 9 9 6 8
# le joueur joue dans le coin
# alors ordi joue a coté.
# stratégies :
# Win: If the player has two in a row, they can place a third to get three in a row.
# Block: If the opponent has two in a row, the player must play the third themselves to block the opponent.
# Fork: Cause a scenario where the player has two ways to win (two non-blocked lines of 2).
# Blocking an opponent's fork: If there is only one possible fork for the opponent, the player should block it. Otherwise, the player should block all forks in any way that simultaneously allows them to make two in a row. Otherwise, the player should make a two in a row to force the opponent into defending, as long as it does not result in them producing a fork. For example, if "X" has two opposite corners and "O" has the center, "O" must not play a corner move to win. (Playing a corner move in this scenario produces a fork for "X" to win.)
# Center: A player marks the center. (If it is the first move of the game, playing a corner move gives the second player more opportunities to make a mistake and may therefore be the better choice; however, it makes no difference between perfect players.)
# Opposite corner: If the opponent is in the corner, the player plays the opposite corner.
# Empty corner: The player plays in a corner square.
# Empty side: The player plays in a middle square on any of the four sides.
#
return $dest
}
test_coup_valide(){
if [ ${tableau[$1]} = "-" ];then
valid=1
else
if [ $2 = "joueur" ];then
echo "Choix impossible"
fi
fi
return $valid
}
choisir_case(){
valid=0
while [ $valid == 0 ]
do
read -r -p "Quelle case ? " case
# test si vide ?
test_coup_valide $case joueur
done
return $case
}
# joueur joue
joueurjoue(){
affiche_tableau
# q? entrez votre case ?
case=&choisir_case
#echo choix : $case
tableau[$case]="X"
return
}
# test si gagne
testgagne(){
# vérification si une des 8 solutions a 3 x ou 3 o
# 1 7 1 1 7 3 4 2
# 5 5 4 2 8 6 5 5
# 9 3 7 3 9 9 6 8
winning=1-5-9,7-5-3,1-4-7,1-2-3,7-8-9,3-6-9,4-5-6,2-5-8
for i in ${winning//,/ }
do
IFS=- read A B C <<< "$i"
if [ ${tableau[$A]} != '-' ] && [ ${tableau[$A]} == ${tableau[$B]} ] && [ ${tableau[$B]} == ${tableau[$C]} ]; then
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo "${tableau[$A]} WINS ! ${tableau[$A]} ${tableau[$B]} ${tableau[$C]}"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
affiche_tableau
exit
fi
done
return
}
#########################
# MAIN
#########################
# choix du niveau de l'ordinateur
LEVEL=0
valid=0
while [ $valid == 0 ]
do
read -r -p "Niveau de difficulté (0,1,2) ? " LEVEL
case $LEVEL in
0) echo "LEVEL $LEVEL (Facile)"
valid=1
;;
1) echo "LEVEL $LEVEL (moins facile)"
valid=1
;;
*) echo "NOT READY YET"
sleep 1
;;
esac
done
# random qui commence
JOUEUR=$((1 + $RANDOM % 2))
case $JOUEUR in
1) echo ordinateur commence;
J=0
;;
2) echo joueur commence;
J=1
;;
esac
### c'est parti pour 9 tours de jeu
for i in {1..9}
do
turn=$i
#echo tour de jeu: $turn
if [ $J -eq 0 ];then
J=1
ordijoue
else
J=0
joueurjoue
fi
testgagne
done
affiche_tableau