-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.lua
74 lines (61 loc) · 1.44 KB
/
tic_tac_toe.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
turns = 0
grid = {}
for y = 1, 3 do
grid[y] = {" ", " ", " "}
end
function check_win()
local turn = get_turn()
-- Rows
for y = 1, 3 do
if turn == grid[y][1] and grid[y][1] == grid[y][2] and grid[y][2] == grid[y][3] then
return true
end
end
-- Columns
for x = 1, 3 do
if turn == grid[1][x] and grid[1][x] == grid[2][x] and grid[2][x] == grid[3][x] then
return true
end
end
-- Diagonals
if turn == grid[1][1] and grid[1][1] == grid[2][2] and grid[2][2] == grid[3][3] then
return grid[1][1]
elseif turn == grid[1][3] and grid[1][3] == grid[2][2] and grid[2][2] == grid[3][1] then
return true
end
end
function draw_board()
print("\n\t 1 2 3\n")
for i = 1, 3 do
print("\t" .. i .. " " .. grid[i][1] .. " | " .. grid[i][2] .. " | " .. grid[i][3])
if i < 3 then
print("\t ---+---+---")
end
end
end
function get_turn()
if turns % 2 == 0 then
return "O"
else
return "X"
end
end
while true do
draw_board()
print("\nIt's " .. get_turn() .. " turn! Type a coordinate! Ex: \"2 3\":")
local x, y = io.read("*n", "*n")
if x and y and x > 0 and y > 0 and x < 4 and y < 4 and grid[y][x] == " " then
grid[y][x] = get_turn()
if turns > 3 and check_win() then
draw_board()
print(get_turn() .. " won!")
break
end
turns = turns + 1
if (turns == 9) then
draw_board()
print("Draw!")
break
end
end
end