Advertisement
IhavenonameSDA

Adjacency criss cross applesauce solver

Dec 3rd, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct state {
  7.     int gamestate[9];
  8.     int depth;
  9.     int index;
  10.     string statepath;
  11.     string movepath;
  12. };
  13.  
  14. state STATETREE[320000];
  15. state currState;
  16. state nextState;
  17. state winState;
  18. state teststate;
  19. int testdepth=0;
  20. int testindex=0;
  21. int PAR=37;
  22. bool didwin=false;
  23.  
  24. void initializeTree();
  25. void makeMove(int index, int cell);
  26. void updateTree(int index, int move);
  27. int getIndex(state s);
  28. string to_string(int number);
  29.  
  30. int main(){
  31.     int i=0;
  32.     int k=0;
  33.    
  34.     cout<<"Code running..."<<endl;
  35.     initializeTree();
  36.     cout<<"Tree initialized."<<endl;
  37.    
  38.     while(testdepth<PAR&&didwin==false){
  39.         while(i<320000){
  40.             if(STATETREE[i].depth==testdepth){
  41.                 currState=STATETREE[i];
  42.                 while(k<4){
  43.                     makeMove(i, k);
  44.                     testindex=getIndex(nextState);
  45.                     if(testdepth < STATETREE[testindex].depth){
  46.                         updateTree(testindex, k);
  47.                     }
  48.                     k++;
  49.                 }
  50.             }
  51.             k=0;
  52.             i++;
  53.         }
  54.         i=0;
  55.         if(didwin==false){
  56.             cout<<"Depth "<<testdepth+1<<" completed. Proceeding to depth "<<testdepth+2<<"."<<endl;
  57.         }
  58.         else{
  59.             cout<<"Solution found at depth: "<<testdepth+1<<endl;
  60.         }
  61.         testdepth++;
  62.     }
  63. }
  64.  
  65. void initializeTree(){
  66.     int i=0;
  67.     int j=0;
  68.     while(i<320000){
  69.         STATETREE[i].depth=99;
  70.         STATETREE[i].index=i;
  71.         STATETREE[i].movepath="";
  72.         STATETREE[i].statepath="";
  73.             if(i==0){
  74.                 while(j<9){
  75.                     STATETREE[i].gamestate[j]=0;   
  76.                     j++;
  77.                 }
  78.             }
  79.             else{
  80.                 j=0;
  81.                 while(j<9){
  82.                     STATETREE[i].gamestate[j]=STATETREE[i-1].gamestate[j]; 
  83.                     j++;
  84.                 }
  85.                 j=8;
  86.                 STATETREE[i].gamestate[j]++;
  87.                 while(j>=0){
  88.                     if(j==8){
  89.                         if(STATETREE[i].gamestate[j]==2){
  90.                             STATETREE[i].gamestate[j]=0;
  91.                             STATETREE[i].gamestate[j-1]++;
  92.                         }
  93.                     }
  94.                     if(j>=4&&j<=7){
  95.                         if(STATETREE[i].gamestate[j]==5){
  96.                             STATETREE[i].gamestate[j]=0;
  97.                             STATETREE[i].gamestate[j-1]++;
  98.                         }
  99.                     }
  100.                     if(j>=0&&j<=3){
  101.                         if(STATETREE[i].gamestate[j]==4){
  102.                             STATETREE[i].gamestate[j]=0;
  103.                             if(j!=0){
  104.                                 STATETREE[i].gamestate[j-1]++;
  105.                             }
  106.                         }
  107.                     }
  108.                     j--;
  109.                 }
  110.             }
  111.         j=0;
  112.         i++;   
  113.     }
  114.     STATETREE[124998].depth=0;
  115. }
  116.  
  117. //cell is 0-3. Cell 0 is the top, cell 1 is the left or right (whichever is active), cell 2 is the bottom, and cell 3 is the switch.
  118. //gamestate[8] is the switch value. 0 means that cell 2 (gamestate[1]) is active, while a 1 means that cell 3 (gamestate[2]) is active.
  119. void makeMove(int index, int cell){
  120.     nextState=currState;
  121.     if(cell==0){
  122.         if(nextState.gamestate[8]==0){
  123.             nextState.gamestate[5]=nextState.gamestate[1];
  124.             nextState.gamestate[1]=nextState.gamestate[0];
  125.         }
  126.         else{
  127.             nextState.gamestate[6]=nextState.gamestate[2];
  128.             nextState.gamestate[2]=nextState.gamestate[0];
  129.         }
  130.         if(nextState.gamestate[4]!=4){
  131.             nextState.gamestate[0]=nextState.gamestate[4];
  132.             nextState.gamestate[4]=4;
  133.         }
  134.     }
  135.     if(cell==2){
  136.         if(nextState.gamestate[8]==0){
  137.             nextState.gamestate[5]=nextState.gamestate[1];
  138.             nextState.gamestate[1]=nextState.gamestate[3];
  139.         }
  140.         else{
  141.             nextState.gamestate[6]=nextState.gamestate[2];
  142.             nextState.gamestate[2]=nextState.gamestate[3];
  143.         }
  144.         if(nextState.gamestate[7]!=4){
  145.             nextState.gamestate[3]=nextState.gamestate[7];
  146.             nextState.gamestate[7]=4;
  147.         }      
  148.     }
  149.     if(cell==1){
  150.         //update pool values
  151.         nextState.gamestate[4]=nextState.gamestate[0];
  152.         nextState.gamestate[7]=nextState.gamestate[3];
  153.         if(nextState.gamestate[8]==0){ //update cell values
  154.             nextState.gamestate[0]=nextState.gamestate[1];
  155.             nextState.gamestate[3]=nextState.gamestate[1];
  156.             if(nextState.gamestate[5]!=4){
  157.                 nextState.gamestate[1]=nextState.gamestate[5];
  158.                 nextState.gamestate[5]=4;
  159.             }
  160.         }
  161.         else{
  162.             nextState.gamestate[0]=nextState.gamestate[2];
  163.             nextState.gamestate[3]=nextState.gamestate[2];
  164.             if(nextState.gamestate[6]!=4){
  165.                 nextState.gamestate[2]=nextState.gamestate[6];
  166.                 nextState.gamestate[6]=4;
  167.             }
  168.         }
  169.     }
  170.     if(cell==3){
  171.         nextState.gamestate[8]=nextState.gamestate[8]-1;
  172.         if(nextState.gamestate[8]<0){
  173.             nextState.gamestate[8]=nextState.gamestate[8]*-1;  
  174.         }
  175.     }
  176. }
  177.  
  178. void updateTree(int index, int move){
  179.     string moveappend = to_string(move);
  180.     string pathappend = to_string(currState.index);
  181.     STATETREE[index].depth=testdepth+1;
  182.     STATETREE[index].movepath=currState.movepath + moveappend;
  183.     STATETREE[index].statepath=currState.statepath + " " + pathappend;
  184.     if(index >= 251250 && index <= 252499){
  185.         didwin = true;
  186.         winState = STATETREE[index];
  187.         cout << "State path: " << winState.statepath << endl;
  188.         cout << "Move path: " << winState.movepath << endl;
  189.     }
  190. }
  191.  
  192. /*
  193.     (STATETREE[n]) % 2 = switch state.
  194.     (STATETREE[n] / 2) % 5 = pool 4 state.
  195.     (STATETREE[n] / 10) % 5 = pool 3 state.
  196.     (STATETREE[n] / 50) % 5 = pool 2 state.
  197.     (STATETREE[n] / 250) % 5 = pool 1 state.
  198.     (STATETREE[n] / 1250) % 4 = cell 4 state.
  199.     (STATETREE[n] / 5000) % 4 = cell 3 state.
  200.     (STATETREE[n] / 20000) % 4 = cell 2 state.
  201.     (STATETREE[n] / 80000) % 4 = cell 1 state.
  202. */
  203. int getIndex(state s){
  204.     return s.gamestate[0]*80000 + s.gamestate[1]*20000 + s.gamestate[2]*5000 + s.gamestate[3]*1250 +
  205.     s.gamestate[4]*250 + s.gamestate[5]*50 + s.gamestate[6]*10 + s.gamestate[7]*2 + s.gamestate[8];
  206. }
  207.  
  208. // Lazy to_string patch by user1890351 on stackoverflow
  209. // Why the function was removed is beyond me, but this should work
  210. string to_string(int number){
  211.     string number_string = "";
  212.     char ones_char;
  213.     int ones = 0;
  214.     while(true){
  215.         ones = number % 10;
  216.         switch(ones){
  217.             case 0: ones_char = '0'; break;
  218.             case 1: ones_char = '1'; break;
  219.             case 2: ones_char = '2'; break;
  220.             case 3: ones_char = '3'; break;
  221.             case 4: ones_char = '4'; break;
  222.             case 5: ones_char = '5'; break;
  223.             case 6: ones_char = '6'; break;
  224.             case 7: ones_char = '7'; break;
  225.             case 8: ones_char = '8'; break;
  226.             case 9: ones_char = '9'; break;
  227.         }
  228.         number -= ones;
  229.         number_string = ones_char + number_string;
  230.         if(number == 0){
  231.             break;
  232.         }
  233.         number = number/10;
  234.     }
  235.     return number_string;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement