Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct state {
- int gamestate[9];
- int depth;
- int index;
- string statepath;
- string movepath;
- };
- state STATETREE[320000];
- state currState;
- state nextState;
- state winState;
- state teststate;
- int testdepth=0;
- int testindex=0;
- int PAR=37;
- bool didwin=false;
- void initializeTree();
- void makeMove(int index, int cell);
- void updateTree(int index, int move);
- int getIndex(state s);
- string to_string(int number);
- int main(){
- int i=0;
- int k=0;
- cout<<"Code running..."<<endl;
- initializeTree();
- cout<<"Tree initialized."<<endl;
- while(testdepth<PAR&&didwin==false){
- while(i<320000){
- if(STATETREE[i].depth==testdepth){
- currState=STATETREE[i];
- while(k<4){
- makeMove(i, k);
- testindex=getIndex(nextState);
- if(testdepth < STATETREE[testindex].depth){
- updateTree(testindex, k);
- }
- k++;
- }
- }
- k=0;
- i++;
- }
- i=0;
- if(didwin==false){
- cout<<"Depth "<<testdepth+1<<" completed. Proceeding to depth "<<testdepth+2<<"."<<endl;
- }
- else{
- cout<<"Solution found at depth: "<<testdepth+1<<endl;
- }
- testdepth++;
- }
- }
- void initializeTree(){
- int i=0;
- int j=0;
- while(i<320000){
- STATETREE[i].depth=99;
- STATETREE[i].index=i;
- STATETREE[i].movepath="";
- STATETREE[i].statepath="";
- if(i==0){
- while(j<9){
- STATETREE[i].gamestate[j]=0;
- j++;
- }
- }
- else{
- j=0;
- while(j<9){
- STATETREE[i].gamestate[j]=STATETREE[i-1].gamestate[j];
- j++;
- }
- j=8;
- STATETREE[i].gamestate[j]++;
- while(j>=0){
- if(j==8){
- if(STATETREE[i].gamestate[j]==2){
- STATETREE[i].gamestate[j]=0;
- STATETREE[i].gamestate[j-1]++;
- }
- }
- if(j>=4&&j<=7){
- if(STATETREE[i].gamestate[j]==5){
- STATETREE[i].gamestate[j]=0;
- STATETREE[i].gamestate[j-1]++;
- }
- }
- if(j>=0&&j<=3){
- if(STATETREE[i].gamestate[j]==4){
- STATETREE[i].gamestate[j]=0;
- if(j!=0){
- STATETREE[i].gamestate[j-1]++;
- }
- }
- }
- j--;
- }
- }
- j=0;
- i++;
- }
- STATETREE[124998].depth=0;
- }
- //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.
- //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.
- void makeMove(int index, int cell){
- nextState=currState;
- if(cell==0){
- if(nextState.gamestate[8]==0){
- nextState.gamestate[5]=nextState.gamestate[1];
- nextState.gamestate[1]=nextState.gamestate[0];
- }
- else{
- nextState.gamestate[6]=nextState.gamestate[2];
- nextState.gamestate[2]=nextState.gamestate[0];
- }
- if(nextState.gamestate[4]!=4){
- nextState.gamestate[0]=nextState.gamestate[4];
- nextState.gamestate[4]=4;
- }
- }
- if(cell==2){
- if(nextState.gamestate[8]==0){
- nextState.gamestate[5]=nextState.gamestate[1];
- nextState.gamestate[1]=nextState.gamestate[3];
- }
- else{
- nextState.gamestate[6]=nextState.gamestate[2];
- nextState.gamestate[2]=nextState.gamestate[3];
- }
- if(nextState.gamestate[7]!=4){
- nextState.gamestate[3]=nextState.gamestate[7];
- nextState.gamestate[7]=4;
- }
- }
- if(cell==1){
- //update pool values
- nextState.gamestate[4]=nextState.gamestate[0];
- nextState.gamestate[7]=nextState.gamestate[3];
- if(nextState.gamestate[8]==0){ //update cell values
- nextState.gamestate[0]=nextState.gamestate[1];
- nextState.gamestate[3]=nextState.gamestate[1];
- if(nextState.gamestate[5]!=4){
- nextState.gamestate[1]=nextState.gamestate[5];
- nextState.gamestate[5]=4;
- }
- }
- else{
- nextState.gamestate[0]=nextState.gamestate[2];
- nextState.gamestate[3]=nextState.gamestate[2];
- if(nextState.gamestate[6]!=4){
- nextState.gamestate[2]=nextState.gamestate[6];
- nextState.gamestate[6]=4;
- }
- }
- }
- if(cell==3){
- nextState.gamestate[8]=nextState.gamestate[8]-1;
- if(nextState.gamestate[8]<0){
- nextState.gamestate[8]=nextState.gamestate[8]*-1;
- }
- }
- }
- void updateTree(int index, int move){
- string moveappend = to_string(move);
- string pathappend = to_string(currState.index);
- STATETREE[index].depth=testdepth+1;
- STATETREE[index].movepath=currState.movepath + moveappend;
- STATETREE[index].statepath=currState.statepath + " " + pathappend;
- if(index >= 251250 && index <= 252499){
- didwin = true;
- winState = STATETREE[index];
- cout << "State path: " << winState.statepath << endl;
- cout << "Move path: " << winState.movepath << endl;
- }
- }
- /*
- (STATETREE[n]) % 2 = switch state.
- (STATETREE[n] / 2) % 5 = pool 4 state.
- (STATETREE[n] / 10) % 5 = pool 3 state.
- (STATETREE[n] / 50) % 5 = pool 2 state.
- (STATETREE[n] / 250) % 5 = pool 1 state.
- (STATETREE[n] / 1250) % 4 = cell 4 state.
- (STATETREE[n] / 5000) % 4 = cell 3 state.
- (STATETREE[n] / 20000) % 4 = cell 2 state.
- (STATETREE[n] / 80000) % 4 = cell 1 state.
- */
- int getIndex(state s){
- return s.gamestate[0]*80000 + s.gamestate[1]*20000 + s.gamestate[2]*5000 + s.gamestate[3]*1250 +
- s.gamestate[4]*250 + s.gamestate[5]*50 + s.gamestate[6]*10 + s.gamestate[7]*2 + s.gamestate[8];
- }
- // Lazy to_string patch by user1890351 on stackoverflow
- // Why the function was removed is beyond me, but this should work
- string to_string(int number){
- string number_string = "";
- char ones_char;
- int ones = 0;
- while(true){
- ones = number % 10;
- switch(ones){
- case 0: ones_char = '0'; break;
- case 1: ones_char = '1'; break;
- case 2: ones_char = '2'; break;
- case 3: ones_char = '3'; break;
- case 4: ones_char = '4'; break;
- case 5: ones_char = '5'; break;
- case 6: ones_char = '6'; break;
- case 7: ones_char = '7'; break;
- case 8: ones_char = '8'; break;
- case 9: ones_char = '9'; break;
- }
- number -= ones;
- number_string = ones_char + number_string;
- if(number == 0){
- break;
- }
- number = number/10;
- }
- return number_string;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement