Skip to main content
added 1 character in body
Source Link
public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  int[] currTile = {tileGrid.length - 1, 0}; // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom leftright
      // can move up or rightleft
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}
public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  int[] currTile = {tileGrid.length - 1, 0}; // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    }else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}
public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  int[] currTile = {tileGrid.length - 1, 0}; // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom right
      // can move up or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}
added 98 characters in body
Source Link
public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  int[] currTile = moveTile(currTile[0],{tileGrid.length currTile[1],- 1, 0)}; // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    }else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}

In the first state, currTile (hole) is in the top left of the grid: int[] currTile = {tileGrid.length - 1, 0};

tileGrid.length = amount of rows, -1 = top row, as top row = number of rows -1.

0 = col to the left.

public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  currTile = moveTile(currTile[0], currTile[1], 1, 0); // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    }else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}
public int[] moveTile(int clickedRow, int clickedCol, int x, int y) {
  // { ... moves the tile ... }
  return new int[]{clickedRow + y, clickedCol + x}; //returns new tile position on the grid
}

public void shuffle() {
  int[] currTile = {tileGrid.length - 1, 0}; // first move, to the right
  for (int i = 0; i < 1000; i++) { // it's set to 1000 for testing 
    if (currTile[0] == tileGrid.length - 1 && currTile[1] == tileGrid[0].length - 1) { // top right
      // can move left or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else if (currTile[0] == 0 && currTile[1] == 0) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      }
    }else if (currTile[0] == 0 && currTile[1] == tileGrid.length - 1) { // bottom left
      // can move up or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      }
    } else if (currTile[0] == 0 && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // bottom row
      //can move up, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1); // up
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[0] == tileGrid.length - 1
                   && currTile[1] > 0 && currTile[1] < tileGrid.length - 1) { // top row
      //can move down, left or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, -1); // down
      } else {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0); // left or right
      }
    } else if (currTile[1] == 0
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // left column
      //can up down or right
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0); // right
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[1] == tileGrid.length - 1
                   && currTile[0] > 0 && currTile[0] < tileGrid.length - 1) { // right column
      //can move up or down or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], -1, 0); // left
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1); // up or down
      }
    } else if (currTile[0] == tileGrid.length - 1 && currTile[1] == 0) {
      //can only move right or down
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, -1);
      }
    } else if (currTile[0] == 0 && currTile[1] == tileGrid.length) {
      //can only move top or left
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], 0, 1);
      } else {
        currTile = moveTile(currTile[0], currTile[1], -1, 0);
      }
    } else {
      // can move in any direction
      if (ran.nextBoolean()) {
        currTile = moveTile(currTile[0], currTile[1], ran.nextInt(1 + 1 + 1) - 1, 0);
      } else {
        currTile = moveTile(currTile[0], currTile[1], 0, ran.nextInt(1 + 1 + 1) - 1);
      }
    }
  }
}

In the first state, currTile (hole) is in the top left of the grid: int[] currTile = {tileGrid.length - 1, 0};

tileGrid.length = amount of rows, -1 = top row, as top row = number of rows -1.

0 = col to the left.

added 98 characters in body
Source Link

As clarification, currTile is the hole, swapping it with random tiles will shuffle the puzzle.

As clarification, currTile is the hole, swapping it with random tiles will shuffle the puzzle.

deleted 21 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
edited body
Source Link
Loading
edited body
Source Link
Loading
added 1 character in body
Source Link
Loading
Source Link
Loading