Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Gloamwood Puzzle Solver
- Author: kreychek _AT_ gmail !DOT! com
- */
- #include <iostream>
- #include <conio.h>
- #include <ctime>
- #include <cstdlib>
- #include <fstream>
- #define FPATH "C:\\gloamwood_solution.txt"
- using namespace std;
- void printGrid( int ary[3][3] ) {
- for ( int i = 0; i < 3; i++ ) {
- for ( int j = 0; j < 3; j++ ) {
- cout << ary[i][j] << " ";
- }
- cout << "\n";
- }
- cout << "\n";
- return;
- }
- // matrix manipulation functions (shift rows/cols)
- void sUpMid( int ary[3][3] ) {
- int buff = -1;
- buff = ary[0][1];
- ary[0][1] = ary[1][1];
- ary[1][1] = ary[2][1];
- ary[2][1] = buff;
- return;
- }
- void sDownMid( int ary[3][3] ) {
- int buff = -1;
- buff = ary[2][1];
- ary[2][1] = ary[1][1];
- ary[1][1] = ary[0][1];
- ary[0][1] = buff;
- return;
- }
- void sUpLeft( int ary[3][3] ) {
- int buff = -1;
- buff = ary[0][0];
- ary[0][0] = ary[1][0];
- ary[1][0] = ary[2][0];
- ary[2][0] = buff;
- return;
- }
- void sDownLeft( int ary[3][3] ) {
- int buff = -1;
- buff = ary[2][0];
- ary[2][0] = ary[1][0];
- ary[1][0] = ary[0][0];
- ary[0][0] = buff;
- return;
- }
- void sUpRight( int ary[3][3] ) {
- int buff = -1;
- buff = ary[0][2];
- ary[0][2] = ary[1][2];
- ary[1][2] = ary[2][2];
- ary[2][2] = buff;
- return;
- }
- void sDownRight( int ary[3][3] ) {
- int buff = -1;
- buff = ary[2][2];
- ary[2][2] = ary[1][2];
- ary[1][2] = ary[0][2];
- ary[0][2] = buff;
- return;
- }
- void sLeftTop( int ary[3][3] ) {
- int buff = -1;
- buff = ary[0][0];
- ary[0][0] = ary[0][1];
- ary[0][1] = ary[0][2];
- ary[0][2] = buff;
- return;
- }
- void sRightTop( int ary[3][3] ) {
- int buff = -1;
- buff = ary[0][2];
- ary[0][2] = ary[0][1];
- ary[0][1] = ary[0][0];
- ary[0][0] = buff;
- return;
- }
- void sLeftMid( int ary[3][3] ) {
- int buff = -1;
- buff = ary[1][0];
- ary[1][0] = ary[1][1];
- ary[1][1] = ary[1][2];
- ary[1][2] = buff;
- return;
- }
- void sRightMid( int ary[3][3] ) {
- int buff = -1;
- buff = ary[1][2];
- ary[1][2] = ary[1][1];
- ary[1][1] = ary[1][0];
- ary[1][0] = buff;
- return;
- }
- void sLeftBot( int ary[3][3] ) {
- int buff = -1;
- buff = ary[2][0];
- ary[2][0] = ary[2][1];
- ary[2][1] = ary[2][2];
- ary[2][2] = buff;
- return;
- }
- void sRightBot( int ary[3][3] ) {
- int buff = -1;
- buff = ary[2][2];
- ary[2][2] = ary[2][1];
- ary[2][1] = ary[2][0];
- ary[2][0] = buff;
- return;
- }
- // mimic manipulation to grid as ingame buttons do
- // funcs named after position of button as hours on a clock face
- void twelve( int ary[3][3] ) {
- sUpMid(ary);
- sDownLeft(ary);
- sDownRight(ary);
- }
- void one( int ary[3][3] ) {
- sUpRight(ary);
- sDownMid(ary);
- sDownLeft(ary);
- }
- void two( int ary[3][3] ) {
- sRightTop(ary);
- sLeftMid(ary);
- sLeftBot(ary);
- }
- void three( int ary[3][3] ) {
- sRightMid(ary);
- sLeftTop(ary);
- sLeftBot(ary);
- }
- void four( int ary[3][3] ) {
- sRightBot(ary);
- sLeftMid(ary);
- sLeftTop(ary);
- }
- void five( int ary[3][3] ) {
- sDownRight(ary);
- sUpMid(ary);
- sUpLeft(ary);
- }
- void six( int ary[3][3] ) {
- sDownMid(ary);
- sUpLeft(ary);
- sUpRight(ary);
- }
- void seven( int ary[3][3] ) {
- sDownLeft(ary);
- sUpMid(ary);
- sUpRight(ary);
- }
- void eight( int ary[3][3] ) {
- sLeftBot(ary);
- sRightMid(ary);
- sRightTop(ary);
- }
- void nine( int ary[3][3] ) {
- sLeftMid(ary);
- sRightTop(ary);
- sRightBot(ary);
- }
- void ten( int ary[3][3] ) {
- sLeftTop(ary);
- sRightMid(ary);
- sRightBot(ary);
- }
- void eleven( int ary[3][3] ) {
- sUpLeft(ary);
- sDownMid(ary);
- sDownRight(ary);
- }
- // does the grid match the solution layout?
- int isDone( int ary[3][3], int soln[3][3] ) {
- int match = 1;
- for ( int i = 0; i < 3; i++ ) {
- for ( int j = 0; j < 3; j++ ) {
- if ( ary[i][j] != soln[i][j] )
- match = 0;
- }
- }
- return match;
- }
- // resets grid to original user input
- void initGrid( int ary[3][3], int orig[3][3] ) {
- for ( int i = 0; i < 3; i++ ) {
- for ( int j = 0; j < 3; j++ ) {
- ary[i][j] = orig[i][j];
- }
- }
- }
- // initialize grid to user input
- void getGrid( int ary[3][3], int orig[3][3] ) {
- for ( int i = 0; i < 3; i++ ) {
- for ( int j = 0; j < 3; j++ ) {
- cout << (i * 3) + j + 1 << ": ";
- cin >> ary[i][j];
- orig[i][j] = ary[i][j];
- }
- }
- }
- void clearList( int mList[31] ) {
- for ( int i = 0; i < 31; i++ )
- mList[i] = -1;
- }
- void printList( int mList[31] ) {
- char buffer[10];
- for ( int i = 0; i < 31; i++ ) {
- if ( mList[i] != -1 )
- cout << itoa(mList[i], buffer, 10) << "\n";
- }
- }
- int main( void ) {
- int grid[3][3],
- startGrid[3][3],
- win[3][3] = { 1, 0, 1, 0, 2, 0, 1, 0, 1 }, // when it's like this we're done
- moveCount = 0,
- moveList[31] = { -1 },
- moveLimit = 10,
- toTry = -1;
- char str[10] = "\0"; // buffer for itoa
- //initGrid(grid);
- ofstream myFile;
- cout << "Starting with the upper left, and working your way down as ordered\n"
- << "on a phone keypad, enter the # corresponding to the shields as you\n"
- << "see them currently. Hit ENTER after each entry.\n\n"
- << "0 - small shield\n"
- << "1 - medium shield\n"
- << "2 - the single large shield\n\n"
- << "NOTE: There must be 4 smalls, 4 mediums, and 1 large.\n"
- << "Invalid inputs will result in an infinite loop and require you to\n"
- << "force close the application. (CTRL-C)\n\n";
- getGrid(grid, startGrid);
- cout << "You entered:\n";
- printGrid(grid);
- cout << "Enter move limit. 10 is reccomended, but some layouts may require a\n"
- << "higher value. If the solver takes more than a few seconds, increase\n"
- << "this value by 5 and try again.\n\n";
- cout << "Move limit (1-30): ";
- cin >> moveLimit;
- cout << "\nSolution will be printed to \"" << FPATH << "\"\n\n";
- cout << "This will overwrite the contents of the file or create it if it doesn't exist.\n";
- myFile.open(FPATH, ios::trunc | ios::out);
- // seed random number generator with current time
- srand((unsigned)time(0));
- cout << "Press any key to begin solving.\n";
- getch();
- while ( !isDone(grid, win) ) {
- if ( moveCount == moveLimit ) {
- cout << ".";
- moveCount = 0;
- initGrid(grid, startGrid);
- clearList(moveList);
- myFile.close();
- myFile.open("c:\\gloamwood_solution.txt", ios::trunc | ios::out); // wipe file each time we start over
- }
- // the solving "algorithm"
- toTry = rand() % 12 + 1;
- moveList[moveCount] = toTry;
- switch ( toTry ) {
- case 1:
- one(grid);
- //cout << "1\n";
- myFile << "1\n";
- break;
- case 2:
- two(grid);
- //cout << "2\n";
- myFile << "2\n";
- break;
- case 3:
- three(grid);
- //cout << "3\n";
- myFile << "3\n";
- break;
- case 4:
- four(grid);
- //cout << "4\n";
- myFile << "4\n";
- break;
- case 5:
- five(grid);
- //cout << "5\n";
- myFile << "5\n";
- break;
- case 6:
- six(grid);
- //cout << "6\n";
- myFile << "6\n";
- break;
- case 7:
- seven(grid);
- //cout << "7\n";
- myFile << "7\n";
- break;
- case 8:
- eight(grid);
- //cout << "8\n";
- myFile << "8\n";
- break;
- case 9:
- nine(grid);
- //cout << "9\n";
- myFile << "9\n";
- break;
- case 10:
- ten(grid);
- //cout << "10\n";
- myFile << "10\n";
- break;
- case 11:
- eleven(grid);
- //cout << "11\n";
- myFile << "11\n";
- break;
- case 12:
- twelve(grid);
- //cout << "12\n";
- myFile << "12\n";
- break;
- default: // should never get here
- cout << "Out of range roll!";
- myFile << "Out of range roll!";
- break;
- }
- moveCount++;
- }
- //printGrid(grid);
- cout << "\n";
- printList(moveList);
- cout << "\nSolved! The above sequence is the order to click the buttons in.\n"
- << "The numbers correspond to positions of hours on a clock.\n\n"
- << "Solution can also be found in C:\\gloamwood_solution.txt";
- myFile << "Solved!\n";
- myFile.close();
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement