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 | -- aaden
print("Input current board")
local h = io.read()
local s = ""
local count = {
["."] = 0,
["x"] = 0,
["o"] = 0,
}
for i in string.gmatch(h, "([%.xo])") do
s = s..i
count[i] = count[i] + 1
end
function entry(board)
local function format(string)
return string.sub(string, 1, 3).."\n"..string.sub(string, 4, 6).."\n"..string.sub(string, 7, 9)
end
local function tile(x, y)
return string.sub(board, x + ((y - 1) * 3), x + ((y - 1) * 3))
end
-- checking for valid horizontal wins
for i=1, 3 do
local period = 0
for j=1, 3 do
local slot = tile(j, i)
if slot == "o" then
period = -1
break
elseif slot == "." then
period = period + 1
end
end
if period == 1 then
return format(string.sub(board, 0, (i - 1) * 3).."xxx"..string.sub(board, ((i + 1) * 3) + 1, -1))
end
end
-- checking for valid vertical wins
for i=1, 3 do
local period = 0
for j=1, 3 do
local slot = tile(i, j)
if slot == "o" then
period = -1
break
elseif slot == "." then
period = period + 1
end
end
if period == 1 then
return format(string.sub(board, 0, i - 1).."x"..string.sub(board, i + 1, i + 2).."x"..string.sub(board, i + 4, i + 5).."x"..string.sub(board, i + 7, -1))
end
end
-- diagonal 1
local period = 0
for i=1, 3 do
local slot = tile(4 - i, i)
if slot == "o" then
break
elseif slot == "." then
period = period + 1
end
if i == 3 and period == 1 then
return format(string.sub(board, 1, 2).."x"..string.sub(board, 4, 4).."x"..string.sub(board, 6, 6).."x"..string.sub(board, 8, 9))
end
end
-- diagonal 2
period = 0
for i=1, 3 do
local slot = tile(i, i)
if slot == "o" then
break
elseif slot == "." then
period = period + 1
end
if i == 3 and period == 1 then
return format("x"..string.sub(board, 2, 4).."x"..string.sub(board, 6, 8).."x")
end
end
-- doing the same for player O so player X can prevent it
-- checking for valid horizontal wins
for i=1, 3 do
local period = 0
local periodpos = {0, 0}
for j=1, 3 do
local slot = tile(j, i)
if slot == "x" then
period = -1
break
elseif slot == "." then
period = period + 1
periodpos = {j, i}
end
end
if period == 1 then
return format(string.sub(board, 0, (periodpos[1] - 1) + ((periodpos[2] - 1) * 3)).."x"..string.sub(board, (periodpos[1] + 1) + ((periodpos[2] - 1) * 3), -1))
end
end
-- checking for valid vertical wins
for i=1, 3 do
local period = 0
local periodpos = {0, 0}
for j=1, 3 do
local slot = tile(i, j)
if slot == "x" then
period = -1
break
elseif slot == "." then
period = period + 1
periodpos = {i, j}
end
end
if period == 1 then
return format(string.sub(board, 0, (periodpos[1] - 1) + ((periodpos[2] - 1) * 3)).."x"..string.sub(board, (periodpos[1] + 1) + ((periodpos[2] - 1) * 3), -1))
end
end
-- diagonal 1
period = 0
local periodpos = {0, 0}
for i=1, 3 do
local slot = tile(4 - i, i)
if slot == "x" then
break
elseif slot == "." then
period = period + 1
periodpos = {4 - i, i}
end
if i == 3 and period == 1 then
return format(string.sub(board, 0, (periodpos[1] - 1) + ((periodpos[2] - 1) * 3)).."x"..string.sub(board, (periodpos[1] + 1) + ((periodpos[2] - 1) * 3), -1))
end
end
-- diagonal 2
period = 0
periodpos = {0, 0}
for i=1, 3 do
local slot = tile(i, i)
if slot == "x" then
break
elseif slot == "." then
period = period + 1
periodpos = {i, i}
end
if i == 3 and period == 1 then
return format(string.sub(board, 0, (periodpos[1] - 1) + ((periodpos[2] - 1) * 3)).."x"..string.sub(board, (periodpos[1] + 1) + ((periodpos[2] - 1) * 3), -1))
end
end
-- method 1
if tile(3, 1) == "." and tile(3, 2) == "." and tile(1, 3) ~= "o" and tile(1, 2) ~= "o" and tile(2, 2) ~= "o" then
if tile(1, 3) == "." then
return format(string.sub(board, 1, 6).."x"..string.sub(board, 8, 9))
elseif tile(1, 2) == "." then
return format(string.sub(board, 1, 3).."x"..string.sub(board, 5, 9))
elseif tile(2, 2) == "." then
return format(string.sub(board, 1, 4).."x"..string.sub(board, 6, 9))
end
end
-- method 2
if tile(2, 2) == "o" then
if tile(3, 1) == "." then
return format(string.sub(board, 1, 2).."x"..string.sub(board, 4, 9))
elseif tile(1, 1) == "." then
return format("x"..string.sub(board, 2, 9))
elseif tile(3, 3) == "." then
return format(string.sub(board, 1, 8).."x")
elseif tile(1, 3) == "." then
return format(string.sub(board, 1, 6).."x"..string.sub(board, 8, 9))
end
end
local candidates = {}
for i=1, 9 do
if string.sub(board, i, i) == "." then
candidates[#candidates+1] = i
end
end
if #candidates > 0 then
local chosen = candidates[math.random(1, #candidates)]
return format(string.sub(board, 0, chosen - 1).."x"..string.sub(board, chosen + 1, 9))
else
return "Nothing to do..."
end
end
print(entry(s))
|
post a comment