Reputation: 19
Basically I have a 2D puzzle which is represented as a 4x4 integer array. The Array is initialised with integers 0..15 I have to Implement the method:
public static void play(int[][] puzzle)
method should take as its argument a 4x4 integer array that represents the puzzle after it has been scrambled. The method should be implemented using a loop that repeatedly: 1.Prints the current state of the puzzle 2.Asks the player to enter a rotation command 3.Performs the rotation requested by the player The loop should terminate once the puzzle is back in its original unscrambled state which means it has to be back to the original Puzzle in order of the numbers 0 to 15.
The program should display a congratulation message when this happens.
The program should include user input validation.
Whenever the user input is invalid, the program should print the warning Invalid input! User input is valid if it is in the form of one of the two commands row or column rotation.
The template of the Puzzle is :
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) {
}
}
As you can see, I have tried to implement the rotating the row method but after I run it, the whole array gets changed instead of a specified row. Can someone please show me what I am doing wrong? Thank you :).
Upvotes: 1
Views: 366
Reputation: 6780
Try this:
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;
}
}
I tested it out and it works great as far as I can tell.
Upvotes: 0