Skip to content

Commit

Permalink
feat: create game configuration + update related view
Browse files Browse the repository at this point in the history
  • Loading branch information
mirko-felice committed Feb 23, 2023
1 parent dcfa0b8 commit 8b8e67c
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 32 deletions.
98 changes: 73 additions & 25 deletions chess/src/main/resources/pages/game-configuration-page.fxml
Expand Up @@ -32,32 +32,80 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Text layoutX="147.0" layoutY="79.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Game Config">
<font>
<Font size="60.0" />
</font>
</Text>
<Button fx:id="startGameButton" focusTraversable="false" layoutX="193.0" layoutY="172.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="255.0" text="Start Game">
<font>
<Font size="20.0" />
</font>
</Button>
<Button fx:id="backButton" focusTraversable="false" layoutX="193.0" layoutY="238.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="255.0" text="Back">
<font>
<Font size="20.0" />
</font>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
<GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" percentWidth="25.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" percentWidth="20.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" percentWidth="10.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" percentWidth="20.0" />
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" percentWidth="25.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="112.0" minHeight="10.0" prefHeight="106.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="163.0" minHeight="5.0" prefHeight="119.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="145.0" minHeight="10.0" prefHeight="77.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="203.0" minHeight="10.0" prefHeight="87.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="147.0" minHeight="10.0" prefHeight="89.0" vgrow="SOMETIMES" />
</rowConstraints>
<Text strokeType="OUTSIDE" text="Game Config" GridPane.columnIndex="1" GridPane.columnSpan="3">
<font>
<Font size="60.0" />
</font>
</Text>
<Button fx:id="startGameButton" focusTraversable="false" mnemonicParsing="false" text="Start Game" GridPane.columnIndex="4" GridPane.rowIndex="4">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="backButton" focusTraversable="false" mnemonicParsing="false" text="Back" GridPane.rowIndex="4">
<font>
<Font size="18.0" />
</font>
</Button>
<ChoiceBox fx:id="timeConstraint" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Time Constraint" GridPane.rowIndex="1">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="Time (minutes)" GridPane.columnIndex="3" GridPane.rowIndex="1">
<font>
<Font size="18.0" />
</font>
</Label>
<Spinner fx:id="time" prefWidth="75.0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
<Label text="White Player" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="whitePlayer" promptText="White Player Name" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Black Player" GridPane.columnIndex="3" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="blackPlayer" promptText="Black Player Name" GridPane.columnIndex="4" GridPane.rowIndex="3">
<GridPane.margin>
<Insets left="20.0" right="20.0" />
</GridPane.margin>
</TextField>
<ChoiceBox fx:id="gameMode" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Label text="Mode" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="18.0" />
</font>
</Label>
</GridPane>
17 changes: 12 additions & 5 deletions chess/src/main/scala/io/github/chess/model/ChessGameStatus.scala
Expand Up @@ -6,6 +6,8 @@
*/
package io.github.chess.model

import io.github.chess.model.configuration.GameConfiguration

/** The state of a chess game. */
trait ChessGameStatus:
/** @return the chess board of this chess game */
Expand All @@ -17,8 +19,10 @@ trait ChessGameStatus:
/** @return the team who is currently playing */
def currentTurn: Team

// TODO: consider these
// def gameConfiguration: GameConfiguration
/** @return the configuration of the game */
def gameConfiguration: GameConfiguration

// TODO: consider this
// def timeRemaining: Time

/** Companion object of [[ChessGameStatus]]. */
Expand All @@ -27,18 +31,21 @@ object ChessGameStatus:
* @param chessBoard the chess board of the game
* @param history the history of the moves executed in the game
* @param initialTurn the team who is playing first
* @param gameConfiguration configuration of the game
* @return a state
*/
def apply(
chessBoard: ChessBoard = ChessBoard.standard,
history: ChessGameHistory = ChessGameHistory(),
initialTurn: Team = Team.WHITE
initialTurn: Team = Team.WHITE,
gameConfiguration: GameConfiguration = GameConfiguration.default
): ChessGameStatus =
BasicChessGameStatus(chessBoard, history, initialTurn)
BasicChessGameStatus(chessBoard, history, initialTurn, gameConfiguration)

/** Basic implementation of a [[ChessGameStatus]]. */
private case class BasicChessGameStatus(
chessBoard: ChessBoard,
history: ChessGameHistory,
currentTurn: Team
currentTurn: Team,
gameConfiguration: GameConfiguration
) extends ChessGameStatus
@@ -0,0 +1,33 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.configuration

import io.github.chess.model.*

