-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttt.lua
executable file
·212 lines (156 loc) · 4.26 KB
/
ttt.lua
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
#!/usr/bin/env lua
--[[
Yes, Tic-Tac-Toe! This is just a silly script to show how
to sub-class and use the Grid class object.
]]
require "grid"
-- This function 'sub-classes' the Grid object, adding
-- new methods for display and such.
function TicTacToe()
local g = grid.Grid(3, 3, " ")
g.allowed = {"O", "X"}
-- For checking diagonal wins. See check_winner().
g.diag_tl_to_br = {{1,1}, {2, 2}, {3, 3}}
g.diag_tr_to_bl = {{1,3}, {2, 2}, {3, 1}}
g._row_y_line = " 1 2 3 "
g._row_line = " +---+---+---+"
g._row_pattern = "%s | %s | %s | %s |"
function g:is_empty(x, y)
return (self:get_cell(x, y) == " ")
end
function g:check_allowed(piece)
if type(piece) ~= "string" then return false end
for _, v in self.allowed do
if piece == v then return true end
end
return false
end
-- Checks for a winner on all rows, columns, and diagonals.
-- Returns the winning piece or the string "tie" in the case
-- of a tie.
function g:check_winner()
local piece = nil
local row, col, diag
-- Check rows.
for x=1, self.size_x do
row = self:get_row(x)
if (self:check_allowed(row[1]) and
row[1] == row[2] and row[2] == row[3]) then
return row[1]
end
end
-- Check Columns
for y=1, self.size_y do
col = self:get_column(y)
if (self:check_allowed(col[1]) and
col[1] == col[2] and col[2] == col[3]) then
return col[1]
end
end
--[[ Check Diagonals. ]]
-- Top left to bottom right.
diag = self:get_cells(self.diag_tl_to_br)
if diag then
if (self:check_allowed(diag[1]) and
diag[1] == diag[2] and diag[2] == diag[3]) then
return diag[1]
end
end
-- Top right to bottom left.
diag = self:get_cells(self.diag_tr_to_bl)
if diag then
if (self:check_allowed(diag[1]) and
diag[1] == diag[2] and diag[2] == diag[3]) then
return diag[1]
end
end
-- Lastly, we check for a tie.
-- Get all the neighbors of 2,2, if they are all not empty
-- -and- 2, 2 is not empty, and there is no winner above,
-- it's a tie, man, it's a tie...
if self:get_cell(2, 2) ~= " " then
local nbrs = self:get_neighbors(2, 2)
local tie = true
local x, y, p
for _, v in nbrs do
x, y, p = unpack(v)
if p == " " then tie = false end
end
if tie then return "tie" end
end
return false
end
function g:display()
local row, s
print("")
print(self._row_y_line)
print(self._row_line)
for x=1, self.size_x do
row = self:get_row(x)
s = string.format(self._row_pattern, x, unpack(row))
print(s)
print(self._row_line)
end
print("")
end
-- Return the new sub-class object
return g
end
-- Lua lacks a decent built-in stdin input() function.
-- Roll our own.
function get_input(str)
local input = nil
local num = nil
if type(str) ~= "string" then str = "Input: " end
io.stdout:write(str)
input = io.stdin:read("*l")
input = string.sub(input, 1)
num = tonumber(input)
if num ~= nil then
return num
else
if input == "q" then
return input
end
end
return nil
end
function main()
local input, row, col
local T = TicTacToe()
local piece = "X"
print("Enter 'q' at any time to quit.")
while true do
T:display()
print(string.format("It is %s's turn.", piece))
row = get_input("Enter Row: ")
if row == "q" then break end
col = get_input("Enter Column: ")
if col == "q" then break end
if row ~= nil and col ~= nil then
if T:is_valid(row, col) and T:is_empty(row, col) then
T:set_cell(row, col, piece)
winner = T:check_winner()
if winner and winner ~= "tie" then
T:display()
print(string.format("The winner is %s!", piece))
break
elseif winner == "tie" then
T:display()
print("The game was a tie!")
break
end
if piece == "X" then
piece = "O"
else
piece = "X"
end
else
local s = "Row %s and Column %s is not valid, please try again!"
print(string.format(s, row, col))
end
end
end
end
-- Call main
main()