Sliding tile puzzle simulator(OBJECT ORIENTED PARADIGM) instructor. Mark Dencier
ID: 3691885 • Letter: S
Question
Sliding tile puzzle simulator(OBJECT ORIENTED PARADIGM) instructor. Mark Dencier 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. Todo 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 Sliding Puzzle::theBoardl3] [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 S1idingPuzzle::SlidingPuzzleO: 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 InitializeBoardO void SlidingPuzzle::InitializeBoardO 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 7 8 *Explanation / Answer
/*
* SlidingPuzzle.cpp
*
* Created on: Apr 20, 2016
* Author: Satish-Ravi
*/
#include "SlidingPuzzle.h"
SlidingPuzzle::SlidingPuzzle() {
initializeBoard();
}
SlidingPuzzle::~SlidingPuzzle() {
// TODO Auto-generated destructor stub
}
void SlidingPuzzle::initializeBoard() {
// TODO: Use random numbers to generate random board
curX = 2;
curY = 2;
int k = 8;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
//0 indicates *
theboard[i][j] = k--;
}
}
bool SlidingPuzzle::isBoardSolved() {
int k = 1;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (i == 2 && j == 2)
break;
if (theboard[i][j] != k)
return false;
k++;
}
return true;
}
bool SlidingPuzzle::slideTile(int direction) {
bool validMove = false;
int tmpX = curX;
int tmpY = curY;
if (direction == DOWN) {
if (curX + 1 >= 0) {
curX++;
validMove = true;
} else
return false;
} else if (direction == UP) {
if (curX - 1 <= 2) {
curX--;
validMove = true;
} else
return false;
} else if (direction == LEFT) {
if (curY - 1 <= 2) {
curY--;
validMove = true;
} else
return false;
} else if (direction == RIGHT) {
if (curY + 1 <= 2) {
curY++;
validMove = true;
} else
return false;
}
if(validMove) {
int tmp = theboard[curX][curY];
theboard[curX][curY] = 0;
theboard[tmpX][tmpY] = tmp;
}
return validMove;
}
void SlidingPuzzle::printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (theboard[i][j] == 0)
cout << "*" << " ";
else
cout << theboard[i][j] << " ";
}
cout << endl;
}
}
int main() {
SlidingPuzzle sp = SlidingPuzzle();
int dir = 0;
bool solved = false;
while (true) {
sp.printBoard();
solved = sp.isBoardSolved();
cout << "Is Board Solved: " << (solved ? "True" : "False") << endl;
if (solved)
break;
cout << " 1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN" << endl;
cout << "Which way to slide:" << endl;
cin >> dir;
if (!sp.slideTile(dir))
cout << "Can not move in this direction" << endl;
}
return 0;
}
/*
* SlidingPuzzle.h
*
* Created on: Apr 20, 2016
* Author: Satish-Ravi
*/
#ifndef SLIDINGPUZZLE_H_
#define SLIDINGPUZZLE_H_
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
#include <iostream>
using namespace std;
class SlidingPuzzle {
private:
int curX;
int curY;
int theboard[3][3];
void initializeBoard();
public:
SlidingPuzzle();
virtual ~SlidingPuzzle();
bool isBoardSolved();
bool slideTile(int);
void printBoard();
};
#endif /* SLIDINGPUZZLE_H_ */
--output-----------
8 7 6
5 4 3
2 1 *
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
1
8 7 6
5 4 3
2 * 1
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
3
8 7 6
5 * 3
2 4 1
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
2
8 7 6
5 3 *
2 4 1
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
4
8 7 6
5 3 1
2 4 *
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
2
Can not move in this direction
8 7 6
5 3 1
2 4 *
Is Board Solved: False
1 -> LEFT : 2-> RIGHT : 3-> UP : 4 -> DOWN
Which way to slide:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.