In: Accounting
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”. This will be used in our game of minesweeper to represent the possible locations the player can check.
The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we need to randomly replace five (5) "S”s with five (5) bombs represented by the capital letter “B” in this second array.
Write a recursive method (DO NOT USE A LOOP) that accepts both 2D arrays and an integer that represents what round it is, and another integer for the score. The method will work as described below.
Print out the first 2D array, our visible field, and allow the user to enter a viable position to check in that field. If they do not enter a viable position, please ask them to enter another position.
After getting a position, check that position in our second 2D array (our hidden field) and see if there is a bomb there. If no bomb is there, the player gets 1 point and that spot on the visible field becomes an “S” and the visible field is printed out again along with the updated round number and another position is taken from the user.
If there is a bomb there, print the hidden field along then print a message saying how many points the player got so far and that they stepped on a bomb in the final round number. This ends the game (a good spot for your base case) and no more user input should be accepted.
import java.util.Random;
import java.util.Scanner;
class Main {
static Scanner sc=new Scanner(System.in);
static Random random=new Random();
static int x;
static int y;
public static void main(String[] args) {
String[][] visibleArray=new String[5][5];
String[][] hiddenArray=new String[5][5];
for(int i=0;i<5;i++) //populating visibleArray with '_'
{
for(int j=0;j<5;j++)
{
visibleArray[i][j]="_";
}
}
for(int i=0,k=0;i<5;i++) //populating hiddenArray with
{ //random 'B'. k is the count of B
for(int j=0;j<5;j++)
{
if(random.nextInt(2)==0 && k<5) //random generates
{ //a number 0 or 1
hiddenArray[i][j]="B";
k++;
}
else if(i==4 && k<5)// if B is not 5 in count
{
hiddenArray[i][j]="B";
k++;
}
else
{
hiddenArray[i][j]="S";
}
}
}
play(visibleArray,hiddenArray,1); //calling function
}
static void play(String[][] firstArray,String[][] secondArray,int round)
{
System.out.print("Out for round "+round+": ");
for(int i=0;i<5;i++) //printing all firstArray
{
for(int j=0;j<5;j++)
{
System.out.print(firstArray[i][j]+" ");
}
}
System.out.println("\n Please enter a position to sweep for mines: ");
x=sc.nextInt(); //take input position 1
y=sc.nextInt(); //take input position 2
if(x<5&&y<5)
{
if(secondArray[x][y]=="S") //check with secondArray to
{ //find if the position is safe
firstArray[x][y]="S";
round++;
play(firstArray,secondArray,round);//recursive
} //function call
else//if position is bombed
{
System.out.println("Sorry! You stepped on a mine in round "+round+". \nYou had "+(round-1)+" points. \nThe actual field:\n");
for(int i=0;i<5;i++)//print actual field
{
for(int j=0;j<5;j++)
{
System.out.print(secondArray[i][j]+" ");
}
System.out.println("");
}
}
}
else //if the input position is invalid
{
System.out.println("That is an invalid position, please enter a position between 0 and 4 for the row and column.");
play(firstArray,secondArray,round); //recursive function call
}
}
}
Output:
More doubts? Ask in the comments. Please leave a thumbs up. Thanks.