-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.lua
105 lines (90 loc) · 3.07 KB
/
Game.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
local class = require("middleclass")
Game = class('Game')
function Game:initialize(board, turn, symbols, depth, squares)
self.turns = board
self.symbols = symbols
self.depth = depth
self.turn = turn
self.squares = squares
_V = {turns = self.turns, symbols = self.symbols, depth = self.depth, turn = self.turn, squares = self.squares} --exclusively used for get and set variable methods
end
function Game:resetVariablesTable()
_V = {turns = self.turns, symbols = self.symbols, depth = self.depth, turn = self.turn, squares = self.squares} --exclusively used for get and set variable methods
end
function Game:setVariable(variableName, value, args)
if args ~= null then
_V[variableName][args] = value
else
_V[variableName] = value
end
end
function Game:status() --checks the game status (win, neutral, lose)
local boardStatus = -1
for i=7,1,-3 do
--print(i,i+1,i+2) --uncomment to understand the loops functionality
if (self.turns[i] == 1 and self.turns[i+1] == 1 and self.turns[i+2] == 1) or (self.turns[i] == 2 and self.turns[i+1] == 2 and self.turns[i+2] == 2) then
--checks for horizontal win
boardStatus = 1
end
end
for i=1,3 do
--[[uncomment the two lines below to understand how these work
print(i,i+3,i+6)
print('-------')
]]--
if (self.turns[i] == 1 and self.turns[i+3] == 1 and self.turns[i+6] == 1) or (self.turns[i] == 2 and self.turns[i+3] == 2 and self.turns[i+6] == 2) then
--checks for vertical win
boardStatus = 1
end
end
for i=3,1,-2 do
--[[uncomment the two lines below to understand how these work
print('-------')
print(i,5,10-i)]]--
if (self.turns[i] == 1 and self.turns[5] == 1 and self.turns[10-i] == 1) or (self.turns[i] == 2 and self.turns[5] == 2 and self.turns[10-i] == 2) then
--checks for a Cross win
boardStatus = 1
end
end
--now we check for draw
if table.getn(self.symbols) == 9 and boardStatus == -1 then
--its a draw fellas
boardStatus = 2
end
return boardStatus
end
function Game:switchTurns(currentTurn)
if currentTurn == null then
currentTurn = self.turn
end
if currentTurn > 1 then --switch turns
self.turn = 1
else
self.turn = 2
end
end
function Game:checkMouseHit(mouseX, mouseY)
local squareKey = null
for key,value in pairs(self.squares) do
if mouseX < value[1]+115 and
mouseY < value[2]+115 and
value[1] < mouseX and
value[2] < mouseY then -- simply put, check if the clicked mouse location is inside the square
squareKey = key
break
end
end
return squareKey
end
function Game:validateInput(squareKey)
local isValid = true
if squareKey == null then
return false
end
for i,v in ipairs(self.symbols) do
if v[2] == squareKey then -- loop through the drawn symbols, if we match something, break to return false
return false
end
end
return isValid
end