Question

In: Accounting

Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...

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.

Solutions

Expert Solution

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.


Related Solutions

Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values...
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array. Declare a multi-dimensional array of integers, which is 10 rows of...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT