public void StartTurn(Cell[,] currentBordData) { var possibleCells = ReversiRules.GetPossibleTurns(currentBordData, _firstPlayer); var cellValue = new Dictionary <Cell, int>(); foreach (var cell in possibleCells) { var value = 0; var rivalLines = ReversiRules.GetRivalLinesAroundCell(currentBordData, cell.x, cell.y, _firstPlayer); foreach (var rivalLine in rivalLines) { value += rivalLine.Count; } cellValue[cell] = value; } var optimalTurn = possibleCells.First(); foreach (var cell in cellValue.Keys) { if (cellValue[cell] > cellValue[optimalTurn]) { optimalTurn = cell; } } OnDoTurn?.Invoke(optimalTurn.x, optimalTurn.y); }
public void RenderToString_DrawInitialBoardWithHints_ExpectedStringForDrawingIsReturned() { var expectedRenderString = " A B C D E F G H \n" + " ┌─┬─┬─┬─┬─┬─┬─┬─┐\n" + " 0│ │ │ │ │ │ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 1│ │ │ │ │ │ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 2│ │ │ │+│ │ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 3│ │ │+│█│░│ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 4│ │ │ │░│█│+│ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 5│ │ │ │ │+│ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 6│ │ │ │ │ │ │ │ │\n" + " ├─┼─┼─┼─┼─┼─┼─┼─┤\n" + " 7│ │ │ │ │ │ │ │ │\n" + " └─┴─┴─┴─┴─┴─┴─┴─┘\n"; var player = Black; var gameBoard = new Board(8, 8); gameBoard.InitBoard(); var hintBoard = ReversiRules.HintPlayer(gameBoard, player); var actualDrawString = Graphics.RenderToString(gameBoard, hintBoard); Assert.AreEqual(expectedRenderString, actualDrawString); }
public static bool CanPutCell(Cell[,] board, int x, int y, bool firstPlayerTurn) { var possibleLines = ReversiRules.GetRivalLinesAroundCell(board, x, y, firstPlayerTurn); return(possibleLines.Find(line => line.Count > 0) != null); }
public void HintPlayer_DrawHintsInHintBoard_ExpectCorrectHintsAreDrawn(char currentPlayer, char[] startData, char[] expectedHintData) { var gameBoard = CreateBoard(startData); var expectedHintBoard = CreateBoard(expectedHintData); var actualHintBoard = ReversiRules.HintPlayer(gameBoard, currentPlayer); Assert.AreEqual(expectedHintBoard, actualHintBoard); }
public void IsValidMove_CheckIfSimpleMoveIsValid_ExpectCorrectBehaviourFromProvidedMoveResult(string moveInput, char currentPlayer, bool expectedMoveResult) { var move = InputHandler.ParseMove(moveInput); var gameBoard = CreateBoard(); var actualResult = ReversiRules.IsValidMove(gameBoard, move, currentPlayer); Assert.AreEqual(expectedMoveResult, actualResult); }
private bool CheckEndGame() { if (ReversiRules.BoardFull(_boardData.Cells)) { GameEvents.Instance.SendEndGame(); return(true); } return(false); }
public void HandlePlayerClick(int x, int y) { if (!ReversiRules.CanPutCell(_currentBordData, x, y, _firstPlayer)) { return; } OnDoTurn?.Invoke(x, y); PlayerInput.OnTouchBoard -= HandlePlayerClick; }
public void CreationReversiRules_CreateRversiRulesObject_CorrectStartBoardIsCreated() { var config = CreateDefaultConfig(); var expectedGameBoard = CreateBoard(); var reversi = new ReversiRules(config); var actualGameBoard = reversi.GameBoard; Assert.AreEqual(expectedGameBoard, actualGameBoard); }
public void CreationReversiRules_CreateRversiRulesObject_CorrectStartPlayerIsStarting() { var expectedCurrentPlayer = Black; var config = CreateDefaultConfig(); var reversi = new ReversiRules(config); var actualCurrentPlayer = reversi.CurrentPlayer; Assert.AreEqual(expectedCurrentPlayer, actualCurrentPlayer); }
public void LoadSaveGame_LoadGameLogFile_CorrectBoardAndCurrentPlayerIsRetrieved(string saveGame, char expectedCurrentPlayer, char[] expectedGameData) { var config = CreateDefaultConfig(); config.SaveGame = saveGame; var expectedGameBoard = CreateBoard(expectedGameData); var reversi = new ReversiRules(config); var actualGameBoard = reversi.GameBoard; var actualCurrentPlayer = reversi.CurrentPlayer; Assert.AreEqual(expectedCurrentPlayer, actualCurrentPlayer); Assert.AreEqual(expectedGameBoard, actualGameBoard); }
public void SetCellOwner(int x, int y, CellOwner cellOwner) { _cells[x, y] = new Cell(x, y, cellOwner); if (cellOwner != CellOwner.None) { var possibleLines = ReversiRules.GetRivalLinesAroundCell(_cells, x, y, cellOwner == CellOwner.First); foreach (var possibleLine in possibleLines) { foreach (var cell in possibleLine) { _cells[cell.x, cell.y] = new Cell(cell.x, cell.y, cellOwner); } } } OnUpdateBoard?.Invoke(Cells); }
public void MakeMove_CheckIfMoveTurnsPiecesProperly_CorrectNewBoardIsObtained(string stringMove, char currentPlayer, char[] startBoard, char[] expectedDataAfterMove) { var expectedBoardAfterMove = CreateBoard(expectedDataAfterMove); var config = CreateDefaultConfig(); var move = InputHandler.ParseMove(stringMove); var gameBoard = CreateBoard(startBoard); var reversi = new ReversiRules(config) { GameBoard = gameBoard }; reversi.MakeMove(move, currentPlayer); reversi.PlacePiece(move, currentPlayer); var actualBoardAfterMove = reversi.GameBoard; Assert.AreEqual(expectedBoardAfterMove, actualBoardAfterMove); }
private void HandleDoTurn(int x, int y) { if (_boardData.Cells[x, y].cellOwner == CellOwner.None) { _boardData.SetCellOwner(x, y, _firstPlayerTurn ? CellOwner.First : CellOwner.Second); } _first.OnDoTurn -= HandleDoTurn; _second.OnDoTurn -= HandleDoTurn; if (CheckEndGame()) { GameResultType = ReversiRules.GetGameResultType(_boardData.Cells); GameEvents.Instance.SendEndGame(); return; } if (ReversiRules.GetPossibleTurns(_boardData.Cells, !_firstPlayerTurn).Count > 0) { _firstPlayerTurn = !_firstPlayerTurn; } StartTurn(); }
public void IsValidMove_CheckIfComplexMoveIsValid_ExpectCorrectBehaviourFromProvidedMoveResult(string moveInput, char currentPlayer, bool expectedMoveResult) { var move = InputHandler.ParseMove(moveInput); var boardData = new[] { // A B C D E F G H ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 0 ' ', ' ', ' ', ' ', B, ' ', ' ', ' ', // 1 ' ', ' ', B, ' ', B, ' ', ' ', ' ', // 2 ' ', W, W, W, B, ' ', ' ', ' ', // 3 ' ', ' ', ' ', B, W, ' ', ' ', ' ', // 4 ' ', ' ', W, ' ', ' ', W, ' ', ' ', // 5 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 6 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // 7 }; var gameBoard = new Board(8, 8) { Data = boardData }; var actualResult = ReversiRules.IsValidMove(gameBoard, move, currentPlayer); Assert.AreEqual(expectedMoveResult, actualResult); }