Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When writing this program, , , YOU MUST DESIGN AN OBJECT THAT REPRESENTS THE SLI

ID: 3580548 • Letter: W

Question

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.

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. Ass nment 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. int Sliding Puzzle the Board 31 [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 amay to determine the board's current state Methods Sliding Puzzle Sliding Puzzle i 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 Init ialize Board void S lie Puzzle Init ding ia, li Description: This method will manually populate the board with the game's starting configuration Initially, the board should look like this the represents the blank space on the board

Explanation / Answer

#include<iostream>
#include<random>
#include <algorithm> // std::shuffle
#include <array> // std::array
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
using namespace std;

class SlidingPuzzle
{
char theBoard[3][3];
int posi,posj;

SlidingPuzzle()
{
char ch = NULL;
int dir = 1;
initializeBoard();
PrintBoard();
cout<<"Is Game Board solved ?"<<isBoardSolved()<<" Press N for new game :";
cin>>ch;
if(ch == 'N')
{
ScrambleBoard();
while(ch == 'C')
{
cout<<" <1> UP | <2> DOWN | <3> LEFT | <4> RIGHT Which way to slide ? "
cin>>dir;
bool op = SlideTile(dir);
PrintBoard();
cout<<"Is Game Board solved ?"<<isBoardSolved()<<" Press C to continue :";
cin>>ch;
}
}
}

void initializeBoard()
{
theBoard={'1','2','3','4','5','6','7','8','9','*'};
posi = posj = 2;
}

bool isBoardSolved()
{
bool status = false;
for(int i=0; i<3 ;i++)
for(int j=0; j<3 ;j++)
if(theBoard[i][j] != chk)
status = false;
return status;
}

void PrintBoard()
{
clrscr();
cout<<" Game Board -------------- ";
for(int i=0; i<3 ;i++)
{
for(int j=0; j<3 ;j++)
cout<<theBoard[i][j]<<" ";
cout<<endl;
}
}

bool SlideTile(int dir)
{
char temp = NULL;
bool status = false;

switch(dir)
{
case 1: //move UP
if((posi-1 == 0) | (posi-1 == 1) | (posi-1 == 2))
{
temp = theBoard[posi-1,j];
theBoard[posi-1,j] = theBoard[posi,posj];
theBoard[posi,posj]; = temp;
posi = posi-1;
status = true;
}
break;
case 2: //move DOWN
if((posi+1 == 0) | (posi+1 == 1) | (posi+1 == 2))
{
temp = theBoard[posi+1,j];
theBoard[posi+1,j] = theBoard[posi,posj];
theBoard[posi,posj]; = temp;
posi = posi+1;
status = true;
}
break;
case 3: //move LEFT
if((posj-1 == 0) | (posj-1 == 1) | (posj-1 == 2))
{
temp = theBoard[posi,j-1];
theBoard[posi,j-1] = theBoard[posi,posj];
theBoard[posi,posj]; = temp;
posj = posj-1;
status = true;
}
break;
case 4: //move RIGHT
if((posj+1 == 0) | (posj+1 == 1) | (posj+1 == 2))
{
temp = theBoard[posi,j+1];
theBoard[posi,j+1] = theBoard[posi,posj];
theBoard[posi,posj]; = temp;
posj = posj+1;
status = true;
}
break;
}
return status;
}

void ScrambleBoard()
{
int i=0;
char list[10];
std::array<char,10> shufflelist {'1','2','3','4','5','6','7','8','9','*'};

// obtain a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
shuffle (shufflelist.begin(), shufflelist.end(), std::default_random_engine(seed));
for (char& x: shufflelist)
{
list[n] = x;
n++;
}
n=0;
for(int i=0; i<3 ;i++)
for(int j=0; j<3 ;j++)
{
theBoard[i][j] = list[n];
if(list[n] == '*')
{
posi = i;
posj = j;
}
n++;
}
}

}

int main()
{
SlidingPuzzle game = new SlidingPuzzle();
return (0);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote