-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pieces objects for generating moves from board states.
- Loading branch information
Showing
10 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model.board | ||
|
||
final case class Move( | ||
val from: Coords, | ||
val to: Coords, | ||
val nextBoardState: BoardState = BoardState.empty | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.Color | ||
import model.board.ChessBoard | ||
|
||
object Bishop extends Piece { | ||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
val maxPositiveSteps = Math.min(7 - coords.rank, 7 - coords.file) | ||
val minPositiveSteps = -Math.min(coords.rank, coords.file) | ||
|
||
val maxNegativeSteps = Math.min(7 - coords.rank, coords.file) | ||
val minNegativeSteps = -Math.min(coords.rank, 7 - coords.file) | ||
|
||
val positiveCoords = | ||
(minPositiveSteps | ||
.to(maxPositiveSteps)) | ||
.map(step => coords.movePositiveDiagonal(step.toByte)) | ||
.flatten | ||
|
||
val negativeCoords = | ||
(minNegativeSteps | ||
.to(maxNegativeSteps)) | ||
.map(step => coords.moveNegativeDiagonal(step.toByte)) | ||
.flatten | ||
|
||
val possibleCoords = positiveCoords ++ negativeCoords | ||
|
||
BitBoard(possibleCoords*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
Seq(board) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package model.board.pieces | ||
|
||
import model.board.ChessBoard | ||
import model.board.Coords | ||
import model.board.BitBoard | ||
import model.Color | ||
|
||
object Empty extends Piece { | ||
|
||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = | ||
BitBoard.empty | ||
|
||
override def legalMoves(board: ChessBoard, coords: Coords): Seq[ChessBoard] = | ||
Seq() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.Color | ||
import model.board.ChessBoard | ||
|
||
object King extends Piece { | ||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
val moves = Seq( | ||
coords.moveHorizontal(1.toByte), | ||
coords.moveHorizontal(-1.toByte), | ||
coords.moveVertical(1.toByte), | ||
coords.moveVertical(-1.toByte), | ||
coords.movePositiveDiagonal(1.toByte), | ||
coords.movePositiveDiagonal(-1.toByte), | ||
coords.moveNegativeDiagonal(1.toByte), | ||
coords.moveNegativeDiagonal(-1.toByte) | ||
) | ||
BitBoard(moves.flatten*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
Seq(board) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.Color | ||
import model.board.ChessBoard | ||
|
||
object Knight extends Piece { | ||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
val moves = | ||
(0.to(7)).map(i => coords.moveKnight(i.toByte)) | ||
|
||
BitBoard(moves.flatten*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
Seq(board) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.board.ChessBoard | ||
import model.Color | ||
|
||
object Pawn extends Piece { | ||
def isOnSecondRank(coords: Coords, color: Color): Boolean = color match { | ||
case Color.White => coords.rank == 1 | ||
case Color.Black => coords.rank == 6 | ||
} | ||
|
||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
val direction = if color.isWhite then 1 else -1 | ||
val jumps = | ||
if isOnSecondRank(coords, color) then | ||
Seq( | ||
coords.moveVertical(direction.toByte), | ||
coords.moveVertical((direction.toByte * 2).toByte) | ||
) | ||
else Seq(coords.moveVertical(direction.toByte)) | ||
|
||
BitBoard(jumps.flatten*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
return Seq(board) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.Move | ||
import model.board.BitBoard | ||
import model.PieceType | ||
import model.Color | ||
import model.board.ChessBoard | ||
|
||
trait Piece { | ||
// Moves the piece could theoretically make from the position if there were no pieces, checks, pins, etc. | ||
def theoreticalMoves(coords: Coords, color: Color): BitBoard | ||
|
||
// Moves the piece can make from the position considering all limitations | ||
def legalMoves(board: ChessBoard, coords: Coords): Seq[ChessBoard] | ||
|
||
def getPiece(pieceType: PieceType): Piece = pieceType match { | ||
case PieceType.Pawn => Pawn | ||
case PieceType.Knight => Knight | ||
case PieceType.Bishop => Bishop | ||
case PieceType.Rook => Rook | ||
case PieceType.Queen => Queen | ||
case PieceType.King => King | ||
case PieceType.Empty => Empty | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.board.ChessBoard | ||
import model.Color | ||
|
||
object Queen extends Piece { | ||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
// Bishop-like moves | ||
val maxPositiveSteps = Math.min(7 - coords.rank, 7 - coords.file) | ||
val minPositiveSteps = -Math.min(coords.rank, coords.file) | ||
|
||
val maxNegativeSteps = Math.min(7 - coords.rank, coords.file) | ||
val minNegativeSteps = -Math.min(coords.rank, 7 - coords.file) | ||
|
||
val positiveCoords = | ||
(minPositiveSteps | ||
.to(maxPositiveSteps)) | ||
.map(step => coords.movePositiveDiagonal(step.toByte)) | ||
.flatten | ||
|
||
val negativeCoords = | ||
(minNegativeSteps | ||
.to(maxNegativeSteps)) | ||
.map(step => coords.moveNegativeDiagonal(step.toByte)) | ||
.flatten | ||
|
||
// Rook-like moves | ||
val maxHorizontalSteps = 7 - coords.file | ||
val minHorizontalSteps = -coords.file | ||
|
||
val maxVerticalSteps = 7 - coords.rank | ||
val minVerticalSteps = coords.file | ||
|
||
val horizontalCoords = | ||
(minHorizontalSteps | ||
.to(maxHorizontalSteps)) | ||
.map(step => coords.moveHorizontal(step.toByte)) | ||
.flatten | ||
|
||
val verticalCoords = | ||
(minVerticalSteps.toInt | ||
.to(maxVerticalSteps)) | ||
.map(step => coords.moveVertical(step.toByte)) | ||
.flatten | ||
|
||
val possibleCoords = | ||
horizontalCoords ++ verticalCoords ++ positiveCoords ++ negativeCoords | ||
|
||
BitBoard(possibleCoords*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
return Seq(board) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package model.board.pieces | ||
|
||
import model.board.Move | ||
import model.board.Coords | ||
import model.board.Board | ||
import model.board.BitBoard | ||
import model.Color | ||
import model.board.ChessBoard | ||
|
||
object Rook extends Piece { | ||
override def theoreticalMoves(coords: Coords, color: Color): BitBoard = { | ||
val maxHorizontalSteps = 7 - coords.file | ||
val minHorizontalSteps = -coords.file | ||
|
||
val maxVerticalSteps = 7 - coords.rank | ||
val minVerticalSteps = coords.file | ||
|
||
val horizontalCoords = | ||
(minHorizontalSteps | ||
.to(maxHorizontalSteps)) | ||
.map(step => coords.moveHorizontal(step.toByte)) | ||
.flatten | ||
|
||
val verticalCoords = | ||
(minVerticalSteps.toInt | ||
.to(maxVerticalSteps)) | ||
.map(step => coords.moveVertical(step.toByte)) | ||
.flatten | ||
|
||
val possibleCoords = horizontalCoords ++ verticalCoords | ||
|
||
BitBoard(possibleCoords*) | ||
} | ||
|
||
override def legalMoves( | ||
board: ChessBoard, | ||
coords: Coords | ||
): Seq[ChessBoard] = { | ||
return Seq(board) | ||
} | ||
} |
Oops, something went wrong.