public virtual BoardStatus? CheckForWinner(IBoardCells boardCells) { foreach (var combination in _winningCombinations) { var firstCell = boardCells.GetCell(combination[0]); //if one cell of the combination is empty, it can't be part of a winning combination - move on if (firstCell == PieceType.Neither) continue; //if all three cells have the same non-empty value, we have a winner if ( boardCells.GetCell(combination[1]) == firstCell && boardCells.GetCell(combination[2]) == firstCell ) { return firstCell == PieceType.Nought ? BoardStatus.NoughtsWins : BoardStatus.CrossesWins; } } //no winning combination found return null; }
public virtual BoardStatus?CheckForWinner(IBoardCells boardCells) { foreach (var combination in _winningCombinations) { var firstCell = boardCells.GetCell(combination[0]); //if one cell of the combination is empty, it can't be part of a winning combination - move on if (firstCell == PieceType.Neither) { continue; } //if all three cells have the same non-empty value, we have a winner if ( boardCells.GetCell(combination[1]) == firstCell && boardCells.GetCell(combination[2]) == firstCell ) { return(firstCell == PieceType.Nought ? BoardStatus.NoughtsWins : BoardStatus.CrossesWins); } } //no winning combination found return(null); }
/// <summary> /// Allow the placing of a piece on the board, subject to certain conditions. It then uses /// both IBoardWinningRules and IBoardCells to work out the current state of the board /// </summary> /// <param name="piece">Type of piece to place on board - Nought or Cross</param> /// <returns>State of the board after applyin the piece - either having a winner (and which one), full or still playable</returns> public BoardStatus AssignPieceToBoard(PieceType piece) { if (_boardCells.NoSpacesLeft()) { throw new Exception("You can't assign a piece to a full board"); } int randomIndex; while (true) { randomIndex = _random.Next(_boardSize); if (_boardCells.GetCell(randomIndex) == PieceType.Neither) { break; } } _boardCells.SetCell(randomIndex, piece); var winner = _boardWinningRules.CheckForWinner(_boardCells); if (winner.HasValue) { return(winner.Value); } if (_boardCells.NoSpacesLeft()) { return(BoardStatus.Full); } return(BoardStatus.StillPlayable); }
public string RenderAsText(IBoardCells boardCells) { Func <int, char> cell = x => _renderLookup[boardCells.GetCell(x)]; return(string.Format( " {0} | {1} | {2} \n" + "-----------\n" + " {3} | {4} | {5} \n" + "-----------\n" + " {6} | {7} | {8} \n", cell(0), cell(1), cell(2), cell(3), cell(4), cell(5), cell(6), cell(7), cell(8) )); }
public string RenderAsText(IBoardCells boardCells) { Func<int, char> cell = x => _renderLookup[boardCells.GetCell(x)]; return string.Format( " {0} | {1} | {2} \n" + "-----------\n" + " {3} | {4} | {5} \n" + "-----------\n" + " {6} | {7} | {8} \n", cell(0), cell(1), cell(2), cell(3), cell(4), cell(5), cell(6), cell(7), cell(8) ); }