-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight.lua
executable file
·75 lines (50 loc) · 1.65 KB
/
Knight.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
require 'Piece'
local array = require 'lib.array'
local inspect = require 'lib.inspect'
Knight = Piece:new()
Knight.hasMoved = false
Knight.type = 'knight'
Knight.symbol = 'n'
Knight.points = 3
function Knight:getPossibleMoves()
self.possibleMoves = {}
local oneLeft = self:moveLeft()
local twoLeft = self:moveLeft(oneLeft)
if twoLeft ~= nil and oneLeft ~= nil then
self.possibleMoves = array.concat(self.possibleMoves, self:_upDown(twoLeft))
end
local oneRight = self:moveRight()
local twoRight = self:moveRight(oneRight)
if twoRight ~= nil and oneRight ~= nil then
self.possibleMoves = array.concat(self.possibleMoves, self:_upDown(twoRight))
end
local oneForward = self:moveForward()
local twoForward = self:moveForward(oneForward)
if twoForward ~= nil and oneForward ~= nil then
self.possibleMoves = array.concat(self.possibleMoves, self:_leftRight(twoForward))
end
local oneBack = self:moveBackwards()
local twoBack = self:moveBackwards(oneBack)
if twoBack ~= nil and oneBack ~= nil then
self.possibleMoves = array.concat(self.possibleMoves, self:_leftRight(twoBack))
end
self.possibleMoves = array.filter(self.possibleMoves, function(square)
return self.color ~= chessBoard:colorOfPieceInSquare(square)
end
)
print("horse moves:"..inspect(self.possibleMoves))
return self.possibleMoves
end
function Knight:_upDown(square)
local moves = {}
table.insert(moves, self:moveForward(square))
table.insert(moves, self:moveBackwards(square))
return moves
end
function Knight:_leftRight(square)
local moves = {}
table.insert(moves, self:moveLeft(square))
table.insert(moves, self:moveRight(square))
return moves
end
return Knight