-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.lua
229 lines (225 loc) · 5.88 KB
/
piece.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
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
Piece = {
r,
c,
type,
team,
spr,
cost = 2,
firstmove = true,
moved = false,
}
function Piece:Create(piece)
local piece = piece or {}
setmetatable(piece, self)
self.__index = self
board[piece.r][piece.c] = piece
return piece
end
function Piece:Draw()
lg.setColor(StringToColor(self.team))
lg.setFont(chessfont)
if selection and self.r == selection.r and self.c == selection.c then
lg.print(self.spr, lm.getX() - tilesize * 0.5, lm.getY() - tilesize * 0.5)
else
lg.print(self.spr, (self.c + 3) * tilesize, (self.r + 3) * tilesize)
end
end
function Piece:Move(r, c)
if self.team ~= board[r][c].team then
for i, p in ipairs(pieces) do
if p == board[r][c] then
table.remove(pieces, i) -- capture the piece
end
end
end
self.firstmove = false
board[r][c] = self
board[self.r][self.c] = nil
board[self.r][self.c] = {}
self.r, self.c = r, c
end
function Piece:Check(r, c)
if game.turn ~= self.team then
return false
end
if self.moved then
return false
end
if board[r][c].team == board[self.r][self.c].team then
return false
end
if self.type == "pawn" then
if self.team == "white" then
if self.r - r == 1 and self.c == c and board[r][c].team ~= "black" then
return true
end
if self.r - r == 1 and math.abs(self.c - c) == 1 and board[r][c].team == "black" then -- capturing
return true
end
if self.firstmove and self.c == c and self.r - r == 2 then
for i = 1, self.r - r do
if board[self.r - i][c].team then
return false
end
end
return true
end
else
if r - self.r == 1 and self.c == c and board[r][c].team ~= "white" then
return true
end
if r - self.r == 1 and math.abs(self.c - c) == 1 and board[r][c].team == "white" then -- capturing
return true
end
if self.firstmove and self.c == c and r - self.r == 2 then
for i = 1, r - self.r do
if board[self.r + i][c].team then
return false
end
end
return true
end
end
elseif self.type == "knight" then
if (math.abs(self.r - r) == 1 and math.abs(self.c - c) == 2) or (math.abs(self.r - r) == 2 and math.abs(self.c - c) == 1) then
return true
end
elseif self.type == "bishop" then
if math.abs(self.r - r) == math.abs(self.c - c) then
for i = 1, math.max(self.r - r - 1, r - self.r - 1) do
-- northeast
if c > self.c and r < self.r then
if board[self.r - i][self.c + i].team then
return false
end
end
-- northwest
if c < self.c and r < self.r then
if board[self.r - i][self.c - i].team then
return false
end
end
-- southeast
if c > self.c and r > self.r then
if board[self.r + i][self.c + i].team then
return false
end
end
-- southwest
if c < self.c and r > self.r then
if board[self.r + i][self.c - i].team then
return false
end
end
end
--check final tile
return true
end
elseif self.type == "rook" then
if self.c == c or self.r == r then
-- north
if self.c == c and self.r - r > 0 then
for i = 1, self.r - r - 1 do
if board[self.r - i][c].team then
return false
end
end
end
-- south
if self.c == c and r - self.r > 0 then
for i = 1, r - self.r - 1 do
if board[self.r + i][c].team then
return false
end
end
end
-- east
if self.r == r and c - self.c > 0 then
for i = 1, c - self.c - 1 do
if board[r][self.c + i].team then
return false
end
end
end
-- west
if self.r == r and self.c - c > 0 then
for i = 1, self.c - c - 1 do
if board[r][self.c - i].team then
return false
end
end
end
return true
end
elseif self.type == "queen" then
if math.abs(self.r - r) == math.abs(self.c - c) then
for i = 1, math.max(self.r - r - 1, r - self.r - 1) do
-- northeast
if c > self.c and r < self.r then
if board[self.r - i][self.c + i].team then
return false
end
end
-- northwest
if c < self.c and r < self.r then
if board[self.r - i][self.c - i].team then
return false
end
end
-- southeast
if c > self.c and r > self.r then
if board[self.r + i][self.c + i].team then
return false
end
end
-- southwest
if c < self.c and r > self.r then
if board[self.r + i][self.c - i].team then
return false
end
end
end
return true -- final tile
end
if self.c == c or self.r == r then
-- north
if self.c == c and self.r - r > 0 then
for i = 1, self.r - r - 1 do
if board[self.r - i][c].team then
return false
end
end
end
-- south
if self.c == c and r - self.r > 0 then
for i = 1, r - self.r - 1 do
if board[self.r + i][c].team then
return false
end
end
end
-- east
if self.r == r and c - self.c > 0 then
for i = 1, c - self.c - 1 do
if board[r][self.c + i].team then
return false
end
end
end
-- west
if self.r == r and self.c - c > 0 then
for i = 1, self.c - c - 1 do
if board[r][self.c - i].team then
return false
end
end
end
return true
end
elseif self.type == "king" then
if math.abs(self.r - r) <= 1 and math.abs(self.c - c) <= 1 then
return true
end
end
return false
end