// gets the firing shot from the user and validates if its a valid position on the grid public void GetFiringShot() { try { int loop = 0; do { // get firing position from the user Console.WriteLine(Environment.NewLine); Console.WriteLine("Enter Firing Position. Eg. B3 - B is the row name and 3 is the col number"); input = Console.ReadLine().ToString().ToUpper(); // check if the input is a valid position from the grid success = ui.ValidatePlacementInput(input); if (!success) { Console.WriteLine(Environment.NewLine); Console.WriteLine("Invalid Input."); } else { firingPosition = input; break; } loop++; } while (loop < rep); // repeat till user enters a valid position for rep times if (!success && loop == rep) { // exit application after maximum retrys Console.WriteLine(Environment.NewLine); Console.WriteLine("Maximum retry exceeded. Exiting... Start a new game."); Environment.Exit(1); } } catch { throw; } }
// gets the Start position to place the Battleship on board from the user and validates it // expects XY coordinates X should be between A-J and Y should be between 1-10 // retrys for 5 times if invalid input is provided public void GetStartPosition() { try { int loop = 0; int outloop = 0; do { do { // get the input from the user Console.WriteLine(Environment.NewLine); Console.WriteLine("Enter the Start Position of the Battleship. Eg. B3 - B is the row name and 3 is the col number"); input = Console.ReadLine().ToString().ToUpper(); // validates the input if its in correct format XY and also a valid position from the grid success = ui.ValidatePlacementInput(input); if (!success) { Console.WriteLine(Environment.NewLine); Console.WriteLine("Invalid Input."); } else { startPosition = input; break; } loop++; } while (loop < rep); // repeat in case invalid input if (!success && loop == rep) { // exits after maximum allowed retrys ea.ExitMaxRetry(); } // if the entered input is a valid position from the grid if (success) { // validate if the battleship can be placed at that position i.e. if it fits from there success = ui.ValidatePlacement(startPosition, placementType, battleshipLen); if (!success) { Console.WriteLine(Environment.NewLine); Console.WriteLine("Length is not sufficient at that position. Try again"); } else { break; } outloop++; } } while (outloop < rep); // repeat if its not fit to get a new position if (!success && outloop == rep) { // exits after maximum allowed retrys ea.ExitMaxRetry(); } } catch { throw; } }