/// <summary> /// Controls the gameplay throughout other methods for validation and I/O. /// </summary> public void Play() { IScoreBoard scoreBoard = new ScoreBoard(); IField gameField = new Field(); IIOManager ioManager = new ConsoleIOManager(); int row = 0; int col = 0; int minesCounter = 0; int revealedCellsCounter = 0; bool hasExploded = false; while (true) { ioManager.PrintInitialMessage(); StartNewGame(gameField, ref row, ref col, ref minesCounter, ref revealedCellsCounter, ref hasExploded); ioManager.PrintGameField(gameField, hasExploded); bool rowAndColAreValid = false; string input = string.Empty; while (true) { input = ioManager.GetUserInput(); if (IsMoveEntered(input)) { string[] inputParams = input.Split(); row = int.Parse(inputParams[0]); col = int.Parse(inputParams[1]); rowAndColAreValid = ValidateRowAndCol(gameField, row, col); if (rowAndColAreValid) { bool cellHasMine = gameField.IsMine(row, col); if (cellHasMine) { hasExploded = true; ioManager.PrintGameField(gameField, hasExploded); ioManager.PrintExplosionMessage(revealedCellsCounter); string currentPlayerName = ioManager.GetUserNickname(); scoreBoard.AddPlayer(currentPlayerName, revealedCellsCounter); ioManager.PrintScoreBoard(scoreBoard); break; } bool winner = IsWinner(gameField.Matrix, minesCounter); if (winner) { ioManager.PrintWinnerMessage(); string currentPlayerName = ioManager.GetUserNickname(); scoreBoard.AddPlayer(currentPlayerName, revealedCellsCounter); break; } gameField.Update(row, col); revealedCellsCounter++; ioManager.PrintGameField(gameField, cellHasMine); } else { ioManager.PrintInvalidCommandMessage(); } } else if (IsCommandEntered(input)) { bool isRestart = false; switch (input) { case "top": { ioManager.PrintScoreBoard(scoreBoard); continue; } case "exit": { ioManager.PrintQuitMessage(); return; } case "restart": { isRestart = true; break; } } if (isRestart) { break; } } else { ioManager.PrintInvalidCommandMessage(); } } } }
public void TestIsMine_InvalidRowLessThanMin() { int cellRow = -1; int cellCol = 5; Field gameField = new Field(); gameField.FillWithRandomMines(); bool isMine = gameField.IsMine(cellRow, cellCol); }
public void TestIsMine_InvalidRowAndCol() { int cellRow = 13; int cellCol = -2; Field gameField = new Field(); gameField.FillWithRandomMines(); bool isMine = gameField.IsMine(cellRow, cellCol); }
public void TestIsMine_InvalidRowGreaterThanMax() { int cellRow = 7; int cellCol = 5; Field gameField = new Field(); gameField.FillWithRandomMines(); bool isMine = gameField.IsMine(cellRow, cellCol); }
public void TestIsMine_CellHasMine() { Random fixedRnd = new Random(5); int cellRow = fixedRnd.Next(0, 5); int cellCol = fixedRnd.Next(0, 10); Field gameField = new Field(); Type fieldType = typeof(Field); fixedRnd = new Random(5); var fieldRandom = fieldType.GetField("random", BindingFlags.Instance | BindingFlags.NonPublic); fieldRandom.SetValue(gameField, fixedRnd); gameField.FillWithRandomMines(); bool isMine = gameField.IsMine(cellRow, cellCol); Assert.AreEqual(true, isMine); }