public class TestPuzzle {
public static Scanner sc = new Scanner(System.in);
public static Random r = new Random();
public static int[][] answer;
public static int x;
public static int y;
public static int level;
public static long start;
public static long end;
public static int step;
public static void main(String[] args) {
System.out.println("请选择难度:1.简单 2.普通 3.困难");
//获取难度
level = sc.nextInt() + 2;
//初始化拼图
int[][] puzzle = initPuzzle();
int[] zeroIndex = findZero(puzzle);
x = zeroIndex[0];
y = zeroIndex[1];
//开始游戏
showPuzzle(puzzle);
start = System.currentTimeMillis();
for(;;){
play(puzzle);
if(isSuccess(puzzle)){
end = System.currentTimeMillis();
System.out.println("恭喜,完成");
break;
}
}
System.out.println("所花时间:"+(end - start));
System.out.println("所走步数:"+step);
}
public static void play(int[][] puzzle){
System.out.println("请输入:w.上移 s.下移 a.左移 d.右移");
switch(sc.next()){
case "w":
top(puzzle);
break;
case "s":
down(puzzle);
break;
case "a":
left(puzzle);
break;
case "d":
right(puzzle);
break;
case "xxx":
puzzle = answer;
break;
}
step++;
showPuzzle(puzzle);
}
public static void top(int[][] puzzle){
if(x == level - 1){
System.out.println("无法上移");
}else{
int t = puzzle[x][y];
puzzle[x][y] = puzzle[x + 1][y];
puzzle[x + 1][y] = t;
x++;
}
}
public static void down(int[][] puzzle){
if(x == 0){
System.out.println("无法下移");
}else{
int t = puzzle[x][y];
puzzle[x][y] = puzzle[x - 1][y];
puzzle[x - 1][y] = t;
x--;
}
}
public static void left(int[][] puzzle){
if(y == level - 1){
System.out.println("无法左移");
}else{
int t = puzzle[x][y];
puzzle[x][y] = puzzle[x][y + 1];
puzzle[x][y + 1] = t;
y++;
}
}
public static void right(int[][] puzzle){
if(y == 0){
System.out.println("无法右移");
}else{
int t = puzzle[x][y];
puzzle[x][y] = puzzle[x][y - 1];
puzzle[x][y - 1] = t;
y--;
}
}
public static boolean isSuccess(int[][] puzzle){
for(int i = 0;i <= puzzle.length - 1;i++){
for(int j = 0;j <= puzzle[i].length - 1;j++){
if(answer[i][j] != puzzle[i][j]){
return false;
}
}
}
return true;
}
public static void showPuzzle(int[][] puzzle){
for(int[] t : puzzle){
for(int te : t){
System.out.print(te+"\t");
}
System.out.println();
}
}
public static int[] findZero(int[][] puzzle){
for(int i = 0;i <= puzzle.length - 1;i++){
for(int j = 0;j <= puzzle[i].length - 1;j++){
if(puzzle[i][j] == 0){
return new int[]{i,j};
}
}
}
return null;
}
public static int[][] initPuzzle(){
//准备一维数组
int[] array = initArray();
//准备答案
answer = oneToTwo(array);
//打乱一维数组
shuffle(array);
//生成拼图
return oneToTwo(array);
}
public static void shuffle(int[] array){
for(int i = 1;i <= array.length;i++){
int i1 = r.nextInt(array.length);
int i2 = r.nextInt(array.length);
int t = array[i1];
array[i1] = array[i2];
array[i2] = t;
}
}
public static int[][] oneToTwo(int[] array){
int[][] arr = new int[level][level];
for(int i = 0;i <= array.length - 1;i++){
arr[i / level][i % level] = array[i];
}
return arr;
}
public static int[] initArray(){
int[] array = new int[level * level];
for(int i = 0;i <= array.length - 1;i++){
array[i] = i + 1;
}
array[array.length - 1] = 0;
return array;
}
}