rubikscube

college code for finding optimal rubiks cube solutions in java

git clone https://9o.is/git/rubikscube.git

RubiksHalfEdgeState.java

(3416B)


      1 import java.util.LinkedList;
      2 import java.util.List;
      3 
      4 /**
      5  * Holds part of the edge state of the rubik's cube with a
      6  * heuristic that measures the amount of moves away from
      7  * the goal state.
      8  */
      9 public class RubiksHalfEdgeState extends RubiksSubState {
     10     public final static String[][] COMBINATIONS = {
     11             {"GR", "RG"},
     12             {"WR", "RW"},
     13             {"RB", "BR"},
     14             {"RY", "YR"},
     15             {"YG", "GY"},
     16             {"BY", "YB"},
     17             {"YO", "OY"},
     18             {"OG", "GO"},
     19             {"BO", "OB"},
     20             {"OW", "WO"},
     21             {"GW", "WG"},
     22             {"WB", "BW"}};
     23 
     24     public RubiksHalfEdgeState(String state, int heuristic) throws Exception {
     25         super(state, heuristic);
     26         if(!isValid(state))
     27            throw new Exception();
     28     }
     29 
     30     public RubiksHalfEdgeState(String state) throws Exception {
     31         super(state);
     32         if(!isValid(state))
     33             throw new Exception();
     34     }
     35 
     36     @Override
     37     protected boolean isValid(String str) {
     38         if(str.length() == 12) {
     39             List<Integer> edgesChecked = new LinkedList<Integer>();
     40             for(int i=0; i<str.length(); i+=2) {
     41                 String edge =
     42                         str.substring(i,i+2).toUpperCase();
     43                 int check = isValidEdge(edge);
     44                 if(check == -1) return false;
     45                 else {
     46                     for(int edgesUsed : edgesChecked) {
     47                         if(edgesUsed == check) return false;
     48                     }
     49                     edgesChecked.add(check);
     50                 }
     51             }
     52             return true;
     53         }
     54         return false;
     55     }
     56 
     57     @Override
     58     //TODO
     59     protected int hashState() {
     60         int counter = 0;
     61         int[] c = new int[6];
     62         int[] o = new int[6];
     63         int[] arraycounter = {0,1,2,3,4,5,6,7,8,9,10,11};
     64 
     65         for(int i=0; i<6;i++) {
     66             String str = state.substring(i*2,(i*2)+2);
     67             for(int j=0; j<12;j++) {
     68                 for(int k=0; k< COMBINATIONS[j].length; k++) {
     69                     if(str.compareTo(COMBINATIONS[j][k])==0) {
     70                         for(int m=j+1; m<arraycounter.length; m++) {
     71                             arraycounter[m] = arraycounter[m] - 1;
     72                         }
     73                         c[counter] = arraycounter[j];
     74                         o[counter] = k;
     75                         counter++;
     76                     }
     77                 }
     78             }
     79         }
     80 
     81         int f1 =
     82                 (fac(11)*c[0]+
     83                         fac(10)*c[1]+
     84                         fac(9)*c[2]+
     85                         fac(8)*c[3]+
     86                         fac(7)*c[4]+
     87                         fac(6)*c[5])/fac(6);
     88 
     89         int f2 =
     90                 (int)(o[5]*Math.pow(2,5) +
     91                         o[4]*Math.pow(2,4) +
     92                         o[3]*Math.pow(2,3) +
     93                         o[2]*Math.pow(2,2) +
     94                         o[1]*Math.pow(2,1) +
     95                         o[0]*Math.pow(2,0));
     96 
     97         return 64*f1 + f2;
     98     }
     99 
    100     /*
    101      * Returns which edge out of all combinations is the string
    102      * or -1 if it didn't match any.
    103      */
    104     public static int isValidEdge(String edge) {
    105         for(int i=0; i<COMBINATIONS.length; i++)
    106             for(int j=0; j<COMBINATIONS[i].length; j++) {
    107                 if(COMBINATIONS[i][j].equals(edge))
    108                     return i;
    109             }
    110         return -1;
    111     }
    112 }