/**
* Represents the configuration of the game containing all needed settings.
* @param timeConstraint [[TimeConstraint]] representing the time limit mode
* @param gameMode [[GameMode]] chosen by the user
* @param whitePlayer [[Player]] representing the white pieces
* @param blackPlayer [[Player]] representing the black pieces
*/
case class GameConfiguration(
var timeConstraint: TimeConstraint,
var gameMode: GameMode,
var whitePlayer: Player,
var blackPlayer: Player
)

object GameConfiguration:

/** Creates a default game configuration, with no time constraint, pvp game mode and no name players. */
def default: GameConfiguration = GameConfiguration(
TimeConstraint.NoLimit,
GameMode.PVP,
Player.noNameWhitePlayer,
Player.noNameBlackPlayer
)
@@ -0,0 +1,16 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.configuration

/** Represents the possible game mode for the chess game. */
enum GameMode:

/** Represents the game mode in which 2 human players play against each other. */
case PVP

/** Represents the game mode in which 1 human player plays against the computer. */
case PVE
@@ -0,0 +1,24 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.configuration

import io.github.chess.model.Team

/**
* Represents a player of the chess game.
* @param name his name
* @param team his [[Team]]
*/
case class Player(name: String, team: Team)

object Player:

/** Creates a no name white player. */
def noNameWhitePlayer: Player = Player("", Team.WHITE)

/** Creates a no name black player. */
def noNameBlackPlayer: Player = Player("", Team.BLACK)
@@ -0,0 +1,28 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.configuration

/** Represents the possible time constraints available to the chess game. */
enum TimeConstraint(var minutes: Int = TimeConstraint.NoMinutesPerMove):

/** Represents a no limit time constraint. */
case NoLimit extends TimeConstraint

/** Represents a time limit per move, defaulting to 5 minutes per move. */
case MoveLimit extends TimeConstraint(TimeConstraint.DefaultMinutesPerMove)

object TimeConstraint:

private final val NoMinutesPerMove = -1

private final val DefaultMinutesPerMove = 5

/** Minimum time limit per move. */
final val MinMoveLimit: Int = 1

/** Maximum time limit per move. */
final val MaxMoveLimit: Int = 30
Expand Up @@ -6,11 +6,14 @@
*/
package io.github.chess.viewcontroller.fxcomponents.controllers

import io.github.chess.model.configuration.{GameConfiguration, GameMode, Player, TimeConstraint}
import io.github.chess.model.Team
import io.github.chess.viewcontroller.ChessApplication.given
import io.github.chess.viewcontroller.{ChessApplicationComponent, ChessApplicationContext}
import io.github.chess.viewcontroller.fxcomponents.controllers.template.FXMLController
import io.github.chess.viewcontroller.fxcomponents.pages.{GamePage, MainMenuPage}
import javafx.scene.control.Button
import javafx.collections.{FXCollections, ObservableList}
import javafx.scene.control.{Button, ChoiceBox, Spinner, SpinnerValueFactory, TextField}
import scalafx.stage.Stage
import java.net.URL
import java.util.ResourceBundle
Expand All @@ -27,7 +30,44 @@ class GameConfigurationPageController(override protected val stage: Stage)(using
private var startGameButton: Button = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var backButton: Button = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var timeConstraint: ChoiceBox[TimeConstraint] = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var time: Spinner[Integer] = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var gameMode: ChoiceBox[GameMode] = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var whitePlayer: TextField = _
@FXML @SuppressWarnings(Array("org.wartremover.warts.Null"))
private var blackPlayer: TextField = _

override def initialize(url: URL, resourceBundle: ResourceBundle): Unit =
this.startGameButton.onMouseClicked = _ => GamePage(stage)
this.backButton.onMouseClicked = _ => MainMenuPage(stage)

this.stage.setResizable(false)
this.timeConstraint.setItems(FXCollections.observableArrayList(TimeConstraint.values*))
this.timeConstraint.setValue(TimeConstraint.NoLimit)
this.timeConstraint.onAction = _ =>
this.time.setDisable(this.timeConstraint.getValue == TimeConstraint.NoLimit)
this.time.setDisable(true)
this.time.setValueFactory(
SpinnerValueFactory.IntegerSpinnerValueFactory(
TimeConstraint.MinMoveLimit,
TimeConstraint.MaxMoveLimit,
TimeConstraint.MoveLimit.minutes
)
)

this.gameMode.setItems(FXCollections.observableArrayList(GameMode.PVP))
this.gameMode.setValue(GameMode.PVP)
this.startGameButton.onMouseClicked = _ =>
val timeConstraint = this.timeConstraint.getValue
timeConstraint.minutes = this.time.getValue
val gameConfiguration = GameConfiguration(
timeConstraint,
this.gameMode.getValue,
Player(this.whitePlayer.getText, Team.WHITE),
Player(this.blackPlayer.getText, Team.BLACK)
)
// TODO pass game configuration
GamePage(stage)

0 comments on commit 8b8e67c

Please sign in to comment.