public void SetProperHighScoreName() { var playField = new Playfield(); Assert.AreEqual(playField.Height, Playfield.InitialHeight); Assert.AreEqual(playField.Width, Playfield.InitialWidth); }
/// <summary> /// refreshes the playfield after balloons are popped /// </summary> /// <param name="matrix">the playfield of type Playfield</param> private void FallBalloons(Playfield matrix) { // This is a complex algorithm Stack<string> columnValues = new Stack<string>(); int rowsLenght = matrix.Height; int columnsLength = matrix.Width; for (int col = 0; col < columnsLength; col++) { for (int row = 0; row < rowsLenght; row++) { if (matrix.Field[row, col] != "0") { columnValues.Push(matrix.Field[row, col]); } } for (int row = rowsLenght - 1; row >= 0; row--) { try { matrix.Field[row, col] = columnValues.Pop(); } catch (Exception) { matrix.Field[row, col] = "0"; } } } }
/// <summary> /// counts how many balloons have been popped in the current move /// </summary> /// <param name="row">int for cell position</param> /// <param name="col">int for cell position</param> /// <param name="playfield">object that contains the playfield matrics form type Playfield</param> /// <param name="selectedCellValue">string for the user input for which balloon to be popped</param> /// <returns>int of count of popped ballons within the current move</returns> private int PopBaloons(int row, int col, Playfield playfield, string selectedCellValue = null) { int poppedBaloons = 0; bool isRowValid = row >= 0 && row < playfield.Height; bool isColValid = col >= 0 && col < playfield.Width; if (isRowValid && isColValid) { if (playfield.Field[row, col] == selectedCellValue) { // Pop current cell playfield.Field[row, col] = "0"; poppedBaloons += 1; // Up poppedBaloons += this.PopBaloons(row - 1, col, playfield, selectedCellValue); // Down poppedBaloons += this.PopBaloons(row + 1, col, playfield, selectedCellValue); // Left poppedBaloons += this.PopBaloons(row, col + 1, playfield, selectedCellValue); // Right poppedBaloons += this.PopBaloons(row, col - 1, playfield, selectedCellValue); } } return poppedBaloons; }
/// <summary> /// checks wether the matrix needs to be modified /// </summary> /// <param name="matrixToModify">object form type playfield that represents the matrix that hold sthe balloons</param> /// <param name="row">matrix size</param> /// <param name="column">matrix size</param> /// <returns>bool value</returns> public bool ChangeMatrix(Playfield matrixToModify, int row, int column) { if (matrixToModify.Field[row, column] == "0") { return true; } string searchedTarget = matrixToModify.Field[row, column]; matrixToModify.Field[row, column] = "0"; this.currentCell.CheckLeft(matrixToModify, row, column, searchedTarget); this.currentCell.CheckRight(matrixToModify, row, column, searchedTarget); this.currentCell.CheckUp(matrixToModify, row, column, searchedTarget); this.currentCell.CheckDown(matrixToModify, row, column, searchedTarget); return false; }
/// <summary> /// checks the cells to the right /// </summary> /// <param name="balloonsMatrix">the playfield for teh game</param> /// <param name="row">takes the playfield size</param> /// <param name="column">takes the playfield size</param> /// <param name="searchedItem">string with the cell that we check for</param> public void CheckRight(Playfield balloonsMatrix, int row, int column, string searchedItem) { int newRow = row; int newColumn = column + 1; try { if (balloonsMatrix.Field[newRow, newColumn] == searchedItem) { balloonsMatrix.Field[newRow, newColumn] = "0"; this.CheckRight(balloonsMatrix, newRow, newColumn, searchedItem); } else { return; } } catch (IndexOutOfRangeException) { return; } }
/// <summary> /// count of balloons /// </summary> /// <param name="row">int for cell position</param> /// <param name="col">int for cell position</param> /// <param name="playfield">object that contains the playfield matrics form type Playfield</param> /// <returns></returns> public int PopBaloons(int row, int col, Playfield playfield) { bool isRowValid = row >= 0 && row < playfield.Height; bool isColValid = col >= 0 && col < playfield.Width; if (isRowValid && isColValid) { string selectedCellValue = playfield.Field[row, col]; if (selectedCellValue == "0") { return 0; } else { return this.PopBaloons(row, col, playfield, selectedCellValue); } } return 0; }
/// <summary> /// counts how many balloons have been popped in the current move /// </summary> /// <param name="row">int for cell position</param> /// <param name="col">int for cell position</param> /// <param name="playfield">object that contains the playfield matrics form type Playfield</param> /// <param name="selectedCellValue">string for the user input for which balloon to be popped</param> /// <returns>int of count of popped ballons within the current move</returns> private int PopBaloons(int row, int col, Playfield playfield, string selectedCellValue = null) { int poppedBaloons = 0; bool isRowValid = row >= 0 && row < playfield.Height; bool isColValid = col >= 0 && col < playfield.Width; if (isRowValid && isColValid) { if (playfield.Field[row, col] == selectedCellValue) { this.playfieldMatrix.ChangeMatrix(playfield, row, col); poppedBaloons++; poppedBaloons += this.PopBaloons(row - 1, col, playfield, selectedCellValue); poppedBaloons += this.PopBaloons(row + 1, col, playfield, selectedCellValue); poppedBaloons += this.PopBaloons(row, col + 1, playfield, selectedCellValue); poppedBaloons += this.PopBaloons(row, col - 1, playfield, selectedCellValue); this.FallBalloons(playfield); } } return poppedBaloons; }