Java完成拼图(方法)

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;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值