Introduction In this assignment, you will be writing a C++ command-line program
ID: 3530010 • Letter: I
Question
Introduction
In this assignment, you will be writing a C++ command-line program that simulates a 3x3 sliding puzzle game. In a sliding puzzle game, 8 tiles with the numerals 1-8 are placed onto a 3x3 square grid. One of the positions is left open for tiles coming from the top, bottom, left, or right (depending on the puzzle configuration). The goal of the game is to slide the tiles around so they appear in order on the puzzle board. When writing this program, you should adhere to the procedural programming paradigm.
Assignment
When completing this program, it would be useful to write a number of functions that perform elementary operations on the board. Once these functions have been written and tested, you can proceed with writing driver control logic that takes the user's input and frames an appropriate call to a function to handle the move.
We should conceptualize the board as a 3x3 two-dimensional array.
int puzzleBoard[3][3];
By writing the following functions, you can create a framework for making moves on the board.
void initializeBoard(int[3][3]);
Description: This function takes the board as a single 3x3 integer array. This function should fill the board with values that represent the tiles on the sliding puzzle. Initially the puzzle should look like this:
1 2 3
4 5 6
7 8 *<=== the * represents the blank space on the board
void printBoard(int[3][3]);
Description: This function takes the board as its only argument. It displays the board on the screen. Initially, I suggest you write this function to give a quick and dirty display so you can move onto coding the core logic of the program. Later, you can come back and upgrade the function to display grid-work and color (see the hints section to learn how to change the console display color).
bool isBoardSolved(int[3][3]);
Description: This function takes the board and determines if it is in the solved state. The solved state looks exactly like the initialization state where the numbers read in order and the empty tile is in the lower-right corner.After the function determines if the board is in the solved state, a boolean value reflecting this should be returned. Later, your driver code will use this value to determine when the puzzle has been solved. You may want to upgrade your return value to an integer if you decide to have additional reporting codes returned from this function. For example, it would help during design to have a return value that indicates if the board has reached a corrupted state (i.e. there are two 5s on the board, an extra empty space was created, etc...)
void slideTile(int[3][3], int);
Description: This function takes two arguments and will attempt to slide a single tile in a single direction on the board. The first argument is the board. This is needed for obvious reasons. The second argument is a "direction code". I suggest coming up with a number scheme to label to four different directions in which the player can move (UP, DOWN, LEFT, RIGHT). The function will take the move direction and first determine if a tile can be slid in that direction. If the move isn't possible at this time, the board should remain unchanged. However, if the move is possible, you should exchange the values on the faces of the two involved tiles to reflect the move.
void scrambleBoard(int[3][3]);
Description: This function takes a board and makes a series of random moves. Write this function after you have written slideTile() and just have it use a random number generator to send a few thousand random moves at the board from the initialized state.
Hints
The following function demonstrates color printing. You can compile and play with the code to learn the basics of color. Keep in mind that you must include the Windows API in order for this to work.
#include <windows.h>
void printTheRainbow();
void printTheRainbow() {
int currentColor = 7;
// get the handle for the console
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// exhaustively print the colors
cout << left;
for(currentColor = 0; currentColor <= 255; currentColor++) {
SetConsoleTextAttribute(hConsole, currentColor);
cout << setw(5) << currentColor
<< "Look at the pretty COLORS!" << endl;
}
}
#1
When the game begins, the board should appear in the solved state.
#2
Have the user press any key to begin scrambling the tiles. Remember, you must make actual moves on the board. A pure random arrangement of the tiles will result in half of the board states being solvable and the other half of the states being unsolvable.
#3
Provide the user with an intuitive interface for moving the tiles. When a move is made, trigger the appropriate functions, clear the screen, and re-draw the board to reflect the new state.
#4
Remember, the asterisk (*) represents the empty tile. To achieve this state, the user pressed "D" to trigger the "2" to move to the right. The empty space does not slide, the numeric tiles slide.
#5
As tiles move into the correct positions, have their face display in a different color. When the user solves the puzzle, the isSolved() function should return true.
Explanation / Answer
#include #include #include #include #include using namespace std; // To use the arrow keys instead of letters #define UP 72 #define LEFT 75 #define RIGHT 77 #define DOWN 80 int solvedBoard[10][10]; void printBoard(int board[][10], int); void initializeBoard(int board[][10], int); void slideTile(int board[][10],int move,int); bool isBoardSolved(int board[][10],int); void scrambleBoard(int board[][10],int); void WaitKey(); int main() { int amount=0; do { system("CLS"); // Best NOT to use system calls, but I'll use for now. Find a better solution // Just to drop request a bit down and over, for looks. coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.