chore: Set up shared-models library and initial project structure for NowChessSystems
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
plugins {
|
||||
id("scala")
|
||||
jacoco
|
||||
application
|
||||
}
|
||||
|
||||
group = "de.nowchess"
|
||||
@@ -16,6 +18,22 @@ scala {
|
||||
versions["SCALA3"]!!
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass.set("de.nowchess.chess.chessMain")
|
||||
}
|
||||
|
||||
tasks.named<JavaExec>("run") {
|
||||
jvmArgs("-Dfile.encoding=UTF-8", "-Dstdout.encoding=UTF-8", "-Dstderr.encoding=UTF-8")
|
||||
standardInput = System.`in`
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
finalizedBy(tasks.jacocoTestReport)
|
||||
}
|
||||
tasks.jacocoTestReport {
|
||||
dependsOn(tasks.test)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation("org.scala-lang:scala3-compiler_3") {
|
||||
@@ -34,6 +52,8 @@ dependencies {
|
||||
}
|
||||
}
|
||||
|
||||
implementation(project(":modules:api"))
|
||||
|
||||
testImplementation(platform("org.junit:junit-bom:5.10.0"))
|
||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package de.nowchess
|
||||
|
||||
object Test {
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("Hello World")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
import scala.io.StdIn
|
||||
|
||||
@main def chessMain(): Unit =
|
||||
println("NowChess TUI — type moves in coordinate notation (e.g. e2e4). Type 'quit' to exit.")
|
||||
gameLoop(Board.initial, Color.White)
|
||||
|
||||
private def gameLoop(board: Board, turn: Color): Unit =
|
||||
println()
|
||||
print(Renderer.render(board))
|
||||
println(s"${turn.label}'s turn. Enter move: ")
|
||||
val input = Option(StdIn.readLine()).getOrElse("quit").trim
|
||||
input match
|
||||
case "quit" | "q" =>
|
||||
println("Game over. Goodbye!")
|
||||
case raw =>
|
||||
Parser.parseMove(raw) match
|
||||
case None =>
|
||||
println(s"Invalid move format '$raw'. Use coordinate notation, e.g. e2e4.")
|
||||
gameLoop(board, turn)
|
||||
case Some((from, to)) =>
|
||||
board.pieceAt(from) match
|
||||
case None =>
|
||||
println(s"No piece on ${from.label}.")
|
||||
gameLoop(board, turn)
|
||||
case Some(movingPiece) =>
|
||||
val (newBoard, captured) = board.withMove(from, to)
|
||||
captured.foreach: cap =>
|
||||
println(s"${turn.label} captures ${cap.color.label} ${cap.pieceType.label} on ${to.label}")
|
||||
gameLoop(newBoard, turn.opposite)
|
||||
@@ -0,0 +1,82 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
enum Color:
|
||||
case White, Black
|
||||
|
||||
def opposite: Color = this match
|
||||
case White => Black
|
||||
case Black => White
|
||||
|
||||
def label: String = this match
|
||||
case White => "White"
|
||||
case Black => "Black"
|
||||
|
||||
enum PieceType:
|
||||
case King, Queen, Rook, Bishop, Knight, Pawn
|
||||
|
||||
def label: String = this match
|
||||
case King => "King"
|
||||
case Queen => "Queen"
|
||||
case Rook => "Rook"
|
||||
case Bishop => "Bishop"
|
||||
case Knight => "Knight"
|
||||
case Pawn => "Pawn"
|
||||
|
||||
final case class Piece(color: Color, pieceType: PieceType):
|
||||
def unicode: String = (color, pieceType) match
|
||||
case (Color.White, PieceType.King) => "\u2654"
|
||||
case (Color.White, PieceType.Queen) => "\u2655"
|
||||
case (Color.White, PieceType.Rook) => "\u2656"
|
||||
case (Color.White, PieceType.Bishop) => "\u2657"
|
||||
case (Color.White, PieceType.Knight) => "\u2658"
|
||||
case (Color.White, PieceType.Pawn) => "\u2659"
|
||||
case (Color.Black, PieceType.King) => "\u265A"
|
||||
case (Color.Black, PieceType.Queen) => "\u265B"
|
||||
case (Color.Black, PieceType.Rook) => "\u265C"
|
||||
case (Color.Black, PieceType.Bishop) => "\u265D"
|
||||
case (Color.Black, PieceType.Knight) => "\u265E"
|
||||
case (Color.Black, PieceType.Pawn) => "\u265F"
|
||||
|
||||
/** Zero-based file (0=a..7=h) and rank (0=rank1..7=rank8). */
|
||||
final case class Square(file: Int, rank: Int):
|
||||
require(file >= 0 && file <= 7 && rank >= 0 && rank <= 7, s"Square out of bounds: $file,$rank")
|
||||
|
||||
def label: String = s"${('a' + file).toChar}${rank + 1}"
|
||||
|
||||
opaque type Board = Map[Square, Piece]
|
||||
|
||||
object Board:
|
||||
def apply(pieces: Map[Square, Piece]): Board = pieces
|
||||
|
||||
extension (b: Board)
|
||||
def pieceAt(sq: Square): Option[Piece] = b.get(sq)
|
||||
def withMove(from: Square, to: Square): (Board, Option[Piece]) =
|
||||
val captured = b.get(to)
|
||||
val updated = b.removed(from).updated(to, b(from))
|
||||
(updated, captured)
|
||||
def pieces: Map[Square, Piece] = b
|
||||
|
||||
val initial: Board =
|
||||
val backRank: Vector[PieceType] =
|
||||
Vector(
|
||||
PieceType.Rook,
|
||||
PieceType.Knight,
|
||||
PieceType.Bishop,
|
||||
PieceType.Queen,
|
||||
PieceType.King,
|
||||
PieceType.Bishop,
|
||||
PieceType.Knight,
|
||||
PieceType.Rook
|
||||
)
|
||||
|
||||
val entries = for
|
||||
file <- 0 until 8
|
||||
(color, rank, row) <- Seq(
|
||||
(Color.White, 0, backRank(file)),
|
||||
(Color.White, 1, PieceType.Pawn),
|
||||
(Color.Black, 7, backRank(file)),
|
||||
(Color.Black, 6, PieceType.Pawn)
|
||||
)
|
||||
yield Square(file, rank) -> Piece(color, row)
|
||||
|
||||
Board(entries.toMap)
|
||||
@@ -0,0 +1,20 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
object Parser:
|
||||
|
||||
/** Parses coordinate notation such as "e2e4" or "g1f3".
|
||||
* Returns None for any input that does not match the expected format.
|
||||
*/
|
||||
def parseMove(input: String): Option[(Square, Square)] =
|
||||
val trimmed = input.trim.toLowerCase
|
||||
Option.when(trimmed.length == 4)(trimmed).flatMap: s =>
|
||||
for
|
||||
from <- parseSquare(s.substring(0, 2))
|
||||
to <- parseSquare(s.substring(2, 4))
|
||||
yield (from, to)
|
||||
|
||||
private def parseSquare(s: String): Option[Square] =
|
||||
Option.when(s.length == 2)(s).flatMap: sq =>
|
||||
val file = sq(0) - 'a'
|
||||
val rank = sq(1) - '1'
|
||||
Option.when(file >= 0 && file <= 7 && rank >= 0 && rank <= 7)(Square(file, rank))
|
||||
@@ -0,0 +1,29 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
object Renderer:
|
||||
|
||||
private val AnsiReset = "\u001b[0m"
|
||||
private val AnsiLightSquare = "\u001b[48;5;223m" // warm beige
|
||||
private val AnsiDarkSquare = "\u001b[48;5;130m" // brown
|
||||
private val AnsiWhitePiece = "\u001b[97m" // bright white text
|
||||
private val AnsiBlackPiece = "\u001b[30m" // black text
|
||||
|
||||
def render(board: Board): String =
|
||||
val sb = new StringBuilder
|
||||
sb.append(" a b c d e f g h\n")
|
||||
for rank <- (0 until 8).reverse do
|
||||
sb.append(s"${rank + 1} ")
|
||||
for file <- 0 until 8 do
|
||||
val sq = Square(file, rank)
|
||||
val isLightSq = (file + rank) % 2 != 0
|
||||
val bgColor = if isLightSq then AnsiLightSquare else AnsiDarkSquare
|
||||
val cellContent = board.pieceAt(sq) match
|
||||
case Some(piece) =>
|
||||
val fgColor = if piece.color == Color.White then AnsiWhitePiece else AnsiBlackPiece
|
||||
s"$bgColor$fgColor ${piece.unicode} $AnsiReset"
|
||||
case None =>
|
||||
s"$bgColor $AnsiReset"
|
||||
sb.append(cellContent)
|
||||
sb.append(s" ${rank + 1}\n")
|
||||
sb.append(" a b c d e f g h\n")
|
||||
sb.toString
|
||||
@@ -0,0 +1,59 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
class ModelTest:
|
||||
|
||||
@Test def colorOpposite(): Unit =
|
||||
assertEquals(Color.Black, Color.White.opposite)
|
||||
assertEquals(Color.White, Color.Black.opposite)
|
||||
|
||||
@Test def squareLabel(): Unit =
|
||||
assertEquals("a1", Square(0, 0).label)
|
||||
assertEquals("e4", Square(4, 3).label)
|
||||
assertEquals("h8", Square(7, 7).label)
|
||||
|
||||
@Test def pieceUnicode(): Unit =
|
||||
assertEquals("\u2654", Piece(Color.White, PieceType.King).unicode)
|
||||
assertEquals("\u265A", Piece(Color.Black, PieceType.King).unicode)
|
||||
assertEquals("\u2659", Piece(Color.White, PieceType.Pawn).unicode)
|
||||
assertEquals("\u265F", Piece(Color.Black, PieceType.Pawn).unicode)
|
||||
|
||||
@Test def initialBoardHas32Pieces(): Unit =
|
||||
assertEquals(32, Board.initial.pieces.size)
|
||||
|
||||
@Test def initialWhiteKingOnE1(): Unit =
|
||||
val e1 = Square(4, 0)
|
||||
assertEquals(Some(Piece(Color.White, PieceType.King)), Board.initial.pieceAt(e1))
|
||||
|
||||
@Test def initialBlackQueenOnD8(): Unit =
|
||||
val d8 = Square(3, 7)
|
||||
assertEquals(Some(Piece(Color.Black, PieceType.Queen)), Board.initial.pieceAt(d8))
|
||||
|
||||
@Test def initialWhitePawnsOnRank2(): Unit =
|
||||
for file <- 0 until 8 do
|
||||
val sq = Square(file, 1)
|
||||
assertEquals(Some(Piece(Color.White, PieceType.Pawn)), Board.initial.pieceAt(sq))
|
||||
|
||||
@Test def withMoveMovesAndLeavesOriginEmpty(): Unit =
|
||||
val e2 = Square(4, 1)
|
||||
val e4 = Square(4, 3)
|
||||
val (newBoard, captured) = Board.initial.withMove(e2, e4)
|
||||
assertEquals(None, newBoard.pieceAt(e2))
|
||||
assertEquals(Some(Piece(Color.White, PieceType.Pawn)), newBoard.pieceAt(e4))
|
||||
assertEquals(None, captured)
|
||||
|
||||
@Test def withMoveCaptureReturnsCapture(): Unit =
|
||||
// Place a black pawn on e4 and a white pawn already there via two moves
|
||||
val e2 = Square(4, 1)
|
||||
val e4 = Square(4, 3)
|
||||
val (board2, _) = Board.initial.withMove(e2, e4)
|
||||
// Place black pawn on d4 manually for capture test
|
||||
val d7 = Square(3, 6)
|
||||
val d4 = Square(3, 3)
|
||||
val (board3, _) = board2.withMove(d7, d4)
|
||||
// Now white pawn on e4 captures black pawn on d4 (diagonal — no legality check)
|
||||
val (board4, cap) = board3.withMove(e4, d4)
|
||||
assertEquals(Some(Piece(Color.Black, PieceType.Pawn)), cap)
|
||||
assertEquals(Some(Piece(Color.White, PieceType.Pawn)), board4.pieceAt(d4))
|
||||
@@ -0,0 +1,31 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
class ParserTest:
|
||||
|
||||
@Test def parsesValidMove(): Unit =
|
||||
assertEquals(Some((Square(4, 1), Square(4, 3))), Parser.parseMove("e2e4"))
|
||||
|
||||
@Test def parsesKnightMove(): Unit =
|
||||
assertEquals(Some((Square(6, 0), Square(5, 2))), Parser.parseMove("g1f3"))
|
||||
|
||||
@Test def ignoresExtraWhitespace(): Unit =
|
||||
assertEquals(Some((Square(4, 1), Square(4, 3))), Parser.parseMove(" e2e4 "))
|
||||
|
||||
@Test def rejectsShortInput(): Unit =
|
||||
assertEquals(None, Parser.parseMove("e2e"))
|
||||
|
||||
@Test def rejectsEmptyInput(): Unit =
|
||||
assertEquals(None, Parser.parseMove(""))
|
||||
|
||||
@Test def rejectsOutOfBoundsFile(): Unit =
|
||||
assertEquals(None, Parser.parseMove("z2a4"))
|
||||
|
||||
@Test def rejectsOutOfBoundsRank(): Unit =
|
||||
assertEquals(None, Parser.parseMove("e9e4"))
|
||||
|
||||
@Test def parsesUppercaseAsInvalid(): Unit =
|
||||
// uppercase files are out of range after toLowerCase — stays lowercase internally
|
||||
assertEquals(Some((Square(4, 1), Square(4, 3))), Parser.parseMove("E2E4"))
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.nowchess.chess
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
|
||||
class RendererTest:
|
||||
|
||||
@Test def renderContainsFileLabels(): Unit =
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.contains("a"), "render output should contain file label 'a'")
|
||||
assertTrue(output.contains("h"), "render output should contain file label 'h'")
|
||||
|
||||
@Test def renderContainsRankLabels(): Unit =
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.contains("1"), "render output should contain rank label '1'")
|
||||
assertTrue(output.contains("8"), "render output should contain rank label '8'")
|
||||
|
||||
@Test def renderContainsWhiteKingUnicode(): Unit =
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.contains("\u2654"), "render output should contain white king \u2654")
|
||||
|
||||
@Test def renderContainsBlackQueenUnicode(): Unit =
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.contains("\u265B"), "render output should contain black queen \u265B")
|
||||
|
||||
@Test def renderContainsAnsiReset(): Unit =
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.contains("\u001b[0m"), "render output should contain ANSI reset code")
|
||||
|
||||
@Test def renderReturnsStringNotUnit(): Unit =
|
||||
// Compilation-time guarantee, but verify non-empty at runtime
|
||||
val output = Renderer.render(Board.initial)
|
||||
assertTrue(output.nonEmpty)
|
||||
Reference in New Issue
Block a user