private void SetFrame() { for (int topAndBottomRow = 0; topAndBottomRow < Game2DArray.GetLength(1); topAndBottomRow++) { Game2DArray[0, topAndBottomRow].CellValue = StringUtilities.FRAME_SYMBOL; Game2DArray[0, topAndBottomRow].IsHidden = false; Game2DArray[Game2DArray.GetLength(0) - 1, topAndBottomRow].CellValue = StringUtilities.FRAME_SYMBOL; Game2DArray[Game2DArray.GetLength(0) - 1, topAndBottomRow].IsHidden = false; } for (int leftAndRightColumn = 0; leftAndRightColumn < Game2DArray.GetLength(0); leftAndRightColumn++) { Game2DArray[leftAndRightColumn, 0].CellValue = StringUtilities.FRAME_SYMBOL; Game2DArray[leftAndRightColumn, 0].IsHidden = false; Game2DArray[leftAndRightColumn, Game2DArray.GetLength(1) - 1].CellValue = StringUtilities.FRAME_SYMBOL; Game2DArray[leftAndRightColumn, Game2DArray.GetLength(1) - 1].IsHidden = false; } }
private void SetMines() { int mineIndex = 0; Random rnd = new Random(); while (mineIndex < MinesCount) { int firstRndNum = rnd.Next(1, Game2DArray.GetLength(0) - 1); int secondRndNum = rnd.Next(1, Game2DArray.GetLength(1) - 1); if (Game2DArray[firstRndNum, secondRndNum].CellValue != StringUtilities.MINE_SYMBOL) { Game2DArray[firstRndNum, secondRndNum].CellValue = StringUtilities.MINE_SYMBOL; //Increases the cells value around the current mine. for (int aroundMineI = firstRndNum - 1; aroundMineI <= firstRndNum + 1; aroundMineI++) { for (int aroundMineJ = secondRndNum - 1; aroundMineJ <= secondRndNum + 1; aroundMineJ++) { Game2DArray[aroundMineI, aroundMineJ].MinesAround++; } } mineIndex++; } } //Gives a value for each cell on the 2D array - excluding the mines! //Also set all cell's isHidden as true. for (int i = 1; i < Game2DArray.GetLength(0) - 1; i++) { for (int j = 1; j < Game2DArray.GetLength(1) - 1; j++) { //Checks if the current cell isn't a mine. if (Game2DArray[i, j].CellValue != StringUtilities.MINE_SYMBOL) { //?: Operator - condition ? first_expression : second_expression; Game2DArray[i, j].CellValue = Game2DArray[i, j].MinesAround == 0 ? StringUtilities.EMPTY_CELL_SYMBOL : (char)Game2DArray[i, j].MinesAround; } Game2DArray[i, j].IsHidden = true; Game2DArray[i, j].IsMarked = false; } } }
public void Print() { int iTop = 5, temp; for (int i = 0; i < Game2DArray.GetLength(0); i++) { temp = CursorHandler.CursorOffSet; for (int j = 0; j < Game2DArray.GetLength(1); j++) { if (!Game2DArray[i, j].IsHidden) { Console.SetCursorPosition(temp, iTop); Console.ForegroundColor = ConsoleColor.DarkCyan; Console.Write(Game2DArray[i, j].CellValue + " "); Console.ForegroundColor = ConsoleColor.White; } temp += 2; } iTop++; Console.WriteLine(); } }