0

I was trying to generate a simulation of Game of 15. Currently at the stage to print a 2D array of randomly generated numbers from 1-15. This could be an example of an output, where the empty space could be any spot in the array:

1   3   5   6
7   9   10  13
2   4   8   11
12  14  15

public class Game {
public static void main(String[] args) {
    Set<Integer> set = new HashSet<>();
    int gameboard[][] = new int[4][4];

    while(set.size() != 4*4){
        set.add((int)(1+Math.random() * 16));
    }

    List<Integer> list = new ArrayList<>(set);

    for (int row=0; row < gameboard.length; row++) {
        for (int col=0; col < gameboard[row].length; col++) {
            gameboard[row][col] = list.get(row*gameboard.length + col);
            System.out.printf("%-4d",gameboard[row][col]);
        }
        System.out.println();
        }
      }

}

However, my current code prints 1-15 in ascending order like this:

1   2   3   4
5   6   7   8
9   10  11  12
13  14  15  16

How should I make the numbers scrambled and different for every run and as well as there being an empty element(like the output above) so the numbers range from 1-15?

2 Answers2

0

There is no reason to first store the numbers in a Set; you can generate random numbers directly to your 2D-array. Furthermore, you are multiplying by 16 instead of 15, so you get numbers ranging 1-16 instead of 1-15.

Since you need the numbers to be unique, you can store the already generated numbers in a list and check the list each iteration. Furthermore, your 2D-array has 16 slots, but you can only fit 15 numbers, so you need to be aware of that as well.

If you want the empty slot to be random, you can simply choose a random number ranging from 1 to 16 and skip that instead of skipping 16. Instead of break you would use continue though, as we aren't done filling the 2D-array if the empty slot isn't the last one.

int[][] gameboard = new int[4][4];
ArrayList<Integer> used = new ArrayList<Integer>();
int emptySlot = (int) (1 + Math.random() * 16);

for(int row = 0; row < gameboard.length; row++) {
    for(int col = 0; col < gameboard[row].length; col++) {
        if(row*gameboard.length + col == emptySlot) {
            System.out.print("    ");
            continue; //skip empty slot
        }
        int number;
        while(used.contains(number = (int) (1 + Math.random() * 15)));
        used.add(number);
        gameboard[row][col] = number;
        System.out.printf("%-4d",gameboard[row][col]);
      }
    System.out.println();
}
RaminS
  • 2,208
  • 4
  • 22
  • 30
  • true, but the numbers generated also can repeat in the table, how should I make it so each element in 2d array is different? – user10947891 Feb 03 '19 at 00:47
  • You didn't mention the need for that in your question. Please update your question. – RaminS Feb 03 '19 at 01:08
  • yes, I just updated it. thank you! Would you have any idea with this? – user10947891 Feb 03 '19 at 01:10
  • I just realized the empty slot in the output is always the last element in the table. Is there anyway I could write the code so it would be any element in the 2d array? – user10947891 Feb 03 '19 at 01:46
0

I would use the: ThreadLocalRandom.current().nextInt(1, 16) which will return a (pseudo) random integer between 1 and 15 to generate the numbers

rasmus91
  • 3,024
  • 3
  • 20
  • 32