-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.lua
More file actions
106 lines (92 loc) · 1.9 KB
/
Copy pathTicTacToe.lua
File metadata and controls
106 lines (92 loc) · 1.9 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
--
-- Created by IntelliJ IDEA.
-- User: Sebastian
-- Date: 14.10.2016
-- Time: 21:14
-- To change this template use File | Settings | File Templates.
--
boardSize = 3
player1 = "x"
player2 = "o"
empty = " "
horizontalSeparator = "-"
verticalSeparator = "|"
board = {}
for i = 0, (boardSize - 1) do
board[i] = {}
for j = 0, (boardSize -1) do
board[i][j] = empty
end
end
function getPiece(x,y)
if (x < boardSize and x > -1 and y < boardSize and y > -1) then
if board[x][y] ~= nil then
return board[x][y]
else return empty
end
else
return empty
end
end
function isEmpty(x,y)
return getPiece(x,y) == empty
end
function placePiece(x, y, piece)
if isEmpty(x,y) then
board[x][y] = piece
return true
else
return false
end
end
function fullBoard()
for i = 0, (boardSize - 1) do
for j = 0, (boardSize - 1) do
if isEmpty(i,j) then
return false
end
end
end
end
function displayBoard()
io.write("*---*")
io.write("\n")
for i = 0, (boardSize -1) do
local row = "|"
for j = 0, (boardSize -1) do
row = row .. getPiece(i,j)
end
io.write(row .. "|")
io.write("\n")
end
io.write("*---*")
end
function getRow(rowNumber)
local row = ""
for i = 0, (boardSize-1) do
row = row .. board[rowNumber][i]
end
return row
end
function getColumn(columnNumber)
local column = ""
for i in board do
end
end
function getDiagonalA()
row = ""
row = row .. board[0][0]
row = row .. board[1][1]
row = row .. board[2][2]
return row
end
function getDiagonalB()
return board[0][0] .. board[1][1] .. board[2][0]
-- return row
end
placePiece(1,0,player1)
placePiece(1,1,player2)
placePiece(2,2,player1)
displayBoard()
io.write("\n")
io.write(getDiagonalA())