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?