private void highlightAvailableTurns(bool shouldHighlight) { var turns = this._validTurns; if (null == turns) { return; } foreach (IReversiTurn singleTurn in turns) { ICellCoordinates turnCell = singleTurn.Position; GameObject cellCube = this._cellsMatrix[turnCell.Row, turnCell.Column]; Material cellColour = null; if (shouldHighlight) { cellColour = this._highlightedCellMaterial; } else { cellColour = turnCell.IsBlack ? this._blackCellMaterial : this._whiteCellMaterial; } cellCube.GetComponent <Renderer>().material = cellColour; } }
public bool IsCellTakenByWhite(ICellCoordinates position) { #if NO_UNITY Check.If(position.Row).IsBetween(0, BOARD_MAX_INDEX); Check.If(position.Column).IsBetween(0, BOARD_MAX_INDEX); #endif return(TAKEN_BY_WHITE == this._cells[position.Row, position.Column]); }
public IEnumerable <ICellCoordinates> GetNeighboursForCell(ICellCoordinates position) { var result = new List <ICellCoordinates>(); if (0 != position.Row) { if (0 != position.Column) { result.Add(new CellCoordinates(position.Row - 1, position.Column - 1)); } result.Add(new CellCoordinates(position.Row - 1, position.Column)); if (position.Column < BOARD_MAX_INDEX) { result.Add(new CellCoordinates(position.Row - 1, position.Column + 1)); } } { if (0 != position.Column) { result.Add(new CellCoordinates(position.Row, position.Column - 1)); } if (position.Column < BOARD_MAX_INDEX) { result.Add(new CellCoordinates(position.Row, position.Column + 1)); } } if (position.Row < BOARD_MAX_INDEX) { if (0 != position.Column) { result.Add(new CellCoordinates(position.Row + 1, position.Column - 1)); } result.Add(new CellCoordinates(position.Row + 1, position.Column)); if (position.Column < BOARD_MAX_INDEX) { result.Add(new CellCoordinates(position.Row + 1, position.Column + 1)); } } #if NO_UNITY foreach (ICellCoordinates c in result) { Check.If(c.Row).IsBetween(0, BOARD_MAX_INDEX); Check.If(c.Column).IsBetween(0, BOARD_MAX_INDEX); } #endif return(result); }
public bool IsCellFree(ICellCoordinates position) { #if NO_UNITY Check.If(position.Row).IsBetween(0, BOARD_MAX_INDEX); Check.If(position.Column).IsBetween(0, BOARD_MAX_INDEX); #endif return(FREE_CELL == this._cells[position.Row, position.Column]); }
public bool IsValidPositionForTurnOnBoard( ICellCoordinates turnPosition, IBoardState board) { var matchingTurns = this._validTurns.Where(t => { return(t.Position.Equals(turnPosition)); }); return(0 != matchingTurns.Count()); }
public bool IsCellTakenByInactivePlayer(ICellCoordinates position) { if (this.IsTurnOfBlackPlayer) { return(this.IsCellTakenByWhite(position)); } else { return(this.IsCellTakenByBlack(position)); } }
public void TryConsumeCellByBlackPlayer(ICellCoordinates cellPosition, bool isBlackPlayer) { if (!IsCellFree(cellPosition)) { throw new ArgumentOutOfRangeException("cellPosition", cellPosition, "Cell already taken."); } #if NO_UNITY Check.If(cellPosition.Row).IsBetween(0, BOARD_MAX_INDEX); Check.If(cellPosition.Column).IsBetween(0, BOARD_MAX_INDEX); #endif this._cells [cellPosition.Row, cellPosition.Column] = isBlackPlayer ? TAKEN_BY_BLACK : TAKEN_BY_WHITE; }
public static string CoordinatesToCellName(ICellCoordinates cellPoint) { setupMappingIfNeeded(); #if NO_UNITY Check.If(cellPoint.Row).IsBetween(0, MatrixBoard.BOARD_SIZE - 1); Check.If(cellPoint.Column).IsBetween(0, MatrixBoard.BOARD_SIZE - 1); #endif char cColumn = indexToLetter[cellPoint.Column]; string cRow = (cellPoint.Row + 1).ToString(); string result = cColumn.ToString() + cRow.ToString(); return(result); }
private IEnumerable <ICellCoordinates> FlippedCellsForDirectionOfTurnOnBoard( ICellCoordinates direction, ICellCoordinates turnCandidate, IBoardState board) { var result = new List <ICellCoordinates>(); int rowIncrement = direction.Row - turnCandidate.Row; int columnIncrement = direction.Column - turnCandidate.Column; #if NO_UNITY Check.If(Math.Abs(rowIncrement)).IsBetween(0, 1); Check.If(Math.Abs(columnIncrement)).IsBetween(0, 1); #endif CellCoordinates current = new CellCoordinates(direction.Row, direction.Column); bool isMyColourFound = false; while (current.Row >= 0 && current.Row < MatrixBoard.BOARD_SIZE && current.Column >= 0 && current.Column < MatrixBoard.BOARD_SIZE) { if (board.IsCellTakenByInactivePlayer(current)) { var currentClone = current.Clone() as CellCoordinates; result.Add(currentClone); } else if (board.IsCellTakenByCurrentPlayer(current)) { isMyColourFound = true; break; } else // empty cell { break; } current.Row += rowIncrement; current.Column += columnIncrement; } if (!isMyColourFound) { return(null); } return(result); }
private IReversiTurn TurnForCellOnBoard( ICellCoordinates turnCandidate, IBoardState board) { var cellNeighbours = board.GetNeighboursForCell(turnCandidate); var directions = cellNeighbours.Where(n => { return(board.IsCellTakenByInactivePlayer(n)); }); var allFlippedCells = new List <ICellCoordinates>(); foreach (ICellCoordinates singleDirection in directions) { IEnumerable <ICellCoordinates> flippedCells = this.FlippedCellsForDirectionOfTurnOnBoard( singleDirection, turnCandidate, board); if (null != flippedCells) { allFlippedCells.AddRange(flippedCells); } } bool isNoDirectionFlippedEnemyItems = (0 == allFlippedCells.Count); if (isNoDirectionFlippedEnemyItems) { return(null); } var result = new ReversiTurnPOD(); { result.Position = turnCandidate; result.PositionsOfFlippedItems = allFlippedCells; } return(result); }
private void createBallWithColourAtCell(Material activePlayerColour, ICellCoordinates cell) { var cellPosition = this._cellsMatrix[cell.Row, cell.Column].transform.position; var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); { sphere.transform.position = new Vector3(cellPosition.x + 0.1f, cellPosition.y, cellPosition.z - 1); sphere.transform.localScale = new Vector3(0.8f, 0.8f, 0.8f); sphere.tag = BALL_TAG; var renderer = sphere.GetComponent <Renderer>(); renderer.shadowCastingMode = ShadowCastingMode.Off; renderer.receiveShadows = false; renderer.material = this._blackItemMaterial; } this._ballsMatrix[cell.Row, cell.Column] = sphere; }
private void handleTapOnCell(GameObject cellCube) { if (this.IsTurnOfAI) { return; } // TODO : maybe compute matrix index by reference string cellName = cellCube.name; ICellCoordinates selectedCellPosition = BoardCoordinatesConverter.CellNameToCoordinates(cellName); if (tryPassTurnOrGameOver()) { return; } var turnValidator = new SearchInSetTurnValidator(this._validTurns); bool isSelectedTurnValid = turnValidator.IsValidPositionForTurnOnBoard(selectedCellPosition, this._boardModel); if (!isSelectedTurnValid) { // TODO : maybe show alert return; } IReversiTurn turn = this._validTurns.Where(t => { string turnPositionName = BoardCoordinatesConverter.CoordinatesToCellName(t.Position); return(cellName.Equals(turnPositionName)); }).First(); this.makeTurn(turn); if (IS_OPPONENT_PLAYER_AI) { StartCoroutine("coroutineMakeTurnByAI"); } }
public void TestTurnsForInitialState() { var turns = this._sut.GetValidTurnsForBoard(this._initialBoard); Assert.IsNotNull(turns); var turnsCount = turns.Count(); Assert.AreEqual(4, turnsCount); IReversiTurn turn = null; ICellCoordinates position = null; ICellCoordinates flippedCell = null; { position = BoardCoordinatesConverter.CellNameToCoordinates("C5"); turn = turns.Where ( t => (t.Position.Row == position.Row && t.Position.Column == position.Column) ).First(); Assert.IsNotNull(turn); Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count()); flippedCell = turn.PositionsOfFlippedItems.First(); Assert.IsNotNull(flippedCell); string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell); Assert.AreEqual("D5", flippedCellName); } { position = BoardCoordinatesConverter.CellNameToCoordinates("D6"); turn = turns.Where ( t => (t.Position.Row == position.Row && t.Position.Column == position.Column) ).First(); Assert.IsNotNull(turn); Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count()); flippedCell = turn.PositionsOfFlippedItems.First(); Assert.IsNotNull(flippedCell); string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell); Assert.AreEqual("D5", flippedCellName); } { position = BoardCoordinatesConverter.CellNameToCoordinates("F4"); turn = turns.Where ( t => (t.Position.Row == position.Row && t.Position.Column == position.Column) ).First(); Assert.IsNotNull(turn); Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count()); flippedCell = turn.PositionsOfFlippedItems.First(); Assert.IsNotNull(flippedCell); string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell); Assert.AreEqual("E4", flippedCellName); } { position = BoardCoordinatesConverter.CellNameToCoordinates("E3"); turn = turns.Where ( t => (t.Position.Row == position.Row && t.Position.Column == position.Column) ).First(); Assert.IsNotNull(turn); Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count()); flippedCell = turn.PositionsOfFlippedItems.First(); Assert.IsNotNull(flippedCell); string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell); Assert.AreEqual("E4", flippedCellName); } }
private void setColourForBallAtCell(Material activePlayerColour, ICellCoordinates cell) { this._ballsMatrix[cell.Row, cell.Column].GetComponent <Renderer>().material = activePlayerColour; }
public HashSet <ICellCoordinates> cellNeighbours(ICellCoordinates cell) { throw new System.NotImplementedException(); }
public void TryConsumeNamedCellByBlackPlayer(string cellName, bool isBlackPlayer) { ICellCoordinates cellPosition = BoardCoordinatesConverter.CellNameToCoordinates(cellName); this.TryConsumeCellByBlackPlayer(cellPosition, isBlackPlayer); }
public void TryConsumeCellByWhitePlayer(ICellCoordinates cellPosition) { this.TryConsumeCellByBlackPlayer(cellPosition, false); }