dracobatman
dracobatman

Reputation: 19

How can I fill this 2D array with X characters?

I'm making my own game right now (i know its java) and I'm a bit confused on how to fill my 2D array with the value of x.

So far it only displays null instead of values. how could I randomize, and then hide 3 "bombs" using the character b and 1 "treasure" using the character T inside the values of the 2D Array?

I would like it hidden after it randomizes and I have zero ideas on how to do that. Maybe call back to another 2D array to display?

class Main {
    public static void main(String[] args) {
        import java.util.Arrays;
        String[][] grid1 = new String[8][8];
        for (int n = 0; n <= grid1.length; n++) {
            System.out.print(n + "\t");
        }
        System.out.println();

        for (int r = 0; r < grid1.length; r++) {
            System.out.print(r + 1 + "\t");
            for (int c = 0; c < grid1[r].length; c++)
                System.out.print(grid1[r][c] + "\t");
        }

        arrays.fill(grid1[0], so);
        System.out.println();
    }
}

As you can see I already tried import java.util.arrays but it gave me errors. So the arrays.fill won't work either.

After adding a few lines of code this is what happens:

0   1   2   3   4   5   6   7   8
1   null    null    null    null    null    null    null    null2   null    null    null    null    null    null    null    null3   null    null    null    null    null    null    null    null4   null    null    null    null    null    null    null    null5   null    null    null    null    null    null    null    null6   null    null    null    null    null    null    null    null7   null    null    null    null    null    null    null    null8   null    null    null    null    null    null    null    null
x   x   x   x   x   x   x   x
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null
null    null    null    null    null    null    null    null

I know the top looks odd but when moving the window into a certain position it lines up.

Upvotes: 1

Views: 2176

Answers (5)

joshi
joshi

Reputation: 1

The import statement is always defined above the whole program so as to import the prebuild functions and classes hence

import java.util.Arrays;

This will be the first line of the program before Main class is defined.

arrays.fill(grid1[0],so);

Here since so is not a variable, it seems as a string "so", it would be good to define it first or make it a string as "so" This code might give some idea on how to use it.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[][] grid1 = new String[8][8];

        for (int r = 0; r < grid1.length; r++) {
            for (int c = 0; c < grid1[r].length; c++)
                System.out.print(grid1[r][c] + "\t");
            System.out.println();
        }

        Arrays.fill(grid1[0], "so");
        System.out.println("\nAfter the array is: ");

        for (int r = 0; r < grid1.length; r++) {
            for (int c = 0; c < grid1[r].length; c++)
                System.out.print(grid1[r][c] + "\t");
            System.out.println();
        }
    }
}

Upvotes: 0

user14940971
user14940971

Reputation:

You can use Arrays#setAll method to fill a multidimensional array and Arrays#fill method inside it for each row.

public static void main(String[] args) {
    int n = 8;
    // create a new empty 2d array filled with nulls
    String[][] grid = new String[n][n];
    // set all array elements to 'X'
    Arrays.setAll(grid, i -> {
        Arrays.fill(grid[i], "X");
        return grid[i];
    });
    // output
    printGrid(grid);
}
public static void printGrid(String[][] grid) {
    // upper row with numbers
    IntStream.range(0, grid.length + 1).forEach(i -> System.out.print(i + " "));
    System.out.println();
    // grid contents with left column of numbers
    IntStream.range(0, grid.length).forEach(i -> {
        // left column with numbers
        System.out.print((i + 1) + " ");
        // grid contents line by line
        Arrays.stream(grid[i]).forEach(str -> System.out.print(str + " "));
        System.out.println();
    });
}

Output:

0 1 2 3 4 5 6 7 8 
1 X X X X X X X X 
2 X X X X X X X X 
3 X X X X X X X X 
4 X X X X X X X X 
5 X X X X X X X X 
6 X X X X X X X X 
7 X X X X X X X X 
8 X X X X X X X X 

See also: How do I return such an multi-dimensional array?

Upvotes: 0

Nowhere Man
Nowhere Man

Reputation: 19545

To fill the first row in 2D array, use Arrays.fill, to fill the rest of the rows, use Arrays.copyOf.

Next, it's better to implement separate methods to perform different tasks:

  1. Create and fill the grid
  2. Print the grid
static String[][] fillGrid(int rows, int cols, String cell) {
    String[][] grid = new String[rows][cols];

    String[] row = new String[cols];
    Arrays.fill(row, cell);
    grid[0] = row;
    for (int i = 1; i < rows; i++) {
        grid[i] = Arrays.copyOf(row, cols);
    }
    
    return grid;
}

static void printGrid(String[][] grid) {
    
    for (int i = 0; i < grid.length; i++) {
        if (i == 0) {
            System.out.print("   ");
            for (int j = 0; j < grid[0].length; j++) {
                System.out.printf("%2d ", j + 1);
            }
            System.out.println();
        }
        for (int j = 0; j < grid[i].length; j++) {
            if (j == 0) {
                System.out.printf("%2d:", i + 1);
            }
            System.out.printf("%2s ", grid[i][j]);
        }
        System.out.println();
    }
}

Then they may be tested like this:

public static void main(String ... args) {
    String[][] grid = fillGrid(8, 8, "x");
    printGrid(grid);
}

The output will be as follows:

    1  2  3  4  5  6  7  8 
 1: x  x  x  x  x  x  x  x 
 2: x  x  x  x  x  x  x  x 
 3: x  x  x  x  x  x  x  x 
 4: x  x  x  x  x  x  x  x 
 5: x  x  x  x  x  x  x  x 
 6: x  x  x  x  x  x  x  x 
 7: x  x  x  x  x  x  x  x 
 8: x  x  x  x  x  x  x  x 

Upvotes: 1

Nikola S.
Nikola S.

Reputation: 93

Ok, firstly all imports need to be typed at the very top aka beginning of the program so place import java.util.Arrays; above class Main.

 import java.util.Arrays;

 class Main {
     public static void main(String[] args) {
         String[][] grid1=new String[8][8];
         for (int n=0; n<=grid1.length; n++) {
             System.out.print(n+"\t");
         }
         System.out.println();

         for (String[] row: grid1) {
             Arrays.fill(row, "x");
         }
        
         for (int r=0; r<grid1.length; r++) {
             System.out.print(r+1+"\t");
             for (int c=0; c<grid1[r].length; c++)
                 System.out.print(grid1[r][c]+"\t");
             System.out.println();
         }
     }
 }

The block of code that does the filling

 for (String[] row: grid1) {
     Arrays.fill(row, "x");
 }

is an enhanced for loop. What it does is it takes each row from your 2D array, stores it in the local variable row, and then fills it (the row) with the value "x".

As for the other part of your question i suppose you could use a random number generator to generate the line and column where a bomb will be and store that number in a set. That way the program will know when the player steps on a bomb or treasure, but the player will be unaware of it until it happens.

Upvotes: 0

Peter Knall
Peter Knall

Reputation: 86

The "import java.util.arrays" needs to be above the class definition at the top of the file.

Upvotes: 0

Related Questions