Skip to content

Commit

Permalink
feat: add the visualization of the initial state of a game
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-derevyanchenko committed Feb 23, 2023
1 parent a4de5b4 commit bfb5b6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Expand Up @@ -38,8 +38,11 @@ case class ChessBoardController private (
with ChessApplicationComponent:
private var chessBoardBelief: Map[Position, Piece] = Map.empty
this.cells.values.foreach(cell => cell.setOnMouseClicked { onCellClicked(_, cell) })
repaint()

// TODO Given that GamePageController must retrieve all data from the model,
// it might be that this method could be removed.
// In alternative it can be made that ChessGameController is in charge of retrieving all the pieces from the board,
// this would mean that it could be better to merge the two methods instead.
/** Retrieves the state of the chess engine service and shows it. */
def repaint(): Unit =
this.context.chessEngineProxy.getState.onSuccess { state =>
Expand Down
Expand Up @@ -7,7 +7,7 @@
package io.github.chess.viewcontroller.fxcomponents.controllers

import io.github.chess.events.PieceMovedEvent
import io.github.chess.viewcontroller.ChessApplication.given
import io.github.chess.viewcontroller.ChessApplication.{start, given}
import io.github.chess.viewcontroller.{ChessApplicationComponent, ChessApplicationContext}
import io.github.chess.viewcontroller.fxcomponents.controllers.ChessBoardController
import io.github.chess.viewcontroller.fxcomponents.controllers.template.FXMLController
Expand Down Expand Up @@ -45,10 +45,15 @@ class GamePageController(override protected val stage: Stage)(using
override def initialize(url: URL, resourceBundle: ResourceBundle): Unit =
chessBoardController = ChessBoardController.fromGridPane(this.chessBoardGridPane)(stage)
this.surrenderButton.onMouseClicked = _ => MainMenuPage(stage)
// TODO get the game status and set the state values in the beginning
context.chessEngineProxy.subscribe[PieceMovedEvent](
PieceMovedEvent.address(),
onPieceMoved
initView()
context.chessEngineProxy.subscribe(PieceMovedEvent.address(), onPieceMoved)

private def initView(): Unit =
context.chessEngineProxy.getState.onSuccess(status =>
chessBoardController.repaint(status.chessBoard.pieces)
currentTurnText.setText(status.currentTurn.toString)
timeRemainingText.setText("-:-:-")
lastMoveText.setText("N/A")
)

private def onPieceMoved(message: Message[PieceMovedEvent]): Unit =
Expand Down

0 comments on commit bfb5b6d

Please sign in to comment.