Question

Java method that accepts (so you don't need to make an array for it) a 2d...

Java method that accepts (so you don't need to make an array for it) a 2d array.of unknown row and column length. it functions like minesweeper but a lot smaller scale. basically in this case the 0's in the array indicate nothing an 8's indicate a mine. So squares surrounding the 8 (mine) indicate how many mines are adjacent to it. So if there are two mines adjacent to one squre it will show two.so like this.

0 1 1 2 1 1

0 1 8 2 8 1

0 1 1 2 1 1

0 0 0 0 0 0

public static void findMinesweeper(int[][] array) {
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class MineSweeper {

    public static void findMinesweeper(int[][] array) {
        int r, c, count;
        for(int i = 0; i < array.length; ++i) {
            for(int j = 0; j < array[i].length; ++j) {
                if(array[i][j] != 8) {
                    count = 0;
                    r = i-1;
                    c = j-1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i-1;
                    c = j;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i-1;
                    c = j+1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i;
                    c = j-1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i;
                    c = j+1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i+1;
                    c = j-1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i+1;
                    c = j;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    r = i+1;
                    c = j+1;
                    if(r < array.length && r >= 0 && c < array[r].length && c >= 0 && array[r][c] == 8) {
                        count++;
                    }
                    array[i][j] = count;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] mines = {{0, 0, 0, 0, 0, 0}, {0, 0, 8, 0, 8, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};
        findMinesweeper(mines);
        for(int i = 0; i < mines.length; ++i) {
            for(int j = 0; j < mines[i].length; ++j) {
                System.out.print(mines[i][j] + "\t");
            }
            System.out.println();
        }
    }

}

Add a comment
Know the answer?
Add Answer to:
Java method that accepts (so you don't need to make an array for it) a 2d...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java Write methods for 2d 1. a method to calculate the sum of a 2d double...

    java Write methods for 2d 1. a method to calculate the sum of a 2d double array 2. a method to calculate the sum of each row of a 2d double array 3. a method to calculate the sum of each column of a 2d double array 4. a method to calculate the average of a 2d double array 5. a method to calculate the average of each row of a 2d double array 6. a method to calculate the...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • The last element in each array in a 2D array is incorrect. It’s your job to...

    The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...

  • How to replace elements in a 2D array? Okay so for my program, I am trying...

    How to replace elements in a 2D array? Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • JAVA ONLY 2D Arrays: You have been given a 10x10 array of integers, called Puzzle. You...

    JAVA ONLY 2D Arrays: You have been given a 10x10 array of integers, called Puzzle. You are to process the array, so that it verifies that each column’s elements are equal to the column’s index. That is, all elements in column 0 should be a 0, all elements in column 1 should be a 1, and so on. (Do not create the array, it is already created, no need to create main either)

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...

    Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12, 8} so The first gap is (5,7)=-2 and the absolute value is 2 The second gap is (7,4)=3 Third gap is (4,9) = -5 its...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT