Project 2c++ Introduction In this assignment, you will be writing a C++ command-
ID: 3680449 • Letter: P
Question
Project 2c++
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 object-oriented programming paradigm.
Assignment
When writing this program, you must design an object that represents the sliding puzzle board. To do this, you must consider the attributes and methods needed by the board to respond to a driver that contains the core logic of the game.
We should begin with a preliminary run-down of the object's features.
Attributes
int SlidingPuzzle::theBoard[3][3];
Description: The primary attribute of the object represents the data on the board. The board array contains the digits 1-8 and the asterisk (*) to represent the open position on the puzzle. When making moves, the data in this array will change to reflect the new state of the board. When the object displays itself on the screen, we will use this array to determine the board's current state.
Methods
SlidingPuzzle::SlidingPuzzle();
Description: This is the constructor method for the object. It will automatically execute once the board has been instantiated in the driver. Upon creation, it would be wise to immediately populate the board with valid values for all of the tiles. You should be able to trigger this with a single call to InitializeBoard().
void SlidingPuzzle::InitializeBoard();
Description: This method will manually populate the board with the game's starting configuration. Initially, the board should look like this:
1 2 3
4 5 6
7 8 * <=== the * represents the blank space on the board
bool SlidingPuzzle::isBoardSolved();
Description: This method examines the board to determine if it is in the solved state. It returns a boolean value reflecting this determination.
bool SlidingPuzzle::slideTile(int);
Description: This method takes a single argument indicating the direction to slide a tile. A good approach would be to set up numerical codes representing the four possible movement directions (UP, DOWN, LEFT, and RIGHT). When the method is called, movement in the indicated direction should take place by exchanging the values in the appropriate tiles. If the move is not possible, the state of the board should remain unchanged. A boolean value should be returned reflecting whether or not the board has been mutated.
void SlidingPuzzle::scrambleBoard();
Description: This method will use the pseudo-random number generator to make an arbitrary number of random moves on the board (I suggest 10,000+ moves to thoroughly confuscate the board). Keep in mind that the seeding of the random number generator should take place in the driver code (not the object's constructor). Also keep in mind that randomly placing the pieces on the board will result in a situation where half of the time the puzzle can be solved and the other half of the time a solution is impossible.
void SlidingPuzzle::PrintBoard();
Description: This method will display the board on the console as a 3x3 grid. I suggest writing a "quick and dirty" version of this method initially so you can look at the maneuvers as they take place on the board during development. When the core logic of the program is done, you should revisit this method and upgrade it to support console color.
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;
}
}
sample screenshots:
When the game begins, the board should appear in the solved state.
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.
Provide the user with an intuitive interface for moving the tiles. When a move is made, trigger the appropriate methods, clear the screen, and re-draw the board to reflect the new state.
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.
To get here, the user pressed "S" to trigger a downward movement of the "4".
As the tiles move into the correct positions, have their face display in a different color.
When the user solves the puzzle, the isBoardSolved() method should return true. You can display a victory message if you would like or simply allow the user to continue moving tiles around from the solved state.
CAUserslhcc Desktop SlidingPuzzleNine.exe isBoardSolvedExplanation / Answer
#include <iostream>
#include <Windows>
#include <conio.h>
#include <time.h>
#include <iomanip>
#include<math.h>
using namespace std;
void initializeBoard(int board[3][3]);
void printBoard(int board[3][3]);
bool isBoardSolved(int[3][3]);
void slideTile(int board[3][3],int move);
void scrambleBoard(int[3][3]);
int main()
{
int puzzleBoard[3][3];
char input;
bool invalid = false;
initializeBoard(puzzleBoard);
printBoard(puzzleBoard);
cout << boolalpha;
cout<<"isBoardSolved(): "<<isBoardSolved(puzzleBoard)<<endl;
cout<<"Press enter to begin"<<endl;
cin.get();
cout<<"Scrambling the game tiles..."<<endl;
scrambleBoard(puzzleBoard);
cout<<"Scrambling complete, press enter to begin solving. "<<endl;
cout<<" Good Luck! "<<endl;
cin.get();
system("CLS");
printBoard(puzzleBoard);
cout<<endl<<endl;
cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl;
cout<<"Input: ";
while(!isBoardSolved(puzzleBoard))
{
input = getch();
system("CLS");
// selecting the game controls
switch(toupper(input))
{
case 'W':
slideTile(puzzleBoard,2);
break;
case 'A':
slideTile(puzzleBoard,0);
break;
case 'S':
slideTile(puzzleBoard,3);
break;
case 'D':
slideTile(puzzleBoard,1);
break;
default:
invalid = true;
}
printBoard(puzzleBoard);
cout<<endl<<endl;
cout<<"[W - Up] [S - Down] [A - Left] [D - Right]"<<endl;
if(invalid)
{
cout<<"Invalid Input - ["<<input<<"]";
invalid = false;
}
else
cout<<"Last Input: "<<input;
}
cout<<endl;
cout<<"Congratulations: BOARD SOLVED"<<endl;
system("PAUSE");
return 0;
}
void printBoard(int board[3][3])
{
// handle hConsole;
// hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
if(board[row][column] == 0)
{
// setting colors
SetConsoleTextAttribute(hConsole, 6);
cout<<" *";
}
else
{
// if the number is in the right position
if(board[row][column] == ((row*3)+(column+1)))
SetConsoleTextAttribute(hConsole, 9);
else
// if the number is not in the right position
SetConsoleTextAttribute(hConsole, 13);
cout<<" "<<board[row][column];
}
}
cout<<endl;
}
SetConsoleTextAttribute(hConsole, 12);
}
void initializeBoard(int board[3][3])
{
int i = 1;
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
board[row][column] = i;
i++;
}
board[2][2] = 0;
}
}
void slideTile(int board[3][3],int move)
{
int emptyRow;
int emptyColum;
int emptySpace[2];
bool legalMoves[4] = {1,1,1,1};
for(int row = 0; row < 3; row++)
{
for(int column = 0; column < 3; column++)
{
if(board[row][column] == 0)
{
emptyRow = row;
emptyColum = column;
}
}
}
if(emptyRow + 1 > 2)
legalMoves[2] = false;
else if(emptyRow - 1 < 0)
legalMoves[3] = false;
if(emptyColum - 1 < 0)
legalMoves[1] = false;
else if(emptyColum + 1 > 2)
legalMoves[0] = false;
switch(move)
{
case 0:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow][emptyColum + 1];
board[emptyRow][emptyColum + 1] = 0;
emptyColum = emptyColum+1;
}
break;
case 1:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow][emptyColum - 1];
board[emptyRow][emptyColum- 1] = 0;
emptyColum = emptyColum-1;
}
break;
case 2:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow+1][emptyColum];
board[emptyRow+1][emptyColum] = 0;
emptyRow = emptyRow+1;
}
break;
case 3:
if(legalMoves[move])
{
board[emptyRow][emptyColum] = board[emptyRow-1][emptyColum];
board[emptyRow-1][emptyColum] = 0;
emptyRow = emptyRow-1;
}
break;
}
}
void scrambleBoard(int board[3][3])
{
srand ( time(NULL) );
int move;
while(isBoardSolved(board))
{
for(int i = 0; i < 9000; i++)
{
move = rand() % 5;
slideTile(board,move);
}
}
}
bool isBoardSolved(int board[3][3])
{
int solvedBoard[3][3] = {{1,2,3},{4,5,6},{7,8,0}};
bool boardSolved = true;
int row = 0;
int col = 0;
while(boardSolved && row<=2)
{
if(solvedBoard[row][col] == board[row][col])
{
col++;
if(col >= 3)
{
row++;
col = 0;
}
}
else
boardSolved = false;
}
return boardSolved;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.