-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.sh
123 lines (116 loc) · 2.6 KB
/
tictactoe.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
#!/bin/bash
PORT="9901"
HOST="localhost"
KSPACE=20
function Draw ()
{
eval 'clear'
for i in 1 2 3
do
echo ''
for j in 1 2 3
do
if [ "${variable[$i$j]}" == "" ]
then
echo -n '_ '
else
echo -n "${variable[$i$j]} "
fi
done
done
}
function ToNet {
echo $1 | nc $HOST $PORT
}
function NetListen
{
nc -l $PORT
}
function endGame
{
echo -e "\nendGame"
if [ $1 == $my ]; then
echo "Вы победили!!!"
elif [ $1 == $enemy ]; then
echo "Вы проиграли (xa-xa)"
fi
echo "Ничья"
exit 0
}
function checkForWin
{
if [ "${variable[11]}" != "" ] && [ "${variable[11]}" == "${variable[12]}" ] && [ "${variable[12]}" == "${variable[13]}" ]; then
endGame "${variable[11]}"
fi
if [ "${variable[21]}" != "" ] && [ "${variable[21]}" == "${variable[22]}" ] && [ "${variable[22]}" == "${variable[23]}" ]; then
endGame "${variable[21]}"
fi
if [ "${variable[31]}" != "" ] && [ "${variable[31]}" == "${variable[32]}" ] && [ "${variable[32]}" == "${variable[33]}" ]; then
endGame "${variable[31]}"
fi
if [ "${variable[11]}" != "" ] && [ "${variable[11]}" == "${variable[21]}" ] && [ "${variable[21]}" == "${variable[31]}" ]; then
endGame "${variable[11]}"
fi
if [ "${variable[12]}" != "" ] && [ "${variable[12]}" == "${variable[22]}" ] && [ "${variable[22]}" == "${variable[32]}" ]; then
endGame "${variable[12]}"
fi
if [ "${variable[13]}" != "" ] && [ "${variable[13]}" == "${variable[23]}" ] && [ "${variable[23]}" == "${variable[33]}" ]; then
endGame "${variable[13]}"
fi
if [ "${variable[11]}" != "" ] && [ "${variable[11]}" == "${variable[22]}" ] && [ "${variable[22]}" == "${variable[33]}" ]; then
endGame "${variable[11]}"
fi
if [ "${variable[31]}" != "" ] && [ "${variable[31]}" == "${variable[22]}" ] && [ "${variable[22]}" == "${variable[13]}" ]; then
endGame "${variable[31]}"
fi
for i in 1 2 3
do
for j in 1 2 3
do
if [ "${variable[$i$j]}" == "" ]
then
return
fi
done
done
endGame "fig"
}
ToNet "x"
if [ "$(NetListen)" == "x" ]; then
ourMove=1
my='x'
enemy='0'
ToNet "0"
else
ourMove=0
my='0'
enemy='x'
fi
Draw
while [ 1 ]; do
if [ "$ourMove" -eq 1 ]; then
checkForWin
echo -e "\nВаш ход"
read x
read y
ToNet $x$y
if [ "${variable[$x$y]}" = "" ]; then
variable[$x$y]=$my
Draw
ourMove=0
else
echo -e "\nНельзя сделать такой ход"
fi
else
checkForWin
echo -e "\nЖдите..."
code=$(NetListen)
if [ "${variable[$code]}" = "" ]; then
variable[code]=$enemy
Draw
ourMove=1
else
echo -e "Нельзя сделать такой ход"
fi
fi
done