Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1.  
  2. public class Dice {
  3.  
  4.     int up;
  5.     int down;
  6.     int left;
  7.     int right;
  8.     int front;
  9.     int back;
  10.    
  11.     int posX;
  12.     int posY;
  13.    
  14.     public Dice(int up, int left, int front, int posX, int posY){
  15.         this.up    =  up;
  16.         this.left  = left;
  17.         this.front = front;
  18.         this.right = 7 - this.left;
  19.         this.down  = 7 - this.up;
  20.         this.back  = 7 - this.front;
  21.        
  22.         this.posX = posX;
  23.         this.posY = posY;
  24.     }
  25.    
  26.     public Dice(Dice dice)
  27.     {
  28.         this.up    = dice.up;
  29.         this.left  = dice.left;
  30.         this.front = dice.front;
  31.         this.right = dice.right;
  32.         this.down  = dice.down;
  33.         this.back  = dice.back;
  34.        
  35.         this.posX = dice.posX;
  36.         this.posY = dice.posY; 
  37.     }
  38.    
  39.     public static void main(String[] args){
  40.         findSolutionsForDice("Dice1", 5, 6, 4, 4, 3);
  41.         System.out.println("");
  42.         findSolutionsForDice("Dice2", 2, 4, 6, 1, 2);
  43.         System.out.println("");
  44.         findSolutionsForDice("Dice3", 4, 5, 6, 3, 1);
  45.     }
  46.    
  47.     public static void findSolutionsForDice(String name, int up, int left, int front, int posX, int posY){
  48.        
  49.         // Make an array for the movement history.
  50.         int[] moveHistory = new int[4];
  51.         // Make a dice in start position.
  52.         Dice currentDice  = new Dice(up, left, front, posX, posY);
  53.         // Call the recursive function which applies movements to the dice.
  54.         extendMovements(moveHistory, currentDice, 0);
  55.     }
  56.    
  57.     public static void extendMovements(int[] history, Dice currentDice, int historyIndex){
  58.         for (int moveType = 0; moveType<10; moveType++){
  59.             // Make a new copy of the current dice.
  60.             Dice dice = new Dice(currentDice);
  61.             // Apply the next move.
  62.             dice.change(moveType);
  63.             // Remember which move we made.
  64.             history[historyIndex] = moveType;
  65.             // Test if we have achieved a target position. If yes, then print the movement history and the current dice state.
  66.             if (dice.test()){
  67.                 String solution = "";
  68.                 for (int i = 0; i <= historyIndex; i++){
  69.                     solution += Dice.getOpName(history[i]) + "; " ;
  70.                 }
  71.                 System.out.println(solution);
  72.                 dice.print();
  73.             }
  74.             // No target position? Then try further movements.
  75.             else if (historyIndex < 3) {
  76.                 extendMovements(history, dice, historyIndex + 1);
  77.             }
  78.         }
  79.     }  
  80.    
  81.     public void turnClockwise(){
  82.         int f   = front;
  83.         int r   = right;
  84.         int b   = back;
  85.         int l   = left;    
  86.        
  87.         left   = f;
  88.         front  = r;
  89.         right  = b;
  90.         back   = l;
  91.     }
  92.  
  93.     public void turnCounterclock(){
  94.         int f   = front;
  95.         int r   = right;
  96.         int b   = back;
  97.         int l   = left;    
  98.        
  99.         left   = b;
  100.         front  = l;
  101.         right  = f;
  102.         back   = r;
  103.     }
  104.    
  105.     public void moveEast(){
  106.         posX = posX + 1;
  107.     }
  108.  
  109.     public void moveWest(){
  110.         posX = posX - 1;
  111.     }
  112.    
  113.     public void moveNorth(){
  114.         posY = posY + 1;
  115.     }
  116.    
  117.     public void moveSouth(){
  118.         posY = posY -1;
  119.     }
  120.    
  121.     public void pushEast(){
  122.         posX = posX + 1;
  123.    
  124.         int u   = up;
  125.         int r   = right;
  126.         int d   = down;
  127.         int l   = left;
  128.        
  129.         right = u;
  130.         up    = l;
  131.         left  = d;
  132.         down  = r;
  133.     }
  134.  
  135.     public void pushWest(){
  136.         posX = posX - 1;
  137.        
  138.         int u   = up;
  139.         int r   = right;
  140.         int d   = down;
  141.         int l   = left;
  142.        
  143.         right = d;
  144.         up    = r;
  145.         left  = u;
  146.         down  = l;     
  147.     }
  148.    
  149.     public void pushNorth(){
  150.         posY = posY + 1;
  151.        
  152.         int u   = up;
  153.         int f   = front;
  154.         int d   = down;
  155.         int b   = back;
  156.        
  157.         back  = u;
  158.         up    = f;
  159.         front = d;
  160.         down  = b;
  161.     }
  162.    
  163.     public void pushSouth(){
  164.         posY = posY -1;
  165.        
  166.         int u   = up;
  167.         int f   = front;
  168.         int d   = down;
  169.         int b   = back;
  170.        
  171.         back  = d;
  172.         up    = b;
  173.         front = u;
  174.         down  = f;
  175.     }
  176.    
  177.     public void change(int i){
  178.         if (i == 0){
  179.             turnClockwise();
  180.         }
  181.         else if (i == 1){
  182.             turnCounterclock();
  183.         }
  184.         else if (i == 2){
  185.             moveNorth();
  186.         }
  187.         else if (i == 3){
  188.             moveWest();
  189.         }
  190.         else if (i == 4){
  191.             moveEast();
  192.         }
  193.         else if (i == 5){
  194.             moveSouth();
  195.         }
  196.         else if (i == 6){
  197.             pushNorth();
  198.         }
  199.         else if (i == 7){
  200.             pushSouth();
  201.         }
  202.         else if (i == 8){
  203.             pushWest();
  204.         }
  205.         else if (i == 9){
  206.             pushEast();
  207.         }  
  208.     }
  209.    
  210.     public static String getOpName(int i){
  211.        
  212.         String name = "";
  213.        
  214.         if (i == 0){
  215.             name = "turnClockwise";
  216.         }
  217.         else if (i == 1){
  218.             name = "turnCounterclock";
  219.         }
  220.         else if (i == 2){
  221.             name = "moveNorth";
  222.         }
  223.         else if (i == 3){
  224.             name = "moveWest";
  225.         }
  226.         else if (i == 4){
  227.             name = "moveEast";
  228.         }
  229.         else if (i == 5){
  230.             name = "moveSouth";
  231.         }
  232.         else if (i == 6){
  233.             name = "pushNorth";
  234.         }
  235.         else if (i == 7){
  236.             name = "pushSouth";
  237.         }
  238.         else if (i == 8){
  239.             name = "pushWest";
  240.         }
  241.         else if (i == 9){
  242.             name = "pushEast";
  243.         }  
  244.        
  245.         return name;
  246.     }
  247.    
  248.     public boolean test(){
  249.         return ((up == 6) && (left == 5) && (posX == 2) && (posY > 1) && (posY < 5));
  250.     }
  251.  
  252.     public String stateToString(){
  253.         return "X: " + posX + "; Y: " + posY + "; Oben: " + up + "; Links: " + left + "; Vorne: " + front;
  254.     }
  255.    
  256.     public void print(){
  257.         System.out.println(stateToString());
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement