Advertisement
Guest User

Rift Gloamwood Puzzle Solver

a guest
Feb 18th, 2011
1,844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.88 KB | None | 0 0
  1. /*
  2.     Gloamwood Puzzle Solver
  3.     Author: kreychek _AT_ gmail !DOT! com
  4. */
  5.  
  6. #include <iostream>
  7. #include <conio.h>
  8. #include <ctime>
  9. #include <cstdlib>
  10. #include <fstream>
  11.  
  12. #define FPATH "C:\\gloamwood_solution.txt"
  13.  
  14. using namespace std;
  15.  
  16. void printGrid( int ary[3][3] ) {
  17.    
  18.     for ( int i = 0; i < 3; i++ ) {
  19.         for ( int j = 0; j < 3; j++ ) {
  20.             cout << ary[i][j] << " ";
  21.         }
  22.         cout << "\n";
  23.     }
  24.  
  25.     cout << "\n";
  26.  
  27.     return;
  28. }
  29.  
  30.  
  31. // matrix manipulation functions (shift rows/cols)
  32. void sUpMid( int ary[3][3] ) {
  33.  
  34.     int buff = -1;
  35.  
  36.     buff = ary[0][1];
  37.     ary[0][1] = ary[1][1];
  38.     ary[1][1] = ary[2][1];
  39.     ary[2][1] = buff;
  40.  
  41.     return;
  42. }
  43.  
  44. void sDownMid( int ary[3][3] ) {
  45.  
  46.     int buff = -1;
  47.  
  48.     buff = ary[2][1];
  49.     ary[2][1] = ary[1][1];
  50.     ary[1][1] = ary[0][1];
  51.     ary[0][1] = buff;
  52.  
  53.     return;
  54. }
  55.  
  56. void sUpLeft( int ary[3][3] ) {
  57.  
  58.     int buff = -1;
  59.  
  60.     buff = ary[0][0];
  61.     ary[0][0] = ary[1][0];
  62.     ary[1][0] = ary[2][0];
  63.     ary[2][0] = buff;
  64.  
  65.     return;
  66. }
  67.  
  68. void sDownLeft( int ary[3][3] ) {
  69.  
  70.     int buff = -1;
  71.  
  72.     buff = ary[2][0];
  73.     ary[2][0] = ary[1][0];
  74.     ary[1][0] = ary[0][0];
  75.     ary[0][0] = buff;
  76.  
  77.     return;
  78. }
  79.  
  80. void sUpRight( int ary[3][3] ) {
  81.  
  82.     int buff = -1;
  83.  
  84.     buff = ary[0][2];
  85.     ary[0][2] = ary[1][2];
  86.     ary[1][2] = ary[2][2];
  87.     ary[2][2] = buff;
  88.  
  89.     return;
  90. }
  91.  
  92. void sDownRight( int ary[3][3] ) {
  93.  
  94.     int buff = -1;
  95.  
  96.     buff = ary[2][2];
  97.     ary[2][2] = ary[1][2];
  98.     ary[1][2] = ary[0][2];
  99.     ary[0][2] = buff;
  100.  
  101.     return;
  102. }
  103.  
  104. void sLeftTop( int ary[3][3] ) {
  105.  
  106.     int buff = -1;
  107.  
  108.     buff = ary[0][0];
  109.     ary[0][0] = ary[0][1];
  110.     ary[0][1] = ary[0][2];
  111.     ary[0][2] = buff;
  112.  
  113.     return;
  114. }
  115.  
  116. void sRightTop( int ary[3][3] ) {
  117.  
  118.     int buff = -1;
  119.  
  120.     buff = ary[0][2];
  121.     ary[0][2] = ary[0][1];
  122.     ary[0][1] = ary[0][0];
  123.     ary[0][0] = buff;
  124.  
  125.     return;
  126. }
  127.  
  128. void sLeftMid( int ary[3][3] ) {
  129.  
  130.     int buff = -1;
  131.  
  132.     buff = ary[1][0];
  133.     ary[1][0] = ary[1][1];
  134.     ary[1][1] = ary[1][2];
  135.     ary[1][2] = buff;
  136.  
  137.     return;
  138. }
  139.  
  140. void sRightMid( int ary[3][3] ) {
  141.  
  142.     int buff = -1;
  143.  
  144.     buff = ary[1][2];
  145.     ary[1][2] = ary[1][1];
  146.     ary[1][1] = ary[1][0];
  147.     ary[1][0] = buff;
  148.  
  149.     return;
  150. }
  151.  
  152. void sLeftBot( int ary[3][3] ) {
  153.  
  154.     int buff = -1;
  155.  
  156.     buff = ary[2][0];
  157.     ary[2][0] = ary[2][1];
  158.     ary[2][1] = ary[2][2];
  159.     ary[2][2] = buff;
  160.  
  161.     return;
  162. }
  163.  
  164. void sRightBot( int ary[3][3] ) {
  165.  
  166.     int buff = -1;
  167.  
  168.     buff = ary[2][2];
  169.     ary[2][2] = ary[2][1];
  170.     ary[2][1] = ary[2][0];
  171.     ary[2][0] = buff;
  172.  
  173.     return;
  174. }
  175.  
  176. // mimic manipulation to grid as ingame buttons do
  177. // funcs named after position of button as hours on a clock face
  178. void twelve( int ary[3][3] ) {
  179.     sUpMid(ary);
  180.     sDownLeft(ary);
  181.     sDownRight(ary);
  182. }
  183.  
  184. void one( int ary[3][3] ) {
  185.     sUpRight(ary);
  186.     sDownMid(ary);
  187.     sDownLeft(ary);
  188. }
  189.  
  190. void two( int ary[3][3] ) {
  191.     sRightTop(ary);
  192.     sLeftMid(ary);
  193.     sLeftBot(ary);
  194. }
  195.  
  196. void three( int ary[3][3] ) {
  197.     sRightMid(ary);
  198.     sLeftTop(ary);
  199.     sLeftBot(ary);
  200. }
  201.  
  202. void four( int ary[3][3] ) {
  203.     sRightBot(ary);
  204.     sLeftMid(ary);
  205.     sLeftTop(ary);
  206. }
  207.  
  208. void five( int ary[3][3] ) {
  209.     sDownRight(ary);
  210.     sUpMid(ary);
  211.     sUpLeft(ary);
  212. }
  213.  
  214. void six( int ary[3][3] ) {
  215.     sDownMid(ary);
  216.     sUpLeft(ary);
  217.     sUpRight(ary);
  218. }
  219.  
  220. void seven( int ary[3][3] ) {
  221.     sDownLeft(ary);
  222.     sUpMid(ary);
  223.     sUpRight(ary);
  224. }
  225.  
  226. void eight( int ary[3][3] ) {
  227.     sLeftBot(ary);
  228.     sRightMid(ary);
  229.     sRightTop(ary);
  230. }
  231.  
  232. void nine( int ary[3][3] ) {
  233.     sLeftMid(ary);
  234.     sRightTop(ary);
  235.     sRightBot(ary);
  236. }
  237.  
  238. void ten( int ary[3][3] ) {
  239.     sLeftTop(ary);
  240.     sRightMid(ary);
  241.     sRightBot(ary);
  242. }
  243.  
  244. void eleven( int ary[3][3] ) {
  245.     sUpLeft(ary);
  246.     sDownMid(ary);
  247.     sDownRight(ary);
  248. }
  249.  
  250. // does the grid match the solution layout?
  251. int isDone( int ary[3][3], int soln[3][3] ) {
  252.     int match = 1;
  253.  
  254.     for ( int i = 0; i < 3; i++ ) {
  255.         for ( int j = 0; j < 3; j++ ) {
  256.             if ( ary[i][j] != soln[i][j] )
  257.                 match = 0;
  258.         }
  259.     }
  260.  
  261.     return match;
  262. }
  263.  
  264. // resets grid to original user input
  265. void initGrid( int ary[3][3], int orig[3][3] ) {
  266.    
  267.     for ( int i = 0; i < 3; i++ ) {
  268.         for ( int j = 0; j < 3; j++ ) {
  269.             ary[i][j] = orig[i][j];
  270.         }
  271.     }
  272.    
  273. }
  274.  
  275.  
  276. // initialize grid to user input
  277. void getGrid( int ary[3][3], int orig[3][3] ) {
  278.  
  279.     for ( int i = 0; i < 3; i++ ) {
  280.         for ( int j = 0; j < 3; j++ ) {
  281.             cout << (i * 3) + j + 1 << ": ";
  282.             cin >> ary[i][j];
  283.             orig[i][j] = ary[i][j];
  284.         }
  285.     }
  286. }
  287.  
  288. void clearList( int mList[31] ) {
  289.     for ( int i = 0; i < 31; i++ )
  290.         mList[i] = -1;
  291. }
  292.  
  293. void printList( int mList[31] ) {
  294.     char buffer[10];
  295.  
  296.     for ( int i = 0; i < 31; i++ ) {
  297.         if ( mList[i] != -1 )
  298.             cout << itoa(mList[i], buffer, 10) << "\n";
  299.     }
  300. }
  301.  
  302. int main( void ) {
  303.  
  304.     int grid[3][3],
  305.         startGrid[3][3],
  306.         win[3][3] = { 1, 0, 1, 0, 2, 0, 1, 0, 1 }// when it's like this we're done
  307.         moveCount = 0,
  308.         moveList[31] = { -1 },
  309.         moveLimit = 10,
  310.         toTry = -1;
  311.     char str[10] = "\0";    // buffer for itoa
  312.  
  313.     //initGrid(grid);
  314.     ofstream myFile;
  315.  
  316.     cout << "Starting with the upper left, and working your way down as ordered\n"
  317.          << "on a phone keypad, enter the # corresponding to the shields as you\n"
  318.          << "see them currently. Hit ENTER after each entry.\n\n"
  319.          << "0 - small shield\n"
  320.          << "1 - medium shield\n"
  321.          << "2 - the single large shield\n\n"
  322.          << "NOTE: There must be 4 smalls, 4 mediums, and 1 large.\n"
  323.          << "Invalid inputs will result in an infinite loop and require you to\n"
  324.          << "force close the application. (CTRL-C)\n\n";
  325.  
  326.     getGrid(grid, startGrid);
  327.  
  328.     cout << "You entered:\n";
  329.     printGrid(grid);
  330.  
  331.     cout << "Enter move limit. 10 is reccomended, but some layouts may require a\n"
  332.          << "higher value. If the solver takes more than a few seconds, increase\n"
  333.          << "this value by 5 and try again.\n\n";
  334.  
  335.     cout << "Move limit (1-30): ";
  336.     cin >> moveLimit;
  337.  
  338.     cout << "\nSolution will be printed to \"" << FPATH << "\"\n\n";
  339.     cout << "This will overwrite the contents of the file or create it if it doesn't exist.\n";
  340.     myFile.open(FPATH, ios::trunc | ios::out);
  341.  
  342.     // seed random number generator with current time
  343.     srand((unsigned)time(0));
  344.  
  345.     cout << "Press any key to begin solving.\n";
  346.  
  347.     getch();
  348.  
  349.     while ( !isDone(grid, win) ) {
  350.  
  351.         if ( moveCount == moveLimit ) {
  352.             cout << ".";
  353.             moveCount = 0;
  354.             initGrid(grid, startGrid);
  355.             clearList(moveList);
  356.             myFile.close();
  357.             myFile.open("c:\\gloamwood_solution.txt", ios::trunc | ios::out);   // wipe file each time we start over
  358.         }
  359.        
  360.         // the solving "algorithm"
  361.  
  362.         toTry = rand() % 12 + 1;
  363.  
  364.         moveList[moveCount] = toTry;
  365.  
  366.         switch ( toTry ) {
  367.             case 1:
  368.                 one(grid);
  369.                 //cout << "1\n";
  370.                 myFile << "1\n";
  371.                 break;
  372.             case 2:
  373.                 two(grid);
  374.                 //cout << "2\n";
  375.                 myFile << "2\n";
  376.                 break;
  377.             case 3:
  378.                 three(grid);
  379.                 //cout << "3\n";
  380.                 myFile << "3\n";
  381.                 break;
  382.             case 4:
  383.                 four(grid);
  384.                 //cout << "4\n";
  385.                 myFile << "4\n";
  386.                 break;
  387.             case 5:
  388.                 five(grid);
  389.                 //cout << "5\n";
  390.                 myFile << "5\n";
  391.                 break;
  392.             case 6:
  393.                 six(grid);
  394.                 //cout << "6\n";
  395.                 myFile << "6\n";
  396.                 break;
  397.             case 7:
  398.                 seven(grid);
  399.                 //cout << "7\n";
  400.                  myFile << "7\n";
  401.                 break;
  402.             case 8:
  403.                 eight(grid);
  404.                 //cout << "8\n";
  405.                 myFile << "8\n";
  406.                 break;
  407.             case 9:
  408.                 nine(grid);
  409.                 //cout << "9\n";
  410.                 myFile << "9\n";
  411.                 break;
  412.             case 10:
  413.                 ten(grid);
  414.                 //cout << "10\n";
  415.                 myFile << "10\n";
  416.                 break;
  417.             case 11:
  418.                 eleven(grid);
  419.                 //cout << "11\n";
  420.                 myFile << "11\n";
  421.                 break;
  422.             case 12:
  423.                 twelve(grid);
  424.                 //cout << "12\n";
  425.                 myFile << "12\n";
  426.                 break;
  427.             default:    // should never get here
  428.                 cout << "Out of range roll!";
  429.                 myFile << "Out of range roll!";
  430.                 break;
  431.         }
  432.         moveCount++;
  433.     }
  434.  
  435.     //printGrid(grid);
  436.  
  437.     cout << "\n";
  438.     printList(moveList);
  439.  
  440.     cout << "\nSolved! The above sequence is the order to click the buttons in.\n"
  441.          << "The numbers correspond to positions of hours on a clock.\n\n"
  442.          << "Solution can also be found in C:\\gloamwood_solution.txt";
  443.     myFile << "Solved!\n";
  444.  
  445.     myFile.close();
  446.  
  447.     getch();
  448.  
  449.     return 0;
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement