Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- #include <string.h> // for strcpy()
- #include <conio.h> // for getche()
- #include <windows.h> // for COLOR!
- #define NUM_ROWS 3 // should not be changed for this solution
- #define NUM_COLS 3 // should not be changed for this soultion
- #define PIVOT -1 // used to mark the pivot spot (blank area) on the puzzle
- #define PIVOT_SYMBOL "*" // used to show the pivot location when drawing the board
- // direction codes (part of the slideTile() interface)
- #define SLIDE_UP 1 // pass to slideTile() to trigger UP movement
- #define SLIDE_DOWN 2 // pass to slideTile() to trigger DOWN movement
- #define SLIDE_LEFT 3 // pass to slideTile() to trigger LEFT movement
- #define SLIDE_RIGHT 4 // pass to slideTile() to trigger RIGHT movement
- #define UNSET -1 // used to arbitrarily indicate an undetermined state in a constuct
- #define COLOR_DEFAULT 7
- #define COLOR_RED 12
- #define COLOR_GREEN 10
- // PROTOTYPES
- void InitializeBoard(int[NUM_ROWS][NUM_COLS]);
- void PrintBoard(int[NUM_ROWS][NUM_COLS]);
- bool slideTile(int[NUM_ROWS][NUM_COLS], int);
- void scrambleBoard(int[NUM_ROWS][NUM_COLS]); // depends upon slideTile()
- bool isBoardSolved(int[NUM_ROWS][NUM_COLS]); // indicates if the board is in the SOLVED state
- // DEVELOPMENT EXTRAS
- //void printTheRainbow(); // A little reminder on how to do color with the Windows API.
- int main() {
- // Declarations
- int slidingBoard[NUM_ROWS][NUM_COLS]; // the board that holds the sliding tiles
- char keyStroke = ' '; // holds the user's keystrokes as they come in
- int directionCode = UNSET; // used to build a direction code to be sent to slideTile()
- InitializeBoard(slidingBoard);
- PrintBoard(slidingBoard);
- //scrambleBoard(slidingBoard);
- while (!isBoardSolved(slidingBoard))
- {
- keyStroke = _getch();
- if (keyStroke == 'w')
- directionCode = SLIDE_UP;
- else if (keyStroke == 's')
- directionCode = SLIDE_DOWN;
- else if (keyStroke == 'a')
- directionCode = SLIDE_LEFT;
- else if (keyStroke == 'd')
- directionCode = SLIDE_RIGHT;
- else
- directionCode = UNSET;
- if (slideTile(slidingBoard, directionCode))
- {
- PrintBoard(slidingBoard);
- }
- }
- _getch(); // Exit
- return 0;
- }
- void InitializeBoard(int theBoard[NUM_ROWS][NUM_COLS])
- {
- int counter = 1;
- for (int i = 0; i < NUM_ROWS; i++)
- {
- for (int j = 0; j < NUM_COLS; j++)
- {
- if (i == (NUM_ROWS - 1) && j == (NUM_COLS - 1))
- {
- theBoard[i][j] = PIVOT;
- }
- else
- {
- theBoard[i][j] = counter;
- counter++;
- }
- }
- }
- }
- void PrintBoard(int theBoard[NUM_ROWS][NUM_COLS])
- {
- system("CLS");
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- for (int i = 0; i < NUM_ROWS; i++)
- {
- for (int j = 0; j < NUM_COLS; j++)
- {
- if (theBoard[i][j] == PIVOT)
- {
- cout << setw(3) << PIVOT_SYMBOL;
- }
- else
- {
- if (isBoardSolved(theBoard))
- {
- SetConsoleTextAttribute(hConsole, COLOR_GREEN);
- }
- cout << setw(3) << theBoard[i][j];
- SetConsoleTextAttribute(hConsole, COLOR_DEFAULT);
- }
- }
- cout << endl;
- }
- cout << "\nisBoardSolved(): ";
- if (isBoardSolved(theBoard))
- {
- cout << "true" << endl;
- }
- else
- {
- cout << "false" << endl;
- }
- cout << "\n| w = UP | s = DOWN | a = LEFT | d = RIGHT |" << endl;
- cout << "Which way to slide:";
- }
- bool slideTile(int theBoard[NUM_ROWS][NUM_COLS], int slideDirection)
- {
- for (int i = 0; i < NUM_ROWS; i++)
- {
- for (int j = 0; j < NUM_COLS; j++)
- {
- if (theBoard[i][j] == PIVOT)
- {
- if (slideDirection == SLIDE_UP)
- {
- if (i == NUM_ROWS - 1)
- return false;
- else
- {
- theBoard[i][j] = theBoard[i + 1][j];
- theBoard[i + 1][j] = -1;
- return true;
- }
- }
- else if (slideDirection == SLIDE_DOWN)
- {
- if (i == 0)
- return false;
- else
- {
- theBoard[i][j] = theBoard[i - 1][j];
- theBoard[i - 1][j] = -1;
- return true;
- }
- }
- else if (slideDirection == SLIDE_LEFT)
- {
- if (j == NUM_COLS - 1)
- return false;
- else
- {
- theBoard[i][j] = theBoard[i][j + 1];
- theBoard[i][j + 1]= -1;
- return true;
- }
- }
- else if (slideDirection == SLIDE_RIGHT)
- {
- if (j == 0)
- return false;
- else
- {
- theBoard[i][j] = theBoard[i][j - 1];
- theBoard[i][j - 1] = -1;
- return true;
- }
- }
- }
- }
- }
- }
- void scrambleBoard(int theBoard[NUM_ROWS][NUM_COLS])
- {
- }
- bool isBoardSolved(int amISolved[NUM_ROWS][NUM_COLS])
- {
- int solvedBoard[3][3] = { { 1, 2, 3 },
- { 4, 5, 6 },
- { 7, 8, PIVOT } };
- for (int i = 0; i < NUM_ROWS; i++)
- {
- for (int j = 0; j < NUM_COLS; j++)
- {
- if (amISolved[i][j] != solvedBoard[i][j])
- {
- return false;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement