繁体   English   中英

旋转用整数0-15初始化的4x4数组的指定行的方法?

[英]Method to rotate the specified row of a 4x4 array initialised with integers 0-15?

基本上我有一个2D拼图,表示为4x4整数数组。 数组初始化为整数0..15我必须实现方法:

public static void play(int[][] puzzle)

方法应该将4x4整数数组作为其参数,该数组表示拼图被加扰后的拼图。 该方法应该使用重复的循环来实现:1。打印拼图的当前状态2.要求玩家输入旋转命令3.执行玩家请求的旋转一旦拼图重新进入其中,循环应该终止原始未加扰状态,这意味着它必须按照数字0到15的顺序返回到原始拼图。

发生这种情况时,程序应显示祝贺消息。

该程序应包括用户输入验证。

每当用户输入无效时,程序应该打印警告无效输入! 如果用户输入采用行或列旋转两个命令之一的形式,则用户输入有效。

拼图的模板是:

public class Puzzle {

    public static final int N = 4;
    public static final int NUMBER_OF_ROTATIONS = 5;

    public static void main(String[] args) {
        int[][] puzzle = new int[N][N];
        reset(puzzle);
        test(puzzle);
        reset(puzzle);
        scramble(puzzle);
        System.out.println("### Testing puzzle game play\n");
        play(puzzle);
    }

    public static void print(int[][] puzzle) {
        for (int[] row : puzzle) {
            for (int elem : row) {
                System.out.printf("%4d", elem);
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void test(int[][] puzzle) {
        System.out.println("### Testing reset method\n");
        print(puzzle);
        System.out.println("### Testing rotate methods\n");
        print(puzzle);
        for (int i = 0; i < N; i++) {
            System.out.println("### rotateColumn(" + i + ")\n");
            rotateColumn(puzzle, i);
            print(puzzle);
            System.out.println("### rotateRow(" + i + ")\n");
            rotateRow(puzzle, i);
            print(puzzle);
        }
        reset(puzzle); 
        System.out.println("### Testing random rotations\n");
        print(puzzle); 
        for (int i = 0; i < 5; i++){
            randomRotation(puzzle);
            print(puzzle); 
        }
    }

    public static void reset(int[][] puzzle) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                puzzle[i][j] = i * N + j;
        }
    }

    public static void scramble(int[][] puzzle) {
        for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
            randomRotation(puzzle);
        }
    }




    public static void rotateRow(int[][] arr, int row) {
        for (int i= arr.length-1; i>0; i--) {
            int r= (int) (Math.random() * (i+1)); int[] c = arr[i];
            arr [i] = arr[r];
            arr[r] = c;
        }


    }


    // TODO: Implement method as specified in assignment brief 

    public static void rotateColumn(int[][] arr, int column) {
    }


    // TODO: Implement method as specified in assignment brief 

    public static void randomRotation(int[][] puzzle) {
    }

    // TODO: Implement method as specified in assignment brief 

    static void play(int[][] puzzle) {
    }

}

正如您所看到的,我已尝试实现旋转行方法但在运行之后,整个数组将被更改而不是指定的行。 有人可以告诉我我做错了什么吗? 谢谢 :)。

尝试这个:

public static void rotateRow(int[][] arr, int row) {
        int newCurrent = arr[row][arr.length - 1];
        int nextCurrent;
        for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) {
            nextCurrent = arr[row][currentIndex];
            arr[row][currentIndex] = newCurrent;
            newCurrent = nextCurrent;
        }
    }

我测试了它,据我所知,它的效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM