public static int GetValidInteger() { int input; while (!int.TryParse(Console.ReadLine(), out input)) //While user input is unable to be parsed into an integer, display error. { Program.PrintColoredString("Invalid input. Try again: ", ConsoleColor.DarkRed); } return(input); }
public static int GetIntegerInRange(int min, int max) { int input = GetValidInteger(); //Gets a valid integer from user input while (input < min || input > max) //While input is less than the min or greater than the max, display an error. { Program.PrintColoredString($"Input not between {min} and {max}. Try again: ", ConsoleColor.DarkRed); input = GetValidInteger(); //Get another valid integer from the user if input is not in range. } return(input); }
public void WriteBoard() { int selectableCells = Horizontal * Vertical; //Goes through y values top to bottom for (int y = 0; y < Vertical; y++) { WriteYCoordinate(y); //Goes through x values left to right for (int x = 0; x < Horizontal; x++) { Cell cell = CellArray[y, x]; if (cell.IsFlagged) { Program.PrintColoredString("F", ConsoleColor.Green); } else if (!cell.IsSelected) { Program.PrintColoredString("#", ConsoleColor.DarkGray); } else if (cell.IsMine) { Program.PrintColoredString("X", ConsoleColor.Red); State = GameState.MineSelected; } else { WriteColoredSurroundingMinesValue(cell.SurroundingMinesValue); selectableCells--; } if (TwoDigitXAxis) //If TwoDigitXAxis is true add extra space to allow room for x coodinates { Console.Write(" "); } else { Console.Write(" "); } } Console.WriteLine(); } WriteXCoordinates(); //Checks if the game has been won if (selectableCells == TotalMines && State != GameState.MineSelected) { State = GameState.GameWon; } }
public static InputCoordinates GetValidCoordinates(int vertical, int horizontal) { while (true) { string input = Console.ReadLine().Trim().ToLower(); string[] values; //Regex validates input is a number between 0 - 30, slash, number between 0 - 30, space, s or f character if (!Regex.IsMatch(input, @"([0-9]|[1-2][0-9]|30)\/([0-9]|[1-2][0-9]|30)\s[sf]")) { Program.PrintColoredString("Invalid input. Try again: ", ConsoleColor.DarkRed); continue; } values = input.Split(new char[] { '/', ' ' }); if (!int.TryParse(values[0], out int x)) { Program.PrintColoredString("Invalid X input. Try again: ", ConsoleColor.DarkRed); continue; } if (!int.TryParse(values[1], out int y)) { Program.PrintColoredString("Invalid Y input. Try again: ", ConsoleColor.DarkRed); continue; } if (x < 1 || x > horizontal) { Program.PrintColoredString($"X input is not between 1 and {horizontal}. Try again: ", ConsoleColor.DarkRed); continue; } if (y < 1 || y > vertical) { Program.PrintColoredString($"Y input is not between 1 and {vertical}. Try again: ", ConsoleColor.DarkRed); continue; } if (!Enum.TryParse(values[2].ToUpper(), out SelectOrFlag option)) { Program.PrintColoredString($"Action is not S or F. Try again: ", ConsoleColor.DarkRed); continue; } return(new InputCoordinates(y, x, option)); } }
private void WriteColoredSurroundingMinesValue(int number) { switch (number) { case 0: Console.Write(" "); break; case 1: Program.PrintColoredString("1", ConsoleColor.Blue); break; case 2: Program.PrintColoredString("2", ConsoleColor.Green); break; case 3: Program.PrintColoredString("3", ConsoleColor.Red); break; case 4: Program.PrintColoredString("4", ConsoleColor.DarkBlue); break; case 5: Program.PrintColoredString("5", ConsoleColor.DarkRed); break; case 6: Program.PrintColoredString("6", ConsoleColor.DarkCyan); break; case 7: Program.PrintColoredString("7", ConsoleColor.Black); break; case 8: Program.PrintColoredString("8", ConsoleColor.DarkGray); break; default: break; } }
public static string GetValidString(string[] options) { string input; string listOfOptions = string.Join(", ", options); //Creates a string of all elements in the Options array seperated by a comma. while (true) { input = Console.ReadLine().Trim().ToLower(); //Gets a lowercase input from user. foreach (string item in options) //Checks if user input equals any string in the Options array. { if (input == item) //If user input equals an option, return the input. { return(input); } } //If user input does not equal any of the options, write a list of options to choose from and have them try again. Program.PrintColoredString($"Input must be either of the following: [{listOfOptions}]. Try again: ", ConsoleColor.DarkRed); } }