So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there
I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot
making it look like there are only four 1s. How would i go about fixing that?
package Grade12; import java.util.Random; public class Ghost { public static void main(String[] args) { Random generator = new Random(); int gameboard [][] = new int [8][8]; int randomx, randomy, counter = 0, sum = 0; for(int row = 0; row < 8; row++){ for(int col = 0; col < 8; col++){ (gameboard[row][col]) = 0; } } for(int row = 0; row < 8; row++){ for(int col = 0; col < 8; col++){ if( counter != 5){ randomx = (int)(Math.random()*8); randomy = (int)(Math.random()*8); gameboard[randomx][randomy] = 1; counter++; if(gameboard[randomx][randomy] == 1){ sum++; } } System.out.print(gameboard[row][col] + " "); } } } }