amanhasnoname
amanhasnoname

Reputation: 276

In Java how can I initialize a 2d array with a specific amount of random values.

I'm making a simulation game where there are doodlebugs and ants and as the simulation goes on the doodlebugs try to eat the ants. The issue I am running into is initializing the 2d array I've created. I need 100 ants and 5 doodlebugs to randomly be placed on the grid. I've randomized the grid but just as a whole so I get a random amount of ants and doodlebugs. I'm also working with a smaller array just util I get this working. Any help would be much appreciated.

 Random rand = new Random(); 
int[][] cells = new int[10][10];

public void display() {

    for(int i=0; i<10; i++) {
        for(int j=0; j<10; j++) {
            cells[i][j] = (int) (Math.random()*3);

            if(cells[i][j] == 2) { // 2 = ants; 
                cells[i][j] = 4;
            }
            if(cells[i][j] == 1) { // 1 = doodlebugs 
                cells[i][j] = 3;
            }
            if(cells[i][j] == 0) {
                cells[i][j] = 0;
            }
        System.out.print(cells[i][j]);  
        }
        System.out.println();
    }
}

Upvotes: 0

Views: 296

Answers (2)

tsttt2
tsttt2

Reputation: 90

You created a 10x10 array, which means it has 100 positions, which means all positions should be occupied by ants? (because you told you need 100 ants - unless the same position can have more than 1 insect).


Anyway, I think you're doing an "inverse" logic. Think about what should be random and what should not. You're looping the entire array and calling random() to know what insect must be placed in each position, so you have no control on how many of each insect will be created.

If you need an exact number of ants and doodlebugs, those are fixed - you don't call random() to know if it's an ant or a dooblebug, you already know how many of them you need. What must be random is their positions, so you should call random() to get the row and column positions, not the insect type.

First of all, I'd create some classes to represent the insects and the cell:

// enum type, better than "magic numbers"
public enum Insect {
    ANT,
    DOODLEBUG;
}

public class Cell {

    // the insect placed in this cell
    private Insect insect;

    public Cell() {
        // cell starts without any insect
        this.insect = null;
    }

    public Insect getInsect() {
        return insect;
    }

    public void setInsect(Insect insect) {
        this.insect = insect;
    }

    // check if cell already has an insect
    public boolean isOccupied() {
        return this.insect != null;
    }
}

Then I initialize the board:

int size = 10; // assuming a square
Cell[][] cells = new Cell[size][size];
// fill with empty cells
for (int i = 0; i < cells.length; i++) {
    for (int j = 0; j < cells[i].length; j++) {
        cells[i][j] = new Cell();
    }
}

And then I fill it with the insects:

// how many ants? Just an example, change the value according to your needs
int nAnts = 8;
// how many doodlebugs? Just an example, change the value according to your needs
int nBugs = 2;

Random rand = new Random();

// place the ants
for (int i = 0; i < nAnts; i++) {
    int row, column;
    // get a random position that's not occupied
    do {
        row = rand.nextInt(size);
        column = rand.nextInt(size);
    } while (cells[row][column].isOccupied());

    // fill with ant
    cells[row][column].setInsect(Insect.ANT);
}

// place the doodlebugs
for (int i = 0; i < nBugs; i++) {
    int row, column;
    // get a random position that's not occupied
    do {
        row = rand.nextInt(size);
        column = rand.nextInt(size);
    } while (cells[row][column].isOccupied());

    // fill with doodlebug
    cells[row][column].setInsect(Insect.DOODLEBUG);
}

The for loops placing insects are a bit repetitive so you could also refactor them to a method.

I also assumed that cells is a square, but if it's not, just create 2 variables for the number of rows and columns and change the code accordingly - the logic is the same, though.

Upvotes: 1

Nick Silvestri
Nick Silvestri

Reputation: 321

A simple way to do this would be to create one for loop that loops the same number of times as the maximum number of things you want (in this case, ants and doodlebugs). In each iteration of the for loop, you can generate random coordinates for the thing. This equates to two for loops, one for ants and one for doodlebugs.

for (int i = 0; i < desiredNumOfAnts; i++)
{
     int randX = rand.nextInt(cells[i].length); // this generates a random X coordinate, up to the length of the current row
     int randY = rand.nextInt(cells.length); // this generates a random Y coordinate, according to the height of the array

     /* using these coordinates, insert a new ant into the cells */
}

See if you can figure out how to do the rest of this yourself!

Upvotes: 0

Related Questions