Skip to content

Commit

Permalink
added pieces functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Light2Dark committed Jul 24, 2022
1 parent 3561cfe commit 0ed0ad2
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 181 deletions.
Binary file removed src/main/musicResource/battlechess.mp3
Binary file not shown.
15 changes: 11 additions & 4 deletions src/main/scala/GameController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@ import java.io.File
@sfxml
class GameController(private val chessBoard: AnchorPane, private val playerOneName: Text, private val playerTwoName: Text, val redTimer: Text, val blackTimer: Text) {

val playerOneTimer: Timer = ChessBoard.timerPlayerOne
val playerTwoTimer: Timer = ChessBoard.timerPlayerTwo
val playerOneTimer: Timer = ChessBoard.playerOne.timer
val playerTwoTimer: Timer = ChessBoard.playerTwo.timer

playerOneName.text.value = ChessBoard.playerOne.name
playerTwoName.text.value = ChessBoard.playerTwo.name

// setting timertext in TimerPanel
TimerPanel.redTimerText = redTimer
TimerPanel.blackTimerText = blackTimer
blackTimer.text = playerTwoTimer.timeToString()

// redTimer.text <== playerOneTimer.timeString()
playerOneTimer.startTimer()

TimerPanel.updateTime(redTeam)
def updateTimerPanel() = { // interaction with timer panel, to update time, reset (future feature)
TimerPanel.updateTime(redTeam)
}
updateTimerPanel()

// placing all chess pieces
chessBoard.children = ChessBoard.pieces
ChessBoard.board = chessBoard


// audio
// val audio = new Media(getClass.getResource("../musicResource/battlechess.mp3").toURI.toString)
// val audio = new Media(getClass.getResource("resources/music/battlechess.mp3").toURI.toString)
// val musicPlayer = new MediaPlayer(media = audio)
// musicPlayer.setCycleCount(MediaPlayer.Indefinite)
// musicPlayer.play()

// for (i <- 0 to 8) {
// val piece = new Horse(xTopLeft + (i * xJump) + i, yTopLeft, redTeam)
// pieces = pieces :+ piece
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/RootController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ class RootController {
System.exit(0)
}
}

4 changes: 2 additions & 2 deletions src/main/scala/StartMenuController.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import model.{ChessBoard, Player}
import model.{ChessBoard, Player, TimerPanel}
import scalafx.collections.ObservableBuffer
import scalafx.scene.control.{Alert, Button, ChoiceBox, TextField}
import scalafxml.core.macros.sfxml
Expand Down Expand Up @@ -33,7 +33,7 @@ class StartMenuController(private val startGameButton: Button, val playerOneInpu
}

