So i have my code all figured out and it does exactly what it needs to but i nee
ID: 3602146 • Letter: S
Question
So i have my code all figured out and it does exactly what it needs to but i need to add a loop in the main method to ask the user if they want to play again... if the first letter of their input is n then that means no and then the program ends. But if they say yes then the program has to start over again. Again this loop (i assume) has to go in the main method.
import java.util.Random;
import java.util.Scanner;
//FIXME class header comment
public class MineSweeper {
public static void main(String[] args) {
System.out.println("Welcome to Mine Sweeper!");
Scanner in = new Scanner(System.in);
String widthString = "What width of map would you like ";
String heightString = "What height of map would you like ";
int max = Config.MAX_SIZE;
int min = Config.MIN_SIZE;
int width = promptUser(in, widthString + "(" + min +" - " + max + "): ", min, max);
int height = promptUser(in, heightString + "(" + min +" - " + max + "): ", min, max);
char [][] map = new char[height][width];
eraseMap(map);
simplePrintMap(map);
String rowPrompt = "row: ";
String columnPrompt = "column: ";
int row = promptUser(in, rowPrompt, 1, height);
int column = promptUser(in, columnPrompt, 1, width);
map[row-1][column-1]= Config.NO_NEARBY_MINE;
simplePrintMap(map);
*NEED HELP HERE MAKING PLAY AGAIN LOOP
}
public static int promptUser(Scanner in, String prompt, int min, int max) {
int userInput = -99;
System.out.print(prompt);
while(1==1)
if(in.hasNextInt()) {
userInput=in.nextInt();
in.nextLine();
if(userInput>=min && userInput<=max) {
break;
}
System.out.println("Expected a number from "+min+" to "
+max+".");
}else {
System.out.println("Expected a number from "+min+" to "+max+
".");
in.next();
in.nextLine();
}
return userInput;
}
public static void eraseMap(char[][] map) {
for (int i = 0; i < map.length; ++i) {
for (int j = 0; j < map[i].length; ++j) {
map[i][j] = Config.UNSWEPT;
}
}
}
public static void simplePrintMap(char[][] map) {
for(int i=0;i< map.length; ++i) {
for(int j=0; j< map[i].length; ++j) {
System.out.print(" " + map[i][j]);
}
System.out.println();
}
}
Explanation / Answer
Here is the update version of the program. Please find the sample output.
Sample Output:
$> java MineSweeper
Welcome to Mine Sweeper!
What width of map would you like (1 - 5): 3
What height of map would you like (1 - 5): 4
f f f
f f f
f f f
f f f
row: 3
column: 3
f f f
f f f
f f d
f f f
Do you want to play again? Continue(Y/N) : y
What width of map would you like (1 - 5):
Code Snippet :
import java.util.Random;
import java.util.Scanner;
//FIXME class header comment
public class MineSweeper {
public static void main(String[] args) {
char restart ;
System.out.println("Welcome to Mine Sweeper!");
Scanner in = new Scanner(System.in);
String widthString = "What width of map would you like ";
String heightString = "What height of map would you like ";
int max = Config.MAX_SIZE;
int min = Config.MIN_SIZE;
do {
int width = promptUser(in, widthString + "(" + min +" - " + max + "): ", min, max);
int height = promptUser(in, heightString + "(" + min +" - " + max + "): ", min, max);
char [][] map = new char[height][width];
eraseMap(map);
simplePrintMap(map);
String rowPrompt = "row: ";
String columnPrompt = "column: ";
int row = promptUser(in, rowPrompt, 1, height);
int column = promptUser(in, columnPrompt, 1, width);
map[row-1][column-1]= Config.NO_NEARBY_MINE;
simplePrintMap(map);
// *NEED HELP HERE MAKING PLAY AGAIN LOOP
System.out.print("Do you want to play again? Continue(Y/N) : ");
restart=in.next().charAt(0);
} while ( restart=='Y'|| restart=='y' );
}
public static int promptUser(Scanner in, String prompt, int min, int max) {
int userInput = -99;
System.out.print(prompt);
while(1==1)
if(in.hasNextInt()) {
userInput=in.nextInt();
in.nextLine();
if(userInput>=min && userInput<=max) {
break;
}
System.out.println("Expected a number from "+min+" to "
+max+".");
}else {
System.out.println("Expected a number from "+min+" to "+max+
".");
in.next();
in.nextLine();
}
return userInput;
}
public static void eraseMap(char[][] map) {
for (int i = 0; i < map.length; ++i) {
for (int j = 0; j < map[i].length; ++j) {
map[i][j] = Config.UNSWEPT;
}
}
}
public static void simplePrintMap(char[][] map) {
for(int i=0;i< map.length; ++i) {
for(int j=0; j< map[i].length; ++j) {
System.out.print(" " + map[i][j]);
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.