Pawn moves
This commit is contained in:
@@ -75,7 +75,7 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func notify(_ event: Event) {
|
func notify(_ event: Event) {
|
||||||
|
#warning("Not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func addPieceToTarget(_ piece: Piece, target: Square.Position) {
|
internal func addPieceToTarget(_ piece: Piece, target: Square.Position) {
|
||||||
|
|||||||
@@ -26,7 +26,13 @@ final class Bishop: Piece, DiagonalMoves {
|
|||||||
let piece = square.piece
|
let piece = square.piece
|
||||||
{
|
{
|
||||||
if piece.color != color {
|
if piece.color != color {
|
||||||
delegate?.notify(.piecePinned(from: self, on: piece))
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
}
|
||||||
last = position
|
last = position
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,7 +41,12 @@ final class Bishop: Piece, DiagonalMoves {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
final class Pawn: Piece {
|
final class Pawn: Piece {
|
||||||
|
private var sign: Int8 {
|
||||||
|
color == .Black ? -1 : 1
|
||||||
|
}
|
||||||
override var pseudoLegalPositions: [Square.Position] {
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
let sign: Int8 = color == .Black ? -1 : 1
|
|
||||||
return [
|
return [
|
||||||
position + (1 * sign, 0),
|
position + (1 * sign, 0),
|
||||||
position + (2 * sign, 0),
|
position + (2 * sign, 0),
|
||||||
@@ -14,7 +16,25 @@ final class Pawn: Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
#warning("Handle en-passant")
|
||||||
|
if pos == position + (2 * sign, 0) && halfMoveCount > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (pos == position + (1 * sign, 0) || pos == position + (2 * sign, 0))
|
||||||
|
&& delegate?.getSquareInfo(on: pos)?.piece != nil
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
guard super.isLegal(on: pos) else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (pos == position + (1 * sign, 1) || pos == position + (1 * sign, -1))
|
||||||
|
&& delegate?.getSquareInfo(on: pos)?.piece == nil
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -85,7 +85,6 @@ public class Piece: Hashable {
|
|||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if let king = p as? King {
|
if let king = p as? King {
|
||||||
#warning("It could be in check or behind another piece.")
|
|
||||||
delegate?.notify(.kingInCheck(self, on: king))
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,13 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|||||||
let piece = square.piece
|
let piece = square.piece
|
||||||
{
|
{
|
||||||
if piece.color != color {
|
if piece.color != color {
|
||||||
delegate?.notify(.piecePinned(from: self, on: piece))
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
}
|
||||||
last = position
|
last = position
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,7 +43,12 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -26,7 +26,13 @@ final class Rook: Piece, LinearMoves {
|
|||||||
let piece = square.piece
|
let piece = square.piece
|
||||||
{
|
{
|
||||||
if piece.color != color {
|
if piece.color != color {
|
||||||
delegate?.notify(.piecePinned(from: self, on: piece))
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
}
|
||||||
last = position
|
last = position
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,7 +41,12 @@ final class Rook: Piece, LinearMoves {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
Reference in New Issue
Block a user