-
Notifications
You must be signed in to change notification settings - Fork 0
/
t3C
executable file
·176 lines (176 loc) · 6.45 KB
/
t3C
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
#!/bin/bash
#
# Title: TICTACTOE ... the GAME! (For two players, x and o.)
# Filename: t3C
# Usage: At the prompt, /bin/bash /path/to/t3C or /path/to/t3C
# Author: JLA Uro ([email protected])
# Date: 10 February 2014 (Feast of Our Lady of Lourdes)
# Revised: 13Feb2014; 01Oct2014 (St. Therese of Lisieux); 12Aug2017 (St. Jane Frances de Chantal)
# 02Sep2018
#
###
##
#
#BLACK='\e[1;30m'
#RED='\e[1;31m'
#GREEN='\e[1;32m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
BOLD='\e[1;39m'
BLINK='\e[5m'
OBLINK='\e[25m'
NORM='\e[0m'
echo -e ""
echo -e "${BOLD}TICTACTOE ... the GAME!${NORM}"
echo -e "For 2 players: ${BLUE}x${NORM} and ${YELLOW}o${NORM}. First ${BLUE}x${NORM}, then ${YELLOW}o${NORM}."
winsx=0
winso=0
ngames=0
while true
do
ngames=$((ngames + 1))
declare -a sq # declare sq an array
sq=(1 2 3 4 5 6 7 8 9) # initial sqaare numbers in sq
echo -e ""
echo -e "Current Grid:" # echo modified grid
echo -e "-------------"
echo -e "| ${sq[0]} | ${sq[1]} | ${sq[2]} |"
echo -e "-------------"
echo -e "| ${sq[3]} | ${sq[4]} | ${sq[5]} |"
echo -e "-------------"
echo -e "| ${sq[6]} | ${sq[7]} | ${sq[8]} |"
echo -e "-------------"
echo -e ""
sqStatus=" 1 2 3 4 5 6 7 8 9"
win=0 # win indicator
i=1 # input i = 1
x="${BLUE}x${NORM}"
o="${YELLOW}o${NORM}"
winx="$x $x $x" # def win x pattern
wino="$o $o $o" # def win o pattern
while [ $win -eq 0 ] # while no win yet
do
echo -e "Status of squares: ${sq[*]}" # print current sqs statuses
sqStatus=`echo "${sqStatus//${entry}/}"`
rtChoice=0
while [ $rtChoice -eq 0 ]
do
entryCode=0
while [ "$entryCode" -eq 0 ];
do # print available sq choices
echo -e "Remaining squares: $sqStatus"
if [ "$((i % 2))" != "0" ]; then
echo -e "Entry for $x?"
else
echo -e "Entry for $o?"
fi
read entry # input player's ith square (x,o/odd,even i)
# verify {1..9}
if [ "$entry" = "1" -o "$entry" = 2 -o "$entry" = "3" -o "$entry" = "4" -o "$entry" = "5" -o "$entry" = "6" -o "$entry" = "7" -o "$entry" = "8" -o "$entry" = "9" ]; then
entryCode=1
else
echo -e "You entered ${BOLD}\"$entry\"${NORM}. Entry error. Pls. re-enter ..."
entryCode=0
fi
done
# verify non-FILLED status of choice
if [ "${sq[${entry}-1]}" = "$x" -o "${sq[${entry}-1]}" = "$o" ]; then
echo -e "Square ${BOLD}\"$entry\"${NORM} is already FILLED!!!"
else
rtChoice=1
fi
done
if [ "$((i % 2))" != "0" ]; then # player x input; steps 1,3,5,7,9
sq[${entry}-1]="$x" # asign "x" to choice entry
echo -e ""
echo -e "Current Grid:" # echo modified grid
echo -e "-------------"
echo -e "| ${sq[0]} | ${sq[1]} | ${sq[2]} |"
echo -e "-------------"
echo -e "| ${sq[3]} | ${sq[4]} | ${sq[5]} |"
echo -e "-------------"
echo -e "| ${sq[6]} | ${sq[7]} | ${sq[8]} |"
echo -e "-------------"
echo -e ""
wr1="${sq[0]} ${sq[1]} ${sq[2]}" # determine 8 relevant patterns
wr2="${sq[3]} ${sq[4]} ${sq[5]}"
wr3="${sq[6]} ${sq[7]} ${sq[8]}"
wc1="${sq[0]} ${sq[3]} ${sq[6]}"
wc2="${sq[1]} ${sq[4]} ${sq[7]}"
wc3="${sq[2]} ${sq[5]} ${sq[8]}"
wd1="${sq[0]} ${sq[4]} ${sq[8]}"
wd2="${sq[2]} ${sq[4]} ${sq[6]}" # compare 8 relevant patterns to winx patterns
if [ "$wr1" = "$winx" -o "$wr2" = "$winx" -o "$wr3" = "$winx" ]; then
echo -e "${BOLD}hooorrrray${NORM} ... $x ${BLINK}wins!!!${OBLINK}" # print win message
echo -e ""
win=1 # set win var to 1 to end curr game
winsx=$((winsx + 1))
elif [ "$wc1" = "$winx" -o "$wc2" = "$winx" -o "$wc3" = "$winx" -o "$wd1" = "$winx" -o "$wd2" = "$winx" ]; then
echo -e "${BOLD}hooorrrray${NORM} ... $x ${BLINK}wins!!!${OBLINK}"
echo -e ""
win=1
winsx=$((winsx + 1))
fi
i=$((i + 1)) # increment step i
if [ $i -ge 10 -a $win = 0 ]; then # game ends in 9 or less steps
echo -e "${BOLD}${BLINK}No winner! ... ${OBLINK}${NORM}"
echo -e ""
win=1 # let win=1 to end curr game
fi
else
sq[${entry}-1]="$o" # player o input; steps 2,4,6,8
echo -e ""
echo -e "Current Grid:" # echo modified grid
echo -e "-------------"
echo -e "| ${sq[0]} | ${sq[1]} | ${sq[2]} |"
echo -e "-------------"
echo -e "| ${sq[3]} | ${sq[4]} | ${sq[5]} |"
echo -e "-------------"
echo -e "| ${sq[6]} | ${sq[7]} | ${sq[8]} |"
echo -e "-------------"
echo -e ""
wr1="${sq[0]} ${sq[1]} ${sq[2]}"
wr2="${sq[3]} ${sq[4]} ${sq[5]}"
wr3="${sq[6]} ${sq[7]} ${sq[8]}"
wc1="${sq[0]} ${sq[3]} ${sq[6]}"
wc2="${sq[1]} ${sq[4]} ${sq[7]}"
wc3="${sq[2]} ${sq[5]} ${sq[8]}"
wd1="${sq[0]} ${sq[4]} ${sq[8]}"
wd2="${sq[2]} ${sq[4]} ${sq[6]}"
if [ "$wr1" = "$wino" -o "$wr2" = "$wino" -o "$wr3" = "$wino" ]; then
echo -e "${BOLD}hooorrrray${NORM} ... $o ${BLINK}wins!!!${OBLINK}"
echo -e ""
win=1
winso=$((winso + 1))
elif [ "$wc1" = "$wino" -o "$wc2" = "$wino" -o "$wc3" = "$wino" -o "$wd1" = "$wino" -o "$wd2" = "$wino" ]; then
echo -e "${BOLD}hooorrrray${NORM} ... $o ${BLINK}wins!!!${OBLINK}"
echo -e ""
win=1
winso=$((winso + 1))
fi
i=$((i + 1)) # increment step i
fi
done
echo -e "Current score:"
echo -e " Total games played: $ngames"
echo -e " $x wins: $winsx"
echo -e " $o wins: $winso"
echo -e " no wins: $((ngames - winsx - winso))"
echo -e "Another game?[Y/n]"
read no
if [ "$no" = "n" -o "$no" = "N" -o "$no" = "no" -o "$no" = "No" ]; then
echo -e ""
echo -e "Here's hoping you enjoyed ${BOLD}TICTACTOE ... the GAME!${NORM}"
echo -e "Try it again soon!"
echo -e "THANK YOU!!!"
exit
else
echo -e "${BOLD}****************${NORM}"
echo -e "${BOLD}*** New Game ***${NORM}"
echo -e "${BOLD}****************${NORM}"
entry=""
fi
done
#
##
###