lundi 12 mars 2018

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();
    }
}




Aucun commentaire:

Enregistrer un commentaire