diff --git a/Reversi/Assets/BoardEventsHandler.cs b/Reversi/Assets/BoardEventsHandler.cs index ce2c021..8a24770 100644 --- a/Reversi/Assets/BoardEventsHandler.cs +++ b/Reversi/Assets/BoardEventsHandler.cs @@ -6,29 +6,28 @@ using System.Linq; using ReversiKit; using System.Collections.Generic; +using UnityEngine.UI; +using System; public class BoardEventsHandler : MonoBehaviour { #region MonoBehaviour override // Use this for initialization - void Start () + void Start() { this._mutableBoardModel = new MatrixBoard(); this._turnCalculator = new TurnCalculator(); this._boardModel = this._mutableBoardModel; -// UnityEngine.Debug.developerConsoleVisible = true; -// UnityEngine.Debug.LogError("----Start-----"); -// UnityEngine.Debug.LogError ("Turn label : " + this._turnLabel.ToString()); - - this._root = GameObject.Find("root"); -// UnityEngine.Debug.LogError ("Root : " + root.ToString ()); + // Using lowercase methods in this class // to distinguish own methods from built-in unity methods - populateBallsList(); + + populateLabels(); + populateBallsList(); populateCellsList (); populateCellsMatrix (); populateCellColours(); @@ -39,6 +38,7 @@ void Start () void Update() { this.updateTurnLabel(); + this.updateScoreLabels(); this.updateBallColours(); this.highlightAvailableTurns(); @@ -76,18 +76,28 @@ private void handleTapOnCell(GameObject cellCube) // TODO : maybe compute matrix index by reference string cellName = cellCube.name; - // TODO : extract query to ReversiKit - IReversiTurn turn = this._validTurns.Where(t => - { - string turnPositionName = BoardCoordinatesConverter.CoordinatesToCellName(t.Position); - return cellName.Equals(turnPositionName); - }).First(); + if (null == this._validTurns || 0 == this._validTurns.Count()) + { + // TODO : Pass turn + // Or GameOver + + return; + } - if (null != turn) - { - this.makeTurn(turn); - } + var turnsSetToMatchInput = this._validTurns.Where(t => + { + string turnPositionName = BoardCoordinatesConverter.CoordinatesToCellName(t.Position); + return cellName.Equals(turnPositionName); + }); + + if (null == turnsSetToMatchInput || 0 == turnsSetToMatchInput.Count()) + { + // TODO : maybe show alert + return; + } + IReversiTurn turn = turnsSetToMatchInput.First(); + this.makeTurn(turn); } private void makeTurn(IReversiTurn turn) @@ -171,6 +181,15 @@ private void updateBallColours() } } + private void updateScoreLabels() + { + int blackScore = this._boardModel.NumberOfBlackPieces; + int whiteScore = this._boardModel.NumberOfWhitePieces; + + this._blackScoreLabel.text = "Black : " + Convert.ToString(blackScore); + this._whiteScoreLabel.text = "White : " + Convert.ToString(whiteScore); + } + #region Turn Highlight private void unhighlightAvailableTurns() { @@ -185,6 +204,11 @@ private void highlightAvailableTurns() private void highlightAvailableTurns(bool shouldHighlight) { var turns = this._validTurns; + if (null == turns) + { + // win condition will be checked in user input processing code + return; + } foreach (IReversiTurn singleTurn in turns) { @@ -321,10 +345,22 @@ private void populateCellColours() // // this._highlightedCellMaterial = Resources.Load("HighlightedCell.mat", typeof(Material)) as Material; } + + private void populateLabels() + { + var labels = FindObjectsOfType(); + + + this._turnLabel = labels.Where(l => "TurnLabel" == l.name).First(); + this._blackScoreLabel = labels.Where(l => "BlackScore" == l.name).First(); + this._whiteScoreLabel = labels.Where(l => "WhiteScore" == l.name).First(); + } #endregion #region GUI elements - public TextMesh _turnLabel; + public Text _turnLabel; + public Text _blackScoreLabel; + public Text _whiteScoreLabel; private GameObject _root; private GameObject[] _cellsList; diff --git a/Reversi/Assets/BoardEventsHandler.cs.meta b/Reversi/Assets/BoardEventsHandler.cs.meta index 146c758..3275247 100644 --- a/Reversi/Assets/BoardEventsHandler.cs.meta +++ b/Reversi/Assets/BoardEventsHandler.cs.meta @@ -1,11 +1,11 @@ fileFormatVersion: 2 guid: 675ce738300944c6aad497634f9164a2 -timeCreated: 1450469833 +timeCreated: 1450476965 licenseType: Free MonoImporter: serializedVersion: 2 defaultReferences: - - _turnLabel: {fileID: 10265206, guid: 6b93290400504447b832a68e5183fab7, type: 2} + - _turnLabel: {fileID: 11440168, guid: 6b93290400504447b832a68e5183fab7, type: 2} - _whiteItemMaterial: {fileID: 2100000, guid: 160df9e56774841b882f6879da069522, type: 2} - _blackItemMaterial: {fileID: 2100000, guid: 7fe0c9aa683e94b769db9900817d863a, diff --git a/Reversi/Assets/ChessBoard.unity b/Reversi/Assets/ChessBoard.unity index 0cc6aa6..0a690ad 100644 Binary files a/Reversi/Assets/ChessBoard.unity and b/Reversi/Assets/ChessBoard.unity differ diff --git a/Reversi/Assets/ReversiKit/IBoardState.cs b/Reversi/Assets/ReversiKit/IBoardState.cs index fab6927..99a9d4f 100644 --- a/Reversi/Assets/ReversiKit/IBoardState.cs +++ b/Reversi/Assets/ReversiKit/IBoardState.cs @@ -17,6 +17,10 @@ public interface IBoardState IEnumerable GetNeighboursForCell(ICellCoordinates position); IEnumerable GetEmptyEnemyNeighbours(); + int NumberOfBlackPieces { get; } + int NumberOfWhitePieces { get; } + + void ApplyTurn(IReversiTurn turn); } } diff --git a/Reversi/Assets/ReversiKit/MatrixBoard.cs b/Reversi/Assets/ReversiKit/MatrixBoard.cs index 6230e10..e094268 100644 --- a/Reversi/Assets/ReversiKit/MatrixBoard.cs +++ b/Reversi/Assets/ReversiKit/MatrixBoard.cs @@ -184,6 +184,23 @@ public IEnumerable GetNeighboursForCell(ICellCoordinates posit return result; } + + + public int NumberOfBlackPieces + { + get + { + return this._flattenCells.Count(c => this.IsCellTakenByBlack(c)); + } + } + + public int NumberOfWhitePieces + { + get + { + return this._flattenCells.Count(c => this.IsCellTakenByWhite(c)); + } + } #endregion #region Mutable diff --git a/Reversi/Assets/TurnLabel.prefab b/Reversi/Assets/TurnLabel.prefab index 2a139ef..22b1535 100644 Binary files a/Reversi/Assets/TurnLabel.prefab and b/Reversi/Assets/TurnLabel.prefab differ diff --git a/Reversi/Assets/TurnLabel.prefab.meta b/Reversi/Assets/TurnLabel.prefab.meta index bdfa470..3d16230 100644 --- a/Reversi/Assets/TurnLabel.prefab.meta +++ b/Reversi/Assets/TurnLabel.prefab.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 guid: 6b93290400504447b832a68e5183fab7 -timeCreated: 1450202982 +timeCreated: 1450476958 licenseType: Free NativeFormatImporter: userData: diff --git a/Reversi/Library/InspectorExpandedItems.asset b/Reversi/Library/InspectorExpandedItems.asset index 0fe0d6e..2e9c07c 100644 Binary files a/Reversi/Library/InspectorExpandedItems.asset and b/Reversi/Library/InspectorExpandedItems.asset differ