-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.lua
executable file
·163 lines (124 loc) · 4.2 KB
/
King.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
require 'Piece'
local array = require 'lib.array'
local inspect = require 'lib.inspect'
King = Piece:new()
King.hasMoved = false
King.type = 'king'
King.symbol = 'k'
King.isChecked = false
King.points = 0
function King:getPossibleMoves()
self.possibleMoves = {}
print("King Moves 1:"..inspect(self.possibleMoves))
local forward = self:moveForward()
table.insert(self.possibleMoves, forward)
table.insert(self.possibleMoves, self:moveLeft())
table.insert(self.possibleMoves, self:moveRight())
local backwards = self:moveBackwards()
table.insert(self.possibleMoves, backwards)
table.insert(self.possibleMoves, self:moveRight(forward))
table.insert(self.possibleMoves, self:moveRight(backwards))
table.insert(self.possibleMoves, self:moveLeft(forward))
table.insert(self.possibleMoves, self:moveLeft(backwards))
print("King Moves 2:"..inspect(self.possibleMoves))
if self.color == chessBoard.hasTurn then
self.possibleMoves = array.filter(self.possibleMoves, function(square)
return array.index_of(bot:findAttackedSquares(), square) == -1
end
)
end
print("King Moves 3:"..inspect(self.possibleMoves))
for i=#(self.possibleMoves),1,-1 do
local square = self.possibleMoves[i]
local pieceIndex = chessBoard:pieceInSquare(square)
if pieceIndex ~= nil then
local otherPiece = chessBoard.pieces[pieceIndex]
if otherPiece.color == self.color then
table.remove(self.possibleMoves, i)
else
self.possibleMoves[i] = self.possibleMoves[i]
end
end
end
print("King Moves 4:"..inspect(self.possibleMoves))
if self.hasMoved == false and string.len(chessBoard:getCastlingRights(self.color)) > 0 then
self.possibleMoves = array.concat(self:castleCheck(), self.possibleMoves)
end
print("King Moves 5:"..inspect(self.possibleMoves))
self.possibleMoves = array.uniq(self.possibleMoves)
print("King Moves 6:"..inspect(self.possibleMoves))
return self.possibleMoves
end
function King:castleCheck()
local kingRook
local queenRook
local castels = {}
if self.color == 'white' then
kingRook = chessBoard.pieces[chessBoard:pieceInSquare('h1')]
queenRook = chessBoard.pieces[chessBoard:pieceInSquare('a1')]
if kingRook ~= nil and kingRook.hasMoved == false and chessBoard:areSquaresEmpty({'f1', 'g1'}) then
table.insert(castels, 'g1')
end
if queenRook ~= nil and queenRook.hasMoved == false and chessBoard:areSquaresEmpty({'b1', 'c1', 'd1'}) then
table.insert(castels, 'c1')
end
else
kingRook = chessBoard.pieces[chessBoard:pieceInSquare('h8')]
queenRook = chessBoard.pieces[chessBoard:pieceInSquare('a8')]
if kingRook ~= nil and kingRook.hasMoved == false and chessBoard:areSquaresEmpty({'f8', 'g8'}) then
table.insert(castels, 'g8')
end
if queenRook ~= nil and queenRook.hasMoved == false and chessBoard:areSquaresEmpty({'b8', 'c8', 'd8'}) then
table.insert(castels, 'c8')
end
end
return castels
end
function King:initialSquare()
local initialSquare
if self.color == 'white' then
initialSquare = 'e1'
else
initialSquare = 'e8'
end
return initialSquare
end
function King.isKingChecked()
if array.index_of(bot.attackedSquares, self.square) == -1 then
self.isChecked = false
else
self.isChecked = true
end
end
function King:moveTo(square)
local possibleMoves = self:legalMoves()
if array.index_of(possibleMoves, square) ~= -1 and self.color == chessBoard.hasTurn then
if square == 'g'.. (self:getRow()) and self.hasMoved == false then
print('castling kingside')
self:kingSideCastle(square)
elseif square == 'c'.. (self:getRow()) and self.hasMoved == false then
print('castling queenside')
self:queenSideCastle(square)
else
chessBoard:killPieceInSquare(square)
self.square = square
end
self.hasMoved = true
chessBoard:afterMove()
end
self.grabFactor = 0
self.grabed = false
end
function King:kingSideCastle(square)
self.square = square
local rookSquare = 'h'.. self:getRow()
local kingRook = chessBoard.pieces[chessBoard:pieceInSquare(rookSquare)]
kingRook.square = 'f' .. self:getRow()
end
function King:queenSideCastle(square)
self.square = square
local rookSquare = 'a'.. self:getRow()
local queenRook = chessBoard.pieces[chessBoard:pieceInSquare(rookSquare)]
queenRook.square = 'd' .. self:getRow()
end
return King