I am supposed to create a Java program like this: (Note: the bolded 4, 6, 2, 2,
ID: 3599601 • Letter: I
Question
I am supposed to create a Java program like this:
(Note: the bolded 4, 6, 2, 2, and n are inputs)
I also have to implement the method bodies for the following methods keeping these exact method headers :
public static int promptUser(Scanner in, String prompt, int min, int max)
public static void eraseMap(char[][] map)
public static void simplePrintMap(char[][] map)
Lastly, here's an algorithm for the main method is:
Show welcome message Begin new game loop
Call promptUser to get the width and height of the map. Create a 2 dimensional array of char for the map.
Call eraseMap to initialize the map.
Call simplePrintMap to print out the blank map.
Call promptUser for the location (row & column) to sweep.
Mark that location on the map with Config.NO_NEARBY_MINE.
Call simplePrintMap again, showing the visited location.
Continue new game loop while the user wants to keep playing.
When done playing, show the Thank you message.
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class Game{
public static int promptUser(Scanner in, String prompt, int min, int max){
System.out.print(prompt);
int inp = in.nextInt();
if(inp>max && inp<min)
promptUser(in, prompt, min, max);
else
return inp;
return inp;
}
public static void eraseMap(char[][] map){
int i,j,row=map.length,col=map[0].length;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
map[i][j]='.';
}
public static void simplePrintMap(char[][] map){
System.out.println();
for(char[] row : map){
for(char val : row)
System.out.print(val + " ");
System.out.println();
}
}
public static void main(String[] args){
char resp;
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Mine Sweeper!");
do{
int width = promptUser(in, "What width of map would you like (3 - 20): ", 3, 20);
int height = promptUser(in, "What height of map would you like (3 - 20): ", 3, 20);
char map[][] = new char[height][width];
eraseMap(map);
simplePrintMap(map);
int row = promptUser(in, "row: ", 1, height);
int column = promptUser(in, "column: ", 1, width);
//I don't know what do you mean by Config.NO_NEARBY_MINE, there fore I am just putting a null value there
map[row-1][column-1] = '';
simplePrintMap(map);
System.out.println("Would you like to play again (y/n)?");
resp = in.next().charAt(0);
}while(resp == 'y');
System.out.println("Thank you for playing Mine Sweeper!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.