Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.helloworld;
- public class SortChallenge {
- public static void getPos(int[][] array, int[] set) {
- int i=0;
- int j=1;
- boolean X;
- int count = 0; //initialising i, j and counter and the loop break
- for (int a = 0; a < set.length; a++) { //for each number in the set array
- X = false;
- do {
- int pos_i = a / array.length; //sets it's destination i index
- int pos_j = a % array.length; //sets it's destination j index
- for (i = 0; i < array.length; i++) {
- for (j = 0; j < array[i].length; j++) {
- if (array[i][j] == set[a]) { //finds the index of the desired number
- System.out.println("Move Count = " + count);
- System.out.println("i = " + i + " j = " + j + " A = " + set[a]);
- //System.out.println("pi = " + pos_i + " pj = " + pos_j);
- //System.out.println("Current Spot = " + ((j + 1) + (i * 4))); //prints a bunch of shit for error checking
- if (j != pos_j) { //if j coordinate is not correct
- swapNumbersX(i, j, pos_j, array); //move in the x plane
- count += 1;
- System.out.println("j.. = " + j + "Pos j = " + pos_j);
- }else if(i != pos_i) { //if i coordinate is not correct
- swapNumbersY(i, j, pos_i, array); //move in the y plane
- count += 1;
- }if(i == pos_i && j == pos_j){ //if coordinates are correct
- X = true; //break the loop
- }
- }
- }
- }
- //System.out.println("j.. = " + j + "Pos j = " + pos_j);
- //System.out.println("i.. = " + i + "Pos i = " + pos_i);
- } while (X == false); //if i and j are correct values, this will be true
- }
- for (i = 0; i < array.length; i++) {
- for (j = 0; j < array[i].length; j++) {
- System.out.println((array[i][j]));
- }
- }
- }
- private static void swapNumbersX(int a, int b, int i, int[][] array) { //swaps numbers left or right depending on size
- int temp;
- if(i<=b) {
- temp = array[a][b];
- array[a][b] = array[a][b - 1];
- array[a][b - 1] = temp;
- }else{
- temp = array[a][b];
- array[a][b] = array[a][b + 1];
- array[a][b + 1] = temp;
- }
- }
- private static void swapNumbersY(int a, int b, int i, int[][] array) { //swaps numbers up or down depending on size
- int temp;
- if(i<=a) {
- temp = array[a][b];
- array[a][b] = array[a - 1][b];
- array[a - 1][b] = temp;
- }else{
- temp = array[a][b];
- array[a][b] = array[a + 1][b];
- array[a + 1][b] = temp;
- }
- }
- public static void main(String[] args) { //the main turd, initializes all the shit
- int[] set = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
- int[][] input = {{4,6,2,14},{15,8,13,1},{10,5,9,12},{7,11,16,3}};
- //int[][] input = {{2,10,5,14},{16,3,9,4},{7,11,15,8},{6,13,12,1}};
- getPos(input, set);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement