Skip to content

Commit

Permalink
Added "pass turn" and "game over"
Browse files Browse the repository at this point in the history
  • Loading branch information
dodikk committed Dec 18, 2015
1 parent 1af6396 commit c05e5f3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
29 changes: 27 additions & 2 deletions Reversi/Assets/BoardEventsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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" :
Expand Down Expand Up @@ -392,4 +416,5 @@ private void populateLabels()
private const string BALL_TAG = "Ball";

private IEnumerable<IReversiTurn> _validTurns ;
bool _isGameOver;
}
2 changes: 2 additions & 0 deletions Reversi/Assets/ReversiKit/IBoardState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public interface IBoardState

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


void ApplyTurn(IReversiTurn turn);
void PassTurn();
}
}

19 changes: 18 additions & 1 deletion Reversi/Assets/ReversiKit/MatrixBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c05e5f3

Please sign in to comment.