This code is a simulation of Game of 15 puzzle. The output should be:
I'm currently at the stage trying to do a switch statement in order to move a number to the empty space according to user input. Here's what I have attempted. Thank you.
import java.util.ArrayList;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
int rowNum=4;
int colNum=4;
int[][] gameboard = new int[rowNum][colNum];
ArrayList<Integer> used = new ArrayList<Integer>();
int emptySlot = (int) (1 + Math.random() * 16);
for(int row = 0; row < rowNum; row++) {
for(int col = 0; col < colNum; 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();
}
System.out.println();
System.out.print("Enter a move: (l)eft, (u)p, (r)ight, (d)own, or (exit):");
Scanner sc = new Scanner(System.in);
int px=0;
int py=0;
String move = sc.nextLine();
switch (move) {
case "l":
px -= 1;
break;
case "u":
py +=1;
break;
case "r":
px += 1;
break;
case "d":
py -=1;
break;
case "exit":
break;
}
sc.close();
}
}