Skip to content

Commit

Permalink
Added score indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
dodikk committed Dec 18, 2015
1 parent 14a9319 commit 1af6396
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 22 deletions.
74 changes: 55 additions & 19 deletions Reversi/Assets/BoardEventsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -39,6 +38,7 @@ void Start ()
void Update()
{
this.updateTurnLabel();
this.updateScoreLabels();
this.updateBallColours();
this.highlightAvailableTurns();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -321,10 +345,22 @@ private void populateCellColours()
//
// this._highlightedCellMaterial = Resources.Load("HighlightedCell.mat", typeof(Material)) as Material;
}

private void populateLabels()
{
var labels = FindObjectsOfType<Text>();


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;
Expand Down
4 changes: 2 additions & 2 deletions Reversi/Assets/BoardEventsHandler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Reversi/Assets/ChessBoard.unity
Binary file not shown.
4 changes: 4 additions & 0 deletions Reversi/Assets/ReversiKit/IBoardState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public interface IBoardState
IEnumerable<ICellCoordinates> GetNeighboursForCell(ICellCoordinates position);
IEnumerable<ICellCoordinates> GetEmptyEnemyNeighbours();

int NumberOfBlackPieces { get; }
int NumberOfWhitePieces { get; }


void ApplyTurn(IReversiTurn turn);
}
}
Expand Down
17 changes: 17 additions & 0 deletions Reversi/Assets/ReversiKit/MatrixBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ public IEnumerable<ICellCoordinates> 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
Expand Down
Binary file modified Reversi/Assets/TurnLabel.prefab
Binary file not shown.
2 changes: 1 addition & 1 deletion Reversi/Assets/TurnLabel.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Reversi/Library/InspectorExpandedItems.asset
Binary file not shown.

0 comments on commit 1af6396

Please sign in to comment.