feat: NCS-10 Implement Pawn Promotion (#12)
Build & Test (NowChessSystems) TeamCity build finished
Build & Test (NowChessSystems) TeamCity build finished
Reviewed-on: #12 Reviewed-by: Leon Hermann <lq@blackhole.local> Co-authored-by: Janis <janis-e@gmx.de> Co-committed-by: Janis <janis-e@gmx.de>
This commit was merged in pull request #12.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.nowchess.ui.terminal
|
||||
|
||||
import scala.io.StdIn
|
||||
import de.nowchess.api.move.PromotionPiece
|
||||
import de.nowchess.chess.engine.GameEngine
|
||||
import de.nowchess.chess.observer.{Observer, GameEvent, *}
|
||||
import de.nowchess.chess.view.Renderer
|
||||
@@ -11,6 +12,7 @@ import de.nowchess.chess.view.Renderer
|
||||
*/
|
||||
class TerminalUI(engine: GameEngine) extends Observer:
|
||||
private var running = true
|
||||
private var awaitingPromotion = false
|
||||
|
||||
/** Called by GameEngine whenever a game event occurs. */
|
||||
override def onGameEvent(event: GameEvent): Unit =
|
||||
@@ -44,6 +46,10 @@ class TerminalUI(engine: GameEngine) extends Observer:
|
||||
print(Renderer.render(e.board))
|
||||
printPrompt(e.turn)
|
||||
|
||||
case _: PromotionRequiredEvent =>
|
||||
println("Promote to: q=Queen, r=Rook, b=Bishop, n=Knight")
|
||||
synchronized { awaitingPromotion = true }
|
||||
|
||||
/** Start the terminal UI game loop. */
|
||||
def start(): Unit =
|
||||
// Register as observer
|
||||
@@ -57,14 +63,26 @@ class TerminalUI(engine: GameEngine) extends Observer:
|
||||
// Game loop
|
||||
while running do
|
||||
val input = Option(StdIn.readLine()).getOrElse("quit").trim
|
||||
input.toLowerCase match
|
||||
case "quit" | "q" =>
|
||||
running = false
|
||||
println("Game over. Goodbye!")
|
||||
case "" =>
|
||||
printPrompt(engine.turn)
|
||||
case _ =>
|
||||
engine.processUserInput(input)
|
||||
synchronized {
|
||||
if awaitingPromotion then
|
||||
input.toLowerCase match
|
||||
case "q" => awaitingPromotion = false; engine.completePromotion(PromotionPiece.Queen)
|
||||
case "r" => awaitingPromotion = false; engine.completePromotion(PromotionPiece.Rook)
|
||||
case "b" => awaitingPromotion = false; engine.completePromotion(PromotionPiece.Bishop)
|
||||
case "n" => awaitingPromotion = false; engine.completePromotion(PromotionPiece.Knight)
|
||||
case _ =>
|
||||
println("Invalid choice. Enter q, r, b, or n.")
|
||||
println("Promote to: q=Queen, r=Rook, b=Bishop, n=Knight")
|
||||
else
|
||||
input.toLowerCase match
|
||||
case "quit" | "q" =>
|
||||
running = false
|
||||
println("Game over. Goodbye!")
|
||||
case "" =>
|
||||
printPrompt(engine.turn)
|
||||
case _ =>
|
||||
engine.processUserInput(input)
|
||||
}
|
||||
|
||||
// Unsubscribe when done
|
||||
engine.unsubscribe(this)
|
||||
@@ -73,4 +91,3 @@ class TerminalUI(engine: GameEngine) extends Observer:
|
||||
val undoHint = if engine.canUndo then " [undo]" else ""
|
||||
val redoHint = if engine.canRedo then " [redo]" else ""
|
||||
print(s"${turn.label}'s turn. Enter move (or 'quit'/'q' to exit)$undoHint$redoHint: ")
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.scalatest.matchers.should.Matchers
|
||||
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
|
||||
import de.nowchess.chess.engine.GameEngine
|
||||
import de.nowchess.chess.observer.*
|
||||
import de.nowchess.api.board.{Board, Color}
|
||||
import de.nowchess.api.board.{Board, Color, File, Rank, Square}
|
||||
import de.nowchess.chess.logic.GameHistory
|
||||
|
||||
class TerminalUITest extends AnyFunSuite with Matchers {
|
||||
@@ -186,4 +186,142 @@ class TerminalUITest extends AnyFunSuite with Matchers {
|
||||
// The move should have been processed and the board displayed
|
||||
engine.turn shouldBe Color.Black
|
||||
}
|
||||
|
||||
test("TerminalUI shows promotion prompt on PromotionRequiredEvent") {
|
||||
val out = new ByteArrayOutputStream()
|
||||
val engine = new GameEngine()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withOut(out) {
|
||||
ui.onGameEvent(PromotionRequiredEvent(
|
||||
Board(Map.empty), GameHistory(), Color.White,
|
||||
Square(File.E, Rank.R7), Square(File.E, Rank.R8)
|
||||
))
|
||||
}
|
||||
|
||||
out.toString should include("Promote to")
|
||||
}
|
||||
|
||||
test("TerminalUI routes promotion choice to engine.completePromotion") {
|
||||
import de.nowchess.api.move.PromotionPiece
|
||||
|
||||
var capturedPiece: Option[PromotionPiece] = None
|
||||
|
||||
val engine = new GameEngine() {
|
||||
override def processUserInput(rawInput: String): Unit =
|
||||
if rawInput.trim == "e7e8" then
|
||||
notifyObservers(PromotionRequiredEvent(
|
||||
Board(Map.empty), GameHistory.empty, Color.White,
|
||||
Square(File.E, Rank.R7), Square(File.E, Rank.R8)
|
||||
))
|
||||
override def completePromotion(piece: PromotionPiece): Unit =
|
||||
capturedPiece = Some(piece)
|
||||
notifyObservers(MoveExecutedEvent(Board(Map.empty), GameHistory.empty, Color.Black, "e7", "e8", None))
|
||||
}
|
||||
|
||||
val in = new ByteArrayInputStream("e7e8\nq\nquit\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withIn(in) {
|
||||
Console.withOut(out) {
|
||||
ui.start()
|
||||
}
|
||||
}
|
||||
|
||||
capturedPiece should be(Some(PromotionPiece.Queen))
|
||||
out.toString should include("Promote to")
|
||||
}
|
||||
|
||||
test("TerminalUI re-prompts on invalid promotion choice") {
|
||||
import de.nowchess.api.move.PromotionPiece
|
||||
|
||||
var capturedPiece: Option[PromotionPiece] = None
|
||||
|
||||
val engine = new GameEngine() {
|
||||
override def processUserInput(rawInput: String): Unit =
|
||||
if rawInput.trim == "e7e8" then
|
||||
notifyObservers(PromotionRequiredEvent(
|
||||
Board(Map.empty), GameHistory.empty, Color.White,
|
||||
Square(File.E, Rank.R7), Square(File.E, Rank.R8)
|
||||
))
|
||||
override def completePromotion(piece: PromotionPiece): Unit =
|
||||
capturedPiece = Some(piece)
|
||||
notifyObservers(MoveExecutedEvent(Board(Map.empty), GameHistory.empty, Color.Black, "e7", "e8", None))
|
||||
}
|
||||
|
||||
// "x" is invalid, then "r" for rook
|
||||
val in = new ByteArrayInputStream("e7e8\nx\nr\nquit\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withIn(in) {
|
||||
Console.withOut(out) {
|
||||
ui.start()
|
||||
}
|
||||
}
|
||||
|
||||
capturedPiece should be(Some(PromotionPiece.Rook))
|
||||
out.toString should include("Invalid")
|
||||
}
|
||||
|
||||
test("TerminalUI routes Bishop promotion choice to engine.completePromotion") {
|
||||
import de.nowchess.api.move.PromotionPiece
|
||||
|
||||
var capturedPiece: Option[PromotionPiece] = None
|
||||
|
||||
val engine = new GameEngine() {
|
||||
override def processUserInput(rawInput: String): Unit =
|
||||
if rawInput.trim == "e7e8" then
|
||||
notifyObservers(PromotionRequiredEvent(
|
||||
Board(Map.empty), GameHistory.empty, Color.White,
|
||||
Square(File.E, Rank.R7), Square(File.E, Rank.R8)
|
||||
))
|
||||
override def completePromotion(piece: PromotionPiece): Unit =
|
||||
capturedPiece = Some(piece)
|
||||
notifyObservers(MoveExecutedEvent(Board(Map.empty), GameHistory.empty, Color.Black, "e7", "e8", None))
|
||||
}
|
||||
|
||||
val in = new ByteArrayInputStream("e7e8\nb\nquit\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withIn(in) {
|
||||
Console.withOut(out) {
|
||||
ui.start()
|
||||
}
|
||||
}
|
||||
|
||||
capturedPiece should be(Some(PromotionPiece.Bishop))
|
||||
}
|
||||
|
||||
test("TerminalUI routes Knight promotion choice to engine.completePromotion") {
|
||||
import de.nowchess.api.move.PromotionPiece
|
||||
|
||||
var capturedPiece: Option[PromotionPiece] = None
|
||||
|
||||
val engine = new GameEngine() {
|
||||
override def processUserInput(rawInput: String): Unit =
|
||||
if rawInput.trim == "e7e8" then
|
||||
notifyObservers(PromotionRequiredEvent(
|
||||
Board(Map.empty), GameHistory.empty, Color.White,
|
||||
Square(File.E, Rank.R7), Square(File.E, Rank.R8)
|
||||
))
|
||||
override def completePromotion(piece: PromotionPiece): Unit =
|
||||
capturedPiece = Some(piece)
|
||||
notifyObservers(MoveExecutedEvent(Board(Map.empty), GameHistory.empty, Color.Black, "e7", "e8", None))
|
||||
}
|
||||
|
||||
val in = new ByteArrayInputStream("e7e8\nn\nquit\n".getBytes)
|
||||
val out = new ByteArrayOutputStream()
|
||||
val ui = new TerminalUI(engine)
|
||||
|
||||
Console.withIn(in) {
|
||||
Console.withOut(out) {
|
||||
ui.start()
|
||||
}
|
||||
}
|
||||
|
||||
capturedPiece should be(Some(PromotionPiece.Knight))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user