def startGame() = {
val resource = getClass.getResource("view/Game.fxml")
val resource = getClass.getResource("view/Game3.fxml") // ori was Game!!

if (hasValidInput()) {
ChessBoard.playerOne.name = playerOneInput.text.value
Expand Down
108 changes: 54 additions & 54 deletions src/main/scala/model/ChessBoard.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package model

import model.ChessPieces.{Cannon, Chariot, ChessPiece, Elephant, General, Guard, Horse, Soldier}
import utils.Teams
import utils.Teams.{Team, blackTeam, redTeam}
import scalafx.scene.layout.AnchorPane
import utils.Teams.{blackTeam, redTeam}
import utils.Maths._

/*
Expand All @@ -13,25 +13,9 @@ Creates jump amount etc.
Handles captures, attacks, checkmates
*/

/*
Each piece is represented by a string. The first part of the string is the piece type, the second part is the team.
Eg: Hr means Horse in Red team.
O = Empty
S = Soldier
H = Horse
G = General
E = Elephant
C = Cannon
A = Advisor
R = Rook
b = Black team
r = Red team
*/

object ChessBoard {

// Alternative representation
// val boardx = Array.fill(10,9)("O") // create board with 10 rows, 9 cols and fill it with O to indicate empty
// val board: Array[Array[String]] = Array(
// Array("Rb", "Hb", "Eb", "Ab", "Gb", "Ab", "Eb", "Hb", "Rb"),
Expand All @@ -46,46 +30,46 @@ object ChessBoard {
// Array("Sr", "O", "Sr", "O", "Sr", "O", "Sr", "O", "Sr", "O"),
// )
// board foreach { row => row foreach print; println }

val playerOne: Player = new Player()
val playerTwo: Player = new Player()
var board: AnchorPane = new AnchorPane()

// time limit settings
var timeLimit: Double = 15 // default timeLimit
var timerPlayerOne: Timer = new Timer(timeLimit)
var timerPlayerTwo: Timer = new Timer(timeLimit)

def changeTimer(timeLimit: Double) = {
timerPlayerOne = new Timer(timeLimit)
timerPlayerTwo = new Timer(timeLimit)
def changeTimer(timeLimit: Double): Unit = {
playerOne.timer.timeLimit(timeLimit)
playerTwo.timer.timeLimit(timeLimit)
}

// ROW INDEX and COL INDEX so that we can track the chess piece position and delete, move it etc.
var pieces: List[ChessPiece] = List() // use list as we will iterate through the list often to check valid moves, obstruction etc.
pieces = pieces ++ List( // red team, down side
new Soldier(xTopLeft + 0 * xJumpValue, yTopLeft + riverJump + 5 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 0),
new Soldier(xTopLeft + 2 * xJumpValue, yTopLeft + riverJump + 5 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 2),
new Soldier(xTopLeft + 4 * xJumpValue, yTopLeft + riverJump + 5 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 4),
new Soldier(xTopLeft + 6 * xJumpValue, yTopLeft + riverJump + 5 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 6),
new Soldier(xTopLeft + 8 * xJumpValue, yTopLeft + riverJump + 5 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 8),
new General(xTopLeft + 4 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 4),
new Guard(xTopLeft + 3 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 3),
new Guard(xTopLeft + 5 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 5),
new Elephant(xTopLeft + 2 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 2),
new Elephant(xTopLeft + 6 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 6),
new Horse(xTopLeft + 1 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 1),
new Horse(xTopLeft + 7 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 7),
new Chariot(xTopLeft + 8 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 8),
new Chariot(xTopLeft + 0 * xJumpValue, yTopLeft + riverJump + 8 * yJumpValue, redTeam, _rowIndex = 10, _colIndex = 0),
new Cannon(xTopLeft + 1 * xJumpValue, yTopLeft + riverJump + 6 * yJumpValue, redTeam, _rowIndex = 8, _colIndex = 1),
new Cannon(xTopLeft + 7 * xJumpValue, yTopLeft + riverJump + 6 * yJumpValue, redTeam, _rowIndex = 8, _colIndex = 7),
new Soldier(xTopLeft + 0 * xJumpValue, yTopLeft + 6 * yJumpValue, redTeam, _rowIndex = 6, _colIndex = 0),
new Soldier(xTopLeft + 2 * xJumpValue, yTopLeft + 6 * yJumpValue, redTeam, _rowIndex = 6, _colIndex = 2),
new Soldier(xTopLeft + 4 * xJumpValue, yTopLeft + 6 * yJumpValue, redTeam, _rowIndex = 6, _colIndex = 4),
new Soldier(xTopLeft + 6 * xJumpValue, yTopLeft + 6 * yJumpValue, redTeam, _rowIndex = 6, _colIndex = 6),
new Soldier(xTopLeft + 8 * xJumpValue, yTopLeft + 6 * yJumpValue, redTeam, _rowIndex = 6, _colIndex = 8),
new General(xTopLeft + 4 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 4),
new Guard(xTopLeft + 3 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 3),
new Guard(xTopLeft + 5 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 5),
new Elephant(xTopLeft + 2 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 2),
new Elephant(xTopLeft + 6 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 6),
new Horse(xTopLeft + 1 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 1),
new Horse(xTopLeft + 7 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 7),
new Chariot(xTopLeft + 8 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 8),
new Chariot(xTopLeft + 0 * xJumpValue, yTopLeft + 9 * yJumpValue, redTeam, _rowIndex = 9, _colIndex = 0),
new Cannon(xTopLeft + 1 * xJumpValue, yTopLeft + 7 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 1),
new Cannon(xTopLeft + 7 * xJumpValue, yTopLeft + 7 * yJumpValue, redTeam, _rowIndex = 7, _colIndex = 7),
)

pieces = pieces ++ List( // black team, up side
new Soldier(xTopLeft + 0 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 4, _colIndex = 0),
new Soldier(xTopLeft + 2 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 4, _colIndex = 2),
new Soldier(xTopLeft + 4 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 4, _colIndex = 4),
new Soldier(xTopLeft + 6 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 4, _colIndex = 6),
new Soldier(xTopLeft + 8 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 4, _colIndex = 8),
new Soldier(xTopLeft + 0 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 3, _colIndex = 0),
new Soldier(xTopLeft + 2 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 3, _colIndex = 2),
new Soldier(xTopLeft + 4 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 3, _colIndex = 4),
new Soldier(xTopLeft + 6 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 3, _colIndex = 6),
new Soldier(xTopLeft + 8 * xJumpValue, yTopLeft + 3 * yJumpValue, blackTeam, _rowIndex = 3, _colIndex = 8),
new General(xTopLeft + 4 * xJumpValue, yTopLeft, blackTeam, text = "", _rowIndex = 0, _colIndex = 4),
new Guard(xTopLeft + 3 * xJumpValue, yTopLeft, blackTeam, text = "", _rowIndex = 0, _colIndex = 3),
new Guard(xTopLeft + 5 * xJumpValue, yTopLeft, blackTeam, text = "", _rowIndex = 0, _colIndex = 5),
Expand Down Expand Up @@ -120,8 +104,24 @@ object ChessBoard {
// }
// }

def movePiece = {
// returns whether a piece has been successfully deleted, if false, means no piece there
def deletePiece(colIndex: Int, rowIndex: Int): Unit = {
pieces = pieces.filterNot(chessPiece => (chessPiece.rowIndex == rowIndex && chessPiece.colIndex == colIndex))
board.children = pieces
}

def getPiece(colIndex: Int, rowIndex: Int): Option[ChessPiece] = {
val pieceList = pieces.filter(chessPiece => (chessPiece.rowIndex == rowIndex && chessPiece.colIndex == colIndex))
if (pieceList.length == 0) {
None
} else {
Option(pieceList.head)
}
}

def isPiece(colIndex: Int, rowIndex: Int): Boolean = {
val pieceList = pieces.filter(chessPiece => (chessPiece.rowIndex == rowIndex && chessPiece.colIndex == colIndex))
if (pieceList.length == 0) false else true
}

// checks if there is an obstruction on the col and row index that the piece wants to move in
Expand All @@ -146,18 +146,18 @@ object ChessBoard {
(0,0) // no match
}

def checkRiver() = {}
// def checkRiver() = {}

def switchTurn = {
def switchTurn(): Unit = {
_player1Turn = !_player1Turn

if (timerPlayerOne.running) {
timerPlayerOne.stopTimer()
timerPlayerTwo.startTimer()
if (playerOne.timer.running) {
playerOne.timer.stopTimer()
playerTwo.timer.startTimer()
TimerPanel.updateTime(blackTeam)
} else if (timerPlayerTwo.running) {
timerPlayerOne.startTimer()
timerPlayerTwo.stopTimer()
} else if (playerTwo.timer.running) {
playerOne.timer.startTimer()
playerTwo.timer.stopTimer()
TimerPanel.updateTime(redTeam)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/model/ChessPieces/Cannon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class Cannon(_x: Double, _y: Double, team: Team, private val text: String = "炮
def movePiece(deltaX: Double, deltaY: Double): Boolean = {
false
}

def validDragging(deltaX: Double, deltaY: Double): Boolean = false
}
2 changes: 2 additions & 0 deletions src/main/scala/model/ChessPieces/Chariot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class Chariot(_x: Double, _y: Double, team: Team, private val text: String = "
def movePiece(deltaX: Double, deltaY: Double): Boolean = {
false
}

def validDragging(deltaX: Double, deltaY: Double): Boolean = false
}
80 changes: 22 additions & 58 deletions src/main/scala/model/ChessPieces/ChessPiece.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,15 @@ import scalafx.Includes._
import scalafx.scene.input.DragEvent
import scalafx.scene.layout.StackPane
import scalafx.scene.text.Font

import scala.collection.mutable
//import scalafx.scene.input.MouseEvent
import scalafx.scene.paint.Color._
import scalafx.scene.shape.Circle
import utils.Maths._
import utils.Teams.{Team, blackTeam, redTeam}
import scalafx.scene.text.Text
import model.ChessBoard
// src/main/scala/GameController.scala
//abstract class ChessPiece(var x: Double, var y: Double, team: Team, text: String) extends Circle {
// layoutX = x
// layoutY = y
// radius = 20.0
// fill = SandyBrown
// strokeWidth = 1.5
//
// val pieceText = new Text {
// text = "Scala"
// style = "-fx-font: normal bold 100pt sans-serif"
// fill = Red
// }
//
//
// def resetPos = {
// this.layoutX = x
// this.layoutY = y
// }
//
// if (team == redTeam) {
// stroke <== when(hover) choose Yellow otherwise Red
// } else if (team == blackTeam) {
// stroke <== when(hover) choose Yellow otherwise Black
// }
//
// // def cursor_=(v: Cursor): Unit
//
// this.onMouseClicked = (event: Any )=> {
// click
// }
//
// this.onMouseReleased = (event: MouseEvent) => {
// // before moving, check if valid
//
// // reset
// resetPos
//
// move(event.getSceneX - 190, event.getSceneY - 110)
// }
//
// this.onMouseDragged = (event: MouseEvent) => {
// val xPos = event.getSceneX - 190 // need to set offset based on screen size but we will disable resizing for now
// val yPos = event.getSceneY - 110
//
// val xMove = clamp(xPos, -5, 355)
// val yMove = clamp(yPos, 10, 400)
// this.relocate(xMove, yMove)
// }
//
//
//
// def click: Unit = {println("clicked")}
// def move(moveX: Double, moveY: Double)
//// def capture: Unit
//// def die: Unit
//}

abstract class ChessPiece(var x: Double, var y: Double, val team: Team, text: String, var rowIndex: Int, var colIndex: Int) extends StackPane {
this.layoutY = y
Expand All @@ -95,7 +40,7 @@ abstract class ChessPiece(var x: Double, var y: Double, val team: Team, text: St
}

this.onMouseClicked = (event: Any )=> {
println("click")
// to-do: Add points/squares where the piece can move to
}

// def cursor_=(v: Cursor): Unit
Expand Down Expand Up @@ -139,7 +84,26 @@ abstract class ChessPiece(var x: Double, var y: Double, val team: Team, text: St
}
}

// if there is an enemy piece, delete it and return true. If same team's piece, return false
def captureAndMove(colIndex: Int, rowIndex: Int): Boolean = {
val piece = ChessBoard.getPiece(colIndex, rowIndex)
if (piece.isEmpty) return true// no piece on that square

// must be chess piece
if (piece.get.team != this.team) {
ChessBoard.deletePiece(colIndex, rowIndex) // captures the enemy piece
println("Piece is captured!")
true
} else {
println("Already a piece there")
false
}
}

def movePiece(deltaX: Double, deltaY: Double): Boolean // return whether piece has been moved successfully
def validDragging(deltaX: Double, deltaY: Double): Boolean // another abstract method
var xDragBoundary: mutable.HashMap[String, Double] = mutable.HashMap("min" -> 10 , "max" -> 60) // represents boundary of x-axis this piece can move, min and max
var yDragBoundary: mutable.HashMap[String, Double] = mutable.HashMap("min" -> 30 , "max" -> 100) // represents boundary of y-axis this piece can move, min and max

def endTurn: Unit = {
ChessBoard.switchTurn
Expand Down
Loading

0 comments on commit 0ed0ad2

Please sign in to comment.