From c05e5f3015916cbc927b0120797499a73bfb553c Mon Sep 17 00:00:00 2001 From: dodikk Date: Sat, 19 Dec 2015 01:04:14 +0200 Subject: [PATCH] Added "pass turn" and "game over" --- Reversi/Assets/BoardEventsHandler.cs | 29 ++++++++++++++++++++++-- Reversi/Assets/ReversiKit/IBoardState.cs | 2 ++ Reversi/Assets/ReversiKit/MatrixBoard.cs | 19 +++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Reversi/Assets/BoardEventsHandler.cs b/Reversi/Assets/BoardEventsHandler.cs index 8a24770..50da452 100644 --- a/Reversi/Assets/BoardEventsHandler.cs +++ b/Reversi/Assets/BoardEventsHandler.cs @@ -15,6 +15,8 @@ public class BoardEventsHandler : MonoBehaviour // Use this for initialization void Start() { + this._isGameOver = false; + this._mutableBoardModel = new MatrixBoard(); this._turnCalculator = new TurnCalculator(); this._boardModel = this._mutableBoardModel; @@ -78,8 +80,19 @@ private void handleTapOnCell(GameObject cellCube) if (null == this._validTurns || 0 == this._validTurns.Count()) { - // TODO : Pass turn - // Or GameOver + // Passing the turn if current user can't make it. + this._boardModel.PassTurn(); + + + // Game over ??? + this.getAvailableTurns(); + if (null == this._validTurns || 0 == this._validTurns.Count()) + { + // Yes. Game over. + + this._turnLabel.text = "Game Over"; + this._isGameOver = true; + } return; } @@ -107,6 +120,12 @@ private void makeTurn(IReversiTurn turn) this._boardModel.ApplyTurn(turn); this.getAvailableTurns(); this.highlightAvailableTurns(); + + if (0 == this._boardModel.NumberOfFreeCells) + { + this._turnLabel.text = "Game Over"; + this._isGameOver = true; + } } private void drawChangesForTurn(IReversiTurn turn) @@ -152,6 +171,11 @@ private void setColourForBallAtCell(Material activePlayerColour, ICellCoordinate private void updateTurnLabel() { + if (this._isGameOver) + { + return; + } + this._turnLabel.text = this._boardModel.IsTurnOfBlackPlayer ? "Black Player Turn" : @@ -392,4 +416,5 @@ private void populateLabels() private const string BALL_TAG = "Ball"; private IEnumerable _validTurns ; + bool _isGameOver; } diff --git a/Reversi/Assets/ReversiKit/IBoardState.cs b/Reversi/Assets/ReversiKit/IBoardState.cs index 99a9d4f..2893c8a 100644 --- a/Reversi/Assets/ReversiKit/IBoardState.cs +++ b/Reversi/Assets/ReversiKit/IBoardState.cs @@ -19,9 +19,11 @@ public interface IBoardState int NumberOfBlackPieces { get; } int NumberOfWhitePieces { get; } + int NumberOfFreeCells { get; } void ApplyTurn(IReversiTurn turn); + void PassTurn(); } } diff --git a/Reversi/Assets/ReversiKit/MatrixBoard.cs b/Reversi/Assets/ReversiKit/MatrixBoard.cs index e094268..b29ce0e 100644 --- a/Reversi/Assets/ReversiKit/MatrixBoard.cs +++ b/Reversi/Assets/ReversiKit/MatrixBoard.cs @@ -201,6 +201,14 @@ public int NumberOfWhitePieces return this._flattenCells.Count(c => this.IsCellTakenByWhite(c)); } } + + public int NumberOfFreeCells + { + get + { + return this._flattenCells.Count(c => this.IsCellFree(c)); + } + } #endregion #region Mutable @@ -259,9 +267,18 @@ public void ApplyTurn(IReversiTurn turn) this._cells[flippedCell.Row, flippedCell.Column] = newState; } - this.IsTurnOfBlackPlayer = !this.IsTurnOfBlackPlayer; + this.DoPassTurn(); } + public void PassTurn() + { + this.DoPassTurn(); + } + + private void DoPassTurn() + { + this.IsTurnOfBlackPlayer = !this.IsTurnOfBlackPlayer; + } // state of the board private int[,] _cells;