-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPawn.lua
executable file
·162 lines (120 loc) · 3.69 KB
/
Pawn.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
require 'Piece'
local array = require 'lib.array'
local inspect = require 'lib.inspect'
Pawn = Piece:new()
Pawn.hasMoved = false
Pawn.type = 'pawn'
Pawn.symbol = 'p'
Pawn.amPasanOffence = false -- allows other pawns to amPasan attack
Pawn.amPassanDefence = false
Pawn.forwardMove = nil
Pawn.points = 1
Pawn.justDidDoubleMove = false
Pawn.moveMeta = {}
function Pawn:getPossibleMoves()
self.possibleMoves = {}
self.forwardMove = self:moveForward()
if chessBoard:isSquareEmpty(self.forwardMove) then
table.insert(self.possibleMoves, self.forwardMove)
self.moveMeta[self.forwardMove] = 'single'
if self.hasMoved == false then
local doubleMove = self:moveForward(self.forwardMove)
if chessBoard:isSquareEmpty(doubleMove) then
table.insert(self.possibleMoves, doubleMove)
self.moveMeta[doubleMove] = 'double'
end
end
end
return array.concat(self.possibleMoves, self:getAttacks())
end
function Pawn:getAttacks()
local attackSquares = {}
local leftAttack = self:moveLeft(self.forwardMove)
local rightAttack = self:moveRight(self.forwardMove)
table.insert(attackSquares, rightAttack)
table.insert(attackSquares, leftAttack)
attackSquares = array.filter(attackSquares, function(square)
local pieceInSquareIndex = chessBoard:pieceInSquare(square)
return (chessBoard:isSquareEmpty(square) == false and chessBoard.pieces[pieceInSquareIndex].color ~= self.color)
end
)
for i,s in ipairs(attackSquares) do
self.moveMeta[s] = 'attack'
end
if self:inFifthRow() then
local leftSquare = self:moveLeft()
local rightSquare = self:moveRight()
print('ok')
if self:canAmPassan(leftSquare) then
table.insert(attackSquares, leftAttack)
self.moveMeta[leftAttack] = 'am'
elseif self:canAmPassan(rightSquare) then
table.insert(attackSquares, rightAttack)
self.moveMeta[rightAttack] = 'am'
end
end
return attackSquares
end
function Pawn:canAmPassan(square)
local pieceIndex = chessBoard:pieceInSquare(square)
if pieceIndex ~= nil and chessBoard.pieces[pieceIndex].color ~= self.color and chessBoard.pieces[pieceIndex].justDidDoubleMove == true then
return true
end
return false
end
function Pawn:inFifthRow()
if self.color == 'white' then
if tonumber(self:getRow()) == 5 then
return true
end
elseif self.color == 'black' then
if tonumber(self:getRow()) == 4 then
return true
end
else
return false
end
end
function Pawn:getControlledSquares()
local controlledSquares = {}
local leftAttack = self:moveLeft(forwardMove)
local rightAttack = self:moveRight(forwardMove)
table.insert(controlledSquares, rightAttack)
table.insert(controlledSquares, leftAttack)
return controlledSquares
end
function Pawn:moveTo(square)
local possibleMoves = self:legalMoves()
if array.index_of(possibleMoves, square) ~= -1 and self.color == chessBoard.hasTurn then
if self.moveMeta[square] == 'attack' then
chessBoard:killPieceInSquare(square)
elseif self.moveMeta[square] == 'am' then
local attackedRow = self.color == 'white' and 5 or 4
local attackedSquare = self:getColumn(square) .. attackedRow
chessBoard:killPieceInSquare(attackedSquare)
elseif self.moveMeta[square] == 'double' then
self.justDidDoubleMove = true
end
self.square = square
if self:inRome() then
print("Rome is conquered!")
love.graphics.setBlendMode("alpha", "premultiplied")
love.graphics.draw(canvas)
love.graphics.draw(canvas, 100, 0)
end
self.hasMoved = true
chessBoard:afterMove()
end
self.grabFactor = 0
self.grabed = false
end
function Pawn:inRome()
if self:getRow() == '8' and self.color == 'white' then
return true
elseif self:getRow() == '1' and self.color == 'black' then
return true
else
return false
end
end
return Pawn