-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.sh
More file actions
149 lines (133 loc) · 4.17 KB
/
tictactoe.sh
File metadata and controls
149 lines (133 loc) · 4.17 KB
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
#!/bin/bash
# Author : LEOxian
# License: GPLv2
# Description: Tic Tac Toe Game
# Date : 23-March-2013
#trap 'echo -e "\033[00;30m \n\nGracias por jugar \n"; exit 127' SIGINT
#echo -e "\033[00;30m \n\nGracias por jugar \n"
COLOR_RED='\033[01;31m'
COLOR_GREEN='\033[01;32m'
COLOR_BLACK='\033[00;30m'
COLOR_BLACK_BOLD='\033[01;30m'
DEFAULT_COL='\033[01;30m'
array=( "" "" "" "" "" "" "" "" "")
win_msg () {
echo -e "${COLOR_GREEN}Felicidades $USER_NO tu Ganas!!${COLOR_BLACK}\n"
break ;
}
winning_rules () {
if [ $CELL_VALUE == "${array[0]}" ] && [ $CELL_VALUE == "${array[1]}" ] && [ $CELL_VALUE == "${array[2]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[3]}" ] && [ $CELL_VALUE == "${array[4]}" ] && [ $CELL_VALUE == "${array[5]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[6]}" ] && [ $CELL_VALUE == "${array[7]}" ] && [ $CELL_VALUE == "${array[8]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[0]}" ] && [ $CELL_VALUE == "${array[3]}" ] && [ $CELL_VALUE == "${array[6]}" ] ; then
win_msg;
elif [ $CELL_VALUE == "${array[1]}" ] && [ $CELL_VALUE == "${array[4]}" ] && [ $CELL_VALUE == "${array[7]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[2]}" ] && [ $CELL_VALUE == "${array[5]}" ] && [ $CELL_VALUE == "${array[8]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[0]}" ] && [ $CELL_VALUE == "${array[4]}" ] && [ $CELL_VALUE == "${array[8]}" ] ; then
win_msg
elif [ $CELL_VALUE == "${array[2]}" ] && [ $CELL_VALUE == "${array[4]}" ] && [ $CELL_VALUE == "${array[6]}" ] ; then
win_msg
fi
}
tie () {
for k in `seq 0 $( expr ${#array[@]} - 1) `
do
if [ ! -z ${array[$k]} ] ; then
tic_array[$k]=$k
if [ "9" -eq ${#tic_array[@]} ] ; then
echo -e "${COLOR_GREEN}\nTal vez tengas mejor suerte !!${COLOR_BLACK}\n"
exit
fi
fi
done
}
tic_board () {
clear
echo -e "\t${COLOR_BLACK_BOLD}*************************"
echo -e "\t*\t ${array[0]:-0} | ${array[1]:-1} | ${array[2]:-2}\t*"
echo -e "\t*\t____________\t*"
echo -e "\t*\t ${array[3]:-3} | ${array[4]:-4} | ${array[5]:-5}\t*"
echo -e "\t*\t____________\t*"
echo -e "\t*\t ${array[6]:-6} | ${array[7]:-7} | ${array[8]:-8}\t*"
echo -e "\t*************************\n${COLOR_BLACK}"
}
EMPTY_CELL () {
echo -e -n "${DEFAULT_COL}"
read -e -p "$MSG" col
case "$col" in
[0-8]) if [ -z ${array[$col]} ]; then
CELL_IS=empty
else
CELL_IS=notempty
fi;;
*) DEFAULT_COL=$COLOR_RED
MSG="$USER_NO Escribe un número entre 0 a 8 : "
EMPTY_CELL;;
esac
echo -e -n "${COLOR_BLACK}"
}
CHOISE () {
EMPTY_CELL
if [ "$CELL_IS" == "empty" ]; then
array[$col]="$CELL_VALUE"
else
DEFAULT_COL=${COLOR_RED}
MSG="La caja no puede estar vacía, Re-Enter $USER_NO : "
CHOISE
fi
}
PLAYER_NAME () {
if [ -z $USER1 ]; then
read -e -p "Introduce el nombre del primer jugador: " USER1
fi
if [ -z $USER2 ]; then
read -e -p "Introduce el nombre del segundo jugador: " USER2
fi
if [ -z $USER1 ] ; then
echo -e "${COLOR_RED}El nombre del jugandor no puede estar vacío"
PLAYER_NAME
elif [ -z $USER2 ]; then
echo -e "${COLOR_RED}El nombre del jugandor no puede estar vacío"
PLAYER_NAME
fi
}
# Main Program
tic_board
echo -e "${DEFAULT_COL}Bienvenido al juego Tic Tac toe"
echo -e "La reglas son, introducir un número en la caja entre 0 a 8"
read -n 1 -p "Para continuar escribe y : " y
echo -e "\n"
sleep 1
if [ "$y" == "y" ] || [ "$y" == "Y" ]; then
clear
echo -e "${COLOR_BLACK_BOLD}"
PLAYER_NAME
else
echo -e "\nGracias por usarlo !!"
exit
fi
tic_board
while :
do
((i++))
let value=$i % 2
if [ "$value" == "0" ]; then
USER_NO=$USER1
MSG="$USER_NO Introduce tu elección : "
CELL_VALUE="X"
CHOISE
else
USER_NO=$USER2
MSG="$USER_NO Introduce tu elección : "
CELL_VALUE="O"
CHOISE
fi
tic_board
winning_rules
tie
done