-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.lua
More file actions
231 lines (207 loc) · 7.17 KB
/
game.lua
File metadata and controls
231 lines (207 loc) · 7.17 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Game = Object:extend()
function Game:new()
self.turn = "w"
self.status = "main"
end
function Game:getStatus()
return self.status
end
function Game:startGame()
self.status = "playing"
end
function Game:drawGame()
self.status = "draw"
end
function Game:getTurn()
return self.turn
end
function Game:setTurn(color)
self.turn = color
end
function Game:validateMove(board)
local pieces = board:getPieces()
local pressed = board:getMoved()
local lastMove = board:getLastMove()
if pieces[pressed]:getColor() == self.turn then
local king = board:getKing(self.turn)
--Validates move
if self:checkMovement(pieces, pieces[pressed]:getColor(), lastMove, pressed) then
--Validates king in check
if not self:isKingInCheck(king, pieces) then
local capturedPiece = self:checkPieceToBeCaptured(pieces, lastMove, pressed)
--Checks if castling
if pieces[pressed]:checkPossibleCasteling(pieces, board) == false then
return false
end
--Updates position before validating king in check again
pieces[pressed]:updatePos(lastMove)
--Saves captured piece in table to validate if its the one causing check
--If it is, then it can be captured and the move is valid
local capturedPieces = {}
table.insert(capturedPieces, capturedPiece)
if capturedPiece and self:isKingInCheck(king, capturedPieces) then
board:removeCapturedPiece(capturedPiece)
self:resetCapturedFlags(pieces)
return true
end
--If moved piece leaves king checkmated, go back
if self:isKingInCheck(king, pieces) then
self:resetCapturedFlags(pieces)
return false
else
if capturedPiece then
board:removeCapturedPiece(capturedPiece)
self:resetCapturedFlags(pieces)
end
return true
end
else
local capturedPiece = self:checkPieceToBeCaptured(pieces, lastMove, pressed)
pieces[pressed]:updatePos(lastMove)
--Checks if king is still in check after moving piece
if self:isKingInCheck(king, pieces) then
local capturedPieces = {}
table.insert(capturedPieces, capturedPiece)
--If the piece that has king in check can be captured
if capturedPiece and self:isKingInCheck(king, capturedPieces) then
board:removeCapturedPiece(capturedPiece)
self:resetCapturedFlags(pieces)
return true
end
return false
elseif capturedPiece then
board:removeCapturedPiece(capturedPiece)
self:resetCapturedFlags(pieces)
end
return true
end
end
end
return false
end
function Game:nextTurn()
if self:getTurn() == "w" then
self:setTurn("b")
else
self:setTurn("w")
end
end
function Game:finished()
end
function Game:checkmated()
end
function Game:checkMovement(pieces, color, movement, pressed)
local xf,yf = movement:getEnd();
local xo,yo = movement:getStart();
if (pieces[pressed]:validateMovement(movement)) then
--Validates end position is free or occupied by opponent piece
--Validates if it can jump pieces or not
for key, piece in pairs(pieces) do
if (piece) then
local x,y = piece:getActualPos()
if pieces[pressed]:checkTrajectory(x, y, xf, yf, xo, yo) and
pressed ~= key then
return false
end
if piece:checkPos(color, xf, yf) and
pressed ~= key then
return false
end
if piece:checkCoordinates(xf, yf) and
not pieces[pressed]:canCapture(movement, piece) and
pressed ~= key then
return false
end
end
end
else
--If move is not valid, check if piece can capture another by special move (ex. pawn)
for key, piece in pairs(pieces) do
if piece and
pieces[pressed]:canCapture(movement, piece) and
pressed ~= key then
return true
elseif not piece then
return false
end
end
return false
end
return true
end
function Game:isKingInCheck(king, pieces)
local opponent = "w"
if self.turn == "w" then
opponent = "b"
else
opponent = "w"
end
for key, piece in pairs(pieces) do
if piece:getColor() == opponent and self:canPieceCapture(key, king, pieces) then
return true
end
end
return false
end
function Game:checkPieceToBeCaptured(pieces, movement, pressed)
local xf,yf = movement:getEnd();
for key, piece in pairs(pieces) do
if piece and
pressed ~= key and
piece:getName() ~= "king" and
piece:checkCoordinates(xf, yf) then
piece:toBeCaptured(true)
return piece
elseif piece and -- For en passant move
pressed ~= key and
piece:getName() == "pawn" and
pieces[pressed]:getName() == "pawn" and
piece:getEnPassant() then
piece:toBeCaptured(true)
return piece
end
end
return false
end
function Game:canPieceCapture(pressed, otherPiece, pieces)
local movement = Move()
local xo,yo = pieces[pressed]:getActualPos()
local xf,yf = otherPiece:getActualPos()
movement:startMove(xo, yo, pieces[pressed]:getName())
movement:endMove(xf, yf)
if self:checkMovement(pieces, pieces[pressed]:getColor(), movement, pressed) then
return true
elseif pieces[pressed] == "pawn" and pieces[pressed]:canCapture(movement, otherPiece) then
return true
end
return false
end
function Game:resetCapturedFlags(pieces)
for key, piece in pairs(pieces) do
if piece then
piece:toBeCaptured(false)
piece:setEnPassant(false)
piece:setCanBeEnPassant(false)
end
end
end
function Game:showCurrentTurn(current, x, y)
if current == "w" then
love.graphics.print("Turn: White", x, y)
else
love.graphics.print("Turn: Black", x, y)
end
end
function Game:isPawnPromotion(pieces, color)
for key, piece in pairs(pieces) do
local x,y = piece:getActualPos()
if piece:getColor() == "w" and piece:getColor() == color
and y == 7 and piece:getName() == "pawn" then
return piece
elseif piece:getColor() == "b" and piece:getColor() == color
and y == 0 and piece:getName() == "pawn" then
return piece
end
end
return false
end