Skip to content

Commit

Permalink
Add support for CLI frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
zbyju committed Oct 13, 2024
1 parent bcf8947 commit 979e01a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import model.board.Board
import model.board.Coords
import model.PieceType
import model.Color
import model.board.ChessBoard
import view.ChessPresenter
import view.CLI.presenter.CLIChessPresenter

@main def hello: Unit =
// val board = Board.fromFENBoard("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR")
val board = Board.initial
println(board)
object Main {
def main(args: Array[String]): Unit = {
// Initialize the chessboard state (assuming you have a model ready)
val board = ChessBoard.initial

val c1 = Coords.fromString("a2").get
val c2 = Coords.fromString("a1").get
val c3 = Coords.fromString("a8").get
val c4 = Coords.fromString("a7").get
val c5 = Coords.fromString("d4").get
val presenter: ChessPresenter = new CLIChessPresenter()

println(board.isEmpty(c1))
println(board.isEmpty(c2))
println(board.isEmpty(c3))
println(board.isEmpty(c4))
println(board.isEmpty(c5))
println(board.getPieceAndColorAt(c1))
println(board.getPieceAndColorAt(c2))
println(board.getPieceAndColorAt(c3))
println(board.getPieceAndColorAt(c4))
println(board.getPieceAndColorAt(c5))

println(
board.setPiece(PieceType.Queen, Color.White, Coords.fromString("d7").get)
)
presenter.start(board)
}
}
91 changes: 91 additions & 0 deletions src/main/scala/view/CLI/CLIChessPresenter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package view.CLI

package presenter

import model.board.ChessBoard
import scala.io.StdIn.readLine
import view.ChessPresenter
import model.board.Coords
import Console.{GREEN, BLUE, RESET, BOLD}

class CLIChessPresenter extends ChessPresenter {

override def start(board: ChessBoard): Unit = {
println("Welcome to Chess CLI!")

var continue = true
while (continue) {
render(board)

println(
"Enter a coordinate to highlight possible moves (e.g., 'b2') or 'q' to quit:"
)
val input = readLine().trim.toLowerCase

input match {
case "q" =>
continue = false
println("Goodbye!")
case coord if coord.matches("^[a-h][1-8]$") =>
val bitboardPossible =
board.theoreticalMoves(Coords.fromString(coord).get)
val possibleMoves =
bitboardPossible.board.positionsOfSetBits().map(_.toCoords())
highlightTiles(board, possibleMoves)
case coord if coord.matches("^[a-h][1-8] [a-h][1-8]$") =>
println("Making a move")
case _ =>
println(
"Invalid command. Please enter a valid coordinate (e.g., 'b2') or 'q' to quit."
)
}
}
}

// Method to render the chessboard state to the console
private def render(board: ChessBoard): Unit = {
println("Current Board State:")
printBoard(board, Seq.empty)
}

// Method to highlight specific tiles (e.g., possible moves)
private def highlightTiles(
board: ChessBoard,
highlightedTiles: Seq[Coords]
): Unit = {
printBoard(board, highlightedTiles)
}

// Helper method to print the board, highlighting specific tiles
private def printBoard(
board: ChessBoard,
highlightedTiles: Seq[Coords]
): Unit = {
val sb = new StringBuilder
for (rank <- 7 to 0 by -1) {
sb.append(s"${rank + 1}|")
for (file <- 0 to 7) {
val pos = Coords(rank.toByte, file.toByte).get
val piece = board.board.getPieceAndColorAt(pos) match {
case Some((pieceType, color)) =>
pieceType.toChar(color)
case None => '.'
}
val pieceStr =
if (highlightedTiles.contains(pos)) then highlighted(piece.toString)
else piece

sb.append(s"$pieceStr ")
}
sb.append("\n")
}
sb.append("------------------\n")
sb.append(" |a b c d e f g h\n")
print(sb.toString)
}

// Helper method to format a tile for highlighting (in bold text)
private def highlighted(str: String): String = {
s"${BOLD}${BLUE}${str}${RESET}"
}
}
7 changes: 7 additions & 0 deletions src/main/scala/view/ChessPresenter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package view

import model.board.ChessBoard

trait ChessPresenter {
def start(board: ChessBoard): Unit
}

0 comments on commit 979e01a

Please sign in to comment.