public class Dice { int up; int down; int left; int right; int front; int back; int posX; int posY; public Dice(int up, int left, int front, int posX, int posY){ this.up = up; this.left = left; this.front = front; this.right = 7 - this.left; this.down = 7 - this.up; this.back = 7 - this.front; this.posX = posX; this.posY = posY; } public Dice(Dice dice) { this.up = dice.up; this.left = dice.left; this.front = dice.front; this.right = dice.right; this.down = dice.down; this.back = dice.back; this.posX = dice.posX; this.posY = dice.posY; } public static void main(String[] args){ findSolutionsForDice("Dice1", 5, 6, 4, 4, 3); System.out.println(""); findSolutionsForDice("Dice2", 2, 4, 6, 1, 2); System.out.println(""); findSolutionsForDice("Dice3", 4, 5, 6, 3, 1); } public static void findSolutionsForDice(String name, int up, int left, int front, int posX, int posY){ // Make an array for the movement history. int[] moveHistory = new int[4]; // Make a dice in start position. Dice currentDice = new Dice(up, left, front, posX, posY); // Call the recursive function which applies movements to the dice. extendMovements(moveHistory, currentDice, 0); } public static void extendMovements(int[] history, Dice currentDice, int historyIndex){ for (int moveType = 0; moveType<10; moveType++){ // Make a new copy of the current dice. Dice dice = new Dice(currentDice); // Apply the next move. dice.change(moveType); // Remember which move we made. history[historyIndex] = moveType; // Test if we have achieved a target position. If yes, then print the movement history and the current dice state. if (dice.test()){ String solution = ""; for (int i = 0; i <= historyIndex; i++){ solution += Dice.getOpName(history[i]) + "; " ; } System.out.println(solution); dice.print(); } // No target position? Then try further movements. else if (historyIndex < 3) { extendMovements(history, dice, historyIndex + 1); } } } public void turnClockwise(){ int f = front; int r = right; int b = back; int l = left; left = f; front = r; right = b; back = l; } public void turnCounterclock(){ int f = front; int r = right; int b = back; int l = left; left = b; front = l; right = f; back = r; } public void moveEast(){ posX = posX + 1; } public void moveWest(){ posX = posX - 1; } public void moveNorth(){ posY = posY + 1; } public void moveSouth(){ posY = posY -1; } public void pushEast(){ posX = posX + 1; int u = up; int r = right; int d = down; int l = left; right = u; up = l; left = d; down = r; } public void pushWest(){ posX = posX - 1; int u = up; int r = right; int d = down; int l = left; right = d; up = r; left = u; down = l; } public void pushNorth(){ posY = posY + 1; int u = up; int f = front; int d = down; int b = back; back = u; up = f; front = d; down = b; } public void pushSouth(){ posY = posY -1; int u = up; int f = front; int d = down; int b = back; back = d; up = b; front = u; down = f; } public void change(int i){ if (i == 0){ turnClockwise(); } else if (i == 1){ turnCounterclock(); } else if (i == 2){ moveNorth(); } else if (i == 3){ moveWest(); } else if (i == 4){ moveEast(); } else if (i == 5){ moveSouth(); } else if (i == 6){ pushNorth(); } else if (i == 7){ pushSouth(); } else if (i == 8){ pushWest(); } else if (i == 9){ pushEast(); } } public static String getOpName(int i){ String name = ""; if (i == 0){ name = "turnClockwise"; } else if (i == 1){ name = "turnCounterclock"; } else if (i == 2){ name = "moveNorth"; } else if (i == 3){ name = "moveWest"; } else if (i == 4){ name = "moveEast"; } else if (i == 5){ name = "moveSouth"; } else if (i == 6){ name = "pushNorth"; } else if (i == 7){ name = "pushSouth"; } else if (i == 8){ name = "pushWest"; } else if (i == 9){ name = "pushEast"; } return name; } public boolean test(){ return ((up == 6) && (left == 5) && (posX == 2) && (posY > 1) && (posY < 5)); } public String stateToString(){ return "X: " + posX + "; Y: " + posY + "; Oben: " + up + "; Links: " + left + "; Vorne: " + front; } public void print(){ System.out.println(stateToString()); } }