-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleBook.lua
More file actions
176 lines (148 loc) · 4.4 KB
/
RuleBook.lua
File metadata and controls
176 lines (148 loc) · 4.4 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
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
local RuleBook = {}
local function addIfValid(board, token, x, y, listOfMoves)
local foundToken = board:getToken(x, y)
if foundToken ~= nil then
if foundToken.owner ~= token.owner then
if not token.isMaster then
if (x+y+token.x+token.y)%2 ~= 0 then
table.insert(listOfMoves, {x=x, y=y})
end
else
table.insert(listOfMoves, {x=x, y=y})
end
end
return true
end
if not token.isMaster then
if (x+y+token.x+token.y)%2 ~= 0 then
table.insert(listOfMoves, {x=x, y=y})
end
else
table.insert(listOfMoves, {x=x, y=y})
end
return false
end
function RuleBook.listValidMoves(board, token)
local listOfMoves = {}
if token.isHorizontal then
-- To the left of the token
if token.x > 1 then
local y = token.y
for x=token.x-1, 1, -1 do
if addIfValid(board, token, x, y, listOfMoves) then break end
end
end
-- To the right of the token
if token.x < board.width then
local y = token.y
for x=token.x+1, board.width, 1 do
if addIfValid(board, token, x, y, listOfMoves) then break end
end
end
else
-- To the top of the token
if token.y > 1 then
local x = token.x
for y=token.y-1, 1, -1 do
if addIfValid(board, token, x, y, listOfMoves) then break end
end
end
-- To the bottom of the token
if token.y < board.height then
local x = token.x
for y=token.y+1, board.height, 1 do
if addIfValid(board, token, x, y, listOfMoves) then break end
end
end
end
return listOfMoves
end
function RuleBook.isValidMove(gameState, token, toX, toY)
local listOfValidMoves = RuleBook.listValidMoves(gameState.board, token)
for _, pos in ipairs(listOfValidMoves) do
if pos.x == toX and pos.y == toY then
return true
end
end
return false
end
function RuleBook.performMove(gameState, token, toX, toY)
-- TODO: Validate move?
local board = gameState.board
board:moveToken(token, toX, toY)
token.isHorizontal = not token.isHorizontal
if (toX == 1 and toY == 1) or
(toX == 1 and toY == board.height) or
(toX == board.width and toY == 1) or
(toX == board.width and toY == board.height) then
token.isMaster = true
if gameState.currentPlayer.firstMasterPromotionPlace == 322 then
gameState.currentPlayer.firstMasterPromotionPlace = gameState.promotionPlace
gameState.promotionPlace = gameState.promotionPlace + 1
end
end
end
local function isOnePlayerLeft(gameState)
local onePlayerLeft = true
local tokensList = gameState.board:getAllTokens()
for _, t in ipairs(tokensList) do
if t.owner ~= gameState.currentPlayer.owner then
onePlayerLeft = false
break
end
end
return onePlayerLeft
end
function RuleBook.isGameOver(gameState)
-- Check winning conditions
local tokensList = gameState.board:getAllTokens()
local minionsOnBoard = false
for _, t in ipairs(tokensList) do
if not t.isMaster then
minionsOnBoard = true
break
end
end
local onePlayerOnBoard = isOnePlayerLeft(gameState)
local isGameOver = minionsOnBoard == false or onePlayerOnBoard
return isGameOver
end
function RuleBook.getWinner(gameState)
local winner = nil
-- Determine the winner
if isOnePlayerLeft(gameState) then
print("The winner is (the only one left): " .. gameState.currentPlayer.owner)
winner = gameState.currentPlayer.owner
else
local mastersCountByOwner = {}
for _, t in ipairs(gameState.board:getAllTokens()) do
mastersCountByOwner[t.owner] = (mastersCountByOwner[t.owner] == nil) and 1 or (mastersCountByOwner[t.owner]+1)
end
local sortedMastersCount = {}
for owner, count in pairs(mastersCountByOwner) do
table.insert(sortedMastersCount, {["owner"] = owner, ["count"] = count})
end
table.sort( sortedMastersCount, function (a, b) return a.count > b.count end )
if sortedMastersCount[1].count == sortedMastersCount[2].count then
print("Tiebreaker!")
local maxCount = sortedMastersCount[1].count
winner = sortedMastersCount[1].owner
for i=2, #sortedMastersCount, 1 do
if sortedMastersCount[i].count == maxCount then
local player = gameState.playersList[sortedMastersCount[i].owner]
if player.firstMasterPromotionPlace < gameState.playersList[winner].firstMasterPromotionPlace then
winner = player.owner
end
else
break
end
end
print("The winner is (via promotion place): " .. winner)
else
winner = sortedMastersCount[1].owner
print("The winner is (by num of masters " .. sortedMastersCount[1].count .. "): " .. winner)
end
end
return winner
end
return RuleBook