Sliding pieces moves are now illegal if after piece of same color

This commit is contained in:
cdricms
2024-06-30 21:06:03 +02:00
parent 02de114d74
commit d3503f1441
8 changed files with 225 additions and 54 deletions

View File

@@ -8,6 +8,24 @@ final class Bishop: Piece, DiagonalMoves {
return getDiagonalMoves(from: position)
}
override func getLegalPosition() {
legalPositions = []
var last: Square.Position? = nil
for position in pseudoLegalPositions {
if !isLegal(on: position) {
last = position
continue
}
if !DiagonalDirection.isLegal(last: &last, current: position) {
last = nil
}
if last == nil {
legalPositions.append(position)
}
}
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -1,30 +1,118 @@
fileprivate func getDirectionalMoves(from pos: Square.Position, with dir: [(Int8, Int8)]) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in dir {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
private func getDirectionalMoves(
from pos: Square.Position, with dir: [(Int8, Int8)]
) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in dir {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
}
protocol DiagonalMoves {
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position]
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position]
}
protocol Direction: CaseIterable {
var values: (Int8, Int8) { get }
static func isLegal(
last: inout Square.Position?,
current: Square.Position
) -> Bool
}
public enum DiagonalDirection: Direction {
case northWest
case northEast
case southEast
case southWest
public var values: (Int8, Int8) {
return switch self {
case .northWest: (1, -1)
case .northEast: (1, 1)
case .southEast: (-1, 1)
case .southWest: (-1, -1)
}
}
public static func isLegal(
last: inout Square.Position?,
current:
Square.Position
) -> Bool {
var isPred = false
for dir in DiagonalDirection.allCases {
isPred = last == current - dir.values
if isPred {
last = current
break
}
}
return isPred
}
}
extension DiagonalMoves {
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(from: pos, with: [(1, -1), (1, 1), (-1, 1), (-1, -1)])
}
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(
from: pos,
with: [
DiagonalDirection.northWest.values,
DiagonalDirection.northEast.values,
DiagonalDirection.southEast.values,
DiagonalDirection.southWest.values,
])
}
}
protocol LinearMoves {
func getLinearMoves(from pos: Square.Position) -> [Square.Position]
func getLinearMoves(from pos: Square.Position) -> [Square.Position]
}
public enum LinearDirection: Direction {
case north
case south
case east
case west
public var values: (Int8, Int8) {
return switch self {
case .north: (1, 0)
case .south: (-1, 0)
case .east: (0, 1)
case .west: (0, -1)
}
}
public static func isLegal(
last: inout Square.Position?, current: Square.Position
) -> Bool {
var isPred = false
for dir in LinearDirection.allCases {
isPred = last == current - dir.values
if isPred {
last = current
break
}
}
return isPred
}
}
extension LinearMoves {
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(from: pos, with: [(1, 0), (0, 1), (-1, 0), (0, -1)])
}
}
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(
from: pos,
with: [
LinearDirection.north.values,
LinearDirection.east.values,
LinearDirection.south.values,
LinearDirection.west.values,
])
}
}

View File

@@ -65,7 +65,7 @@ public class Piece: Hashable {
internal var pseudoLegalPositions: [Square.Position] {
return []
}
internal var legalPositions = [Square.Position]()
package internal(set) var legalPositions = [Square.Position]()
internal func getLegalPosition() {
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
}

View File

@@ -7,6 +7,27 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
}
override func getLegalPosition() {
legalPositions = []
var last: Square.Position? = nil
for position in pseudoLegalPositions {
if !isLegal(on: position) {
last = position
continue
}
if !LinearDirection.isLegal(last: &last, current: position)
&& !DiagonalDirection.isLegal(last: &last, current: position)
{
last = nil
}
if last == nil {
legalPositions.append(position)
}
}
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -8,6 +8,24 @@ final class Rook: Piece, LinearMoves {
return getLinearMoves(from: position)
}
override func getLegalPosition() {
legalPositions = []
var last: Square.Position? = nil
for position in pseudoLegalPositions {
if !isLegal(on: position) {
last = position
continue
}
if !LinearDirection.isLegal(last: &last, current: position) {
last = nil
}
if last == nil {
legalPositions.append(position)
}
}
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}