diff options
Diffstat (limited to 'src/main/scala/me/robbyzambito/othello/game/Game.scala')
-rw-r--r-- | src/main/scala/me/robbyzambito/othello/game/Game.scala | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/main/scala/me/robbyzambito/othello/game/Game.scala b/src/main/scala/me/robbyzambito/othello/game/Game.scala index 4c0e7a2..6fc278f 100644 --- a/src/main/scala/me/robbyzambito/othello/game/Game.scala +++ b/src/main/scala/me/robbyzambito/othello/game/Game.scala @@ -17,6 +17,10 @@ case class Game(board: Board, val currentPlayer: Player = players(turnCount % players.length) val currentOpponent: Player = players(turnCount % players.length) + /** + * Plays the game. + * @param game Current state of the game. + */ @tailrec final def loop(game: Game = this): Unit = { if (game.winner.isEmpty) { @@ -38,10 +42,10 @@ case class Game(board: Board, * * [[None]] if there has not been a winner yet. Otherwise return the [[Player]] which has won. */ - lazy val winner: Option[Player] = { - if (currentPlayer.canMove(board) || (!currentPlayer.canMove(board) && currentOpponent.canMove(board))) + val winner: Option[Player] = { + if (currentPlayer.canMove(board) || currentOpponent.canMove(board)) None - else { // No one can move + else { // No one can move => Game is finished val whiteCount = board.positions.flatten.count(_ == Position.WHITE) val blackCount = board.positions.flatten.count(_ == Position.BLACK) @@ -64,9 +68,15 @@ case class Game(board: Board, def takeTurn: Game = { println(s"${this}\n") - val move = currentPlayer.nextMove(board) + if (currentPlayer.canMove(board)) { + val move = currentPlayer.nextMove(board) - this.copy(board = move(board, currentPlayer), turnCount = turnCount + 1) + this.copy(board = move(board, currentPlayer), turnCount = turnCount + 1) + } else { + println(s"$currentPlayer has no moves! Skipping turn...") + + this.copy(board = board, turnCount = turnCount + 1) + } } override def toString: String = |