public static bool ValidateCoords(string input, out int row, out int col, MinefieldClass minefield) { var coords = input.Split(','); if (coords.Length != 2) { row = 0; col = 0; Console.WriteLine("\nInvalid Entry, Please Try Again!\n"); return(false); } bool x = int.TryParse(coords[0], out row); bool y = int.TryParse(coords[1], out col); row--; col--; if (row < 0 || col < 0 || row >= minefield.Rows || col >= minefield.Columns) { Console.WriteLine("\nInvalid Entry, Please Try Again!\n"); return(false); } return(x && y); }
public static void Play(MinefieldClass minefield) { minefield.GenerateMinefield(); minefield.DisplayBoard(); while (!minefield.GameOver && !Validation.WonGame(minefield)) { Console.ForegroundColor = ConsoleColor.Gray; int row, col; //user input for coordinants bool valid; do { Console.Write("\n Enter coordinates(ex: 0,0): "); valid = Validation.ValidateCoords(Console.ReadLine(), out row, out col, minefield); } while (!valid); Console.Clear(); minefield.CheckCell(row, col); Console.ResetColor(); //DisplayBoard has its own color scheme in the Grid class minefield.DisplayBoard(); } //Win/loss conditions if (minefield.GameOver) { Console.WriteLine(); minefield.DisplayHiddenBoard(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("\nYou hit a mine... GAME OVER!!"); Console.ResetColor(); } else { Console.WriteLine(); minefield.DisplayHiddenBoard(); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("\nYOU WINN!!!"); Console.ResetColor(); } }
public static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Welcome to Minefield!\n"); Console.ResetColor(); bool valid; //used for validation throughout main Levels level; do { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"Would you like to play " + $"\n(1){Levels.Beginner} - 9x9: 10 Bombs" + $"\n(2){Levels.Intermediate} - 16x16: 40 Bombs" + $"\n(3){Levels.Advanced} - 24x24: 99 Bombs " + $"\n(4){Levels.Custom}"); Console.ResetColor(); valid = Validation.ValidateLevel(Console.ReadLine().ToLower(), out level); } while (!valid); MinefieldClass minefield = null; switch (level) { case Levels.Beginner: minefield = new MinefieldClass(9, 9, 10); break; case Levels.Intermediate: minefield = new MinefieldClass(16, 16, 40); break; case Levels.Advanced: minefield = new MinefieldClass(24, 24, 99); break; case Levels.Custom: int row, col, bombs; do { Console.Write("\n Enter Rows, Columns, Bombs (ex. 0,0,0): "); valid = Validation.ValidateCustom(Console.ReadLine().Trim(), out row, out col, out bombs); } while (!valid); minefield = new MinefieldClass(row, col, bombs); break; } PlayGame.Play(minefield); }
public static bool WonGame(MinefieldClass minefieldClass) { var countsBombs = 0; for (int i = 0; i < minefieldClass.Rows; i++) { for (int j = 0; j < minefieldClass.Columns; j++) { if (!minefieldClass.Board[i, j].IsRevealed && !minefieldClass.Board[i, j].IsFlagged) { return(false); } else if (minefieldClass.Board[i, j].IsBomb && minefieldClass.Board[i, j].IsFlagged) { countsBombs++; } } } if (countsBombs == minefieldClass.Bombs) { return(true); } return(false); }