write a description for this program( please introduce the topic and application
ID: 3918442 • Letter: W
Question
write a description for this program( please introduce the topic and applications, advantages / disadvantages)
#include <time.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <iostream>
class p15 {
public :
void play() {
bool p = true;
std::string a;
while( p ) {
createBrd();
while( !isDone() ) { drawBrd();getMove(); }
drawBrd();
std::cout << " Congratulations! Play again (Y/N)?";
std::cin >> a; if( a != "Y" && a != "y" ) break;
}
}
private:
void createBrd() {
int i = 1; std::vector<int> v;
for( ; i < 16; i++ ) { brd[i - 1] = i; }
brd[15] = 0; x = y = 3;
for( i = 0; i < 1000; i++ ) {
getCandidates( v );
move( v[rand() % v.size()] );
v.clear();
}
}
void move( int d ) {
int t = x + y * 4;
switch( d ) {
case 1: y--; break;
case 2: x++; break;
case 4: y++; break;
case 8: x--;
}
brd[t] = brd[x + y * 4];
brd[x + y * 4] = 0;
}
void getCandidates( std::vector<int>& v ) {
if( x < 3 ) v.push_back( 2 ); if( x > 0 ) v.push_back( 8 );
if( y < 3 ) v.push_back( 4 ); if( y > 0 ) v.push_back( 1 );
}
void drawBrd() {
int r; std::cout << " ";
for( int y = 0; y < 4; y++ ) {
std::cout << "+----+----+----+----+ ";
for( int x = 0; x < 4; x++ ) {
r = brd[x + y * 4];
std::cout << "| ";
if( r < 10 ) std::cout << " ";
if( !r ) std::cout << " ";
else std::cout << r << " ";
}
std::cout << "| ";
}
std::cout << "+----+----+----+----+ ";
}
void getMove() {
std::vector<int> v; getCandidates( v );
std::vector<int> p; getTiles( p, v ); unsigned int i;
while( true ) {
std::cout << " Possible moves: ";
for( i = 0; i < p.size(); i++ ) std::cout << p[i] << " ";
int z; std::cin >> z;
for( i = 0; i < p.size(); i++ )
if( z == p[i] ) { move( v[i] ); return; }
}
}
void getTiles( std::vector<int>& p, std::vector<int>& v ) {
for( unsigned int t = 0; t < v.size(); t++ ) {
int xx = x, yy = y;
switch( v[t] ) {
case 1: yy--; break;
case 2: xx++; break;
case 4: yy++; break;
case 8: xx--;
}
p.push_back( brd[xx + yy * 4] );
}
}
bool isDone() {
for( int i = 0; i < 15; i++ ) {
if( brd[i] != i + 1 ) return false;
}
return true;
}
int brd[16], x, y;
};
int main( int argc, char* argv[] ) {
srand( ( unsigned )time( 0 ) );
p15 p; p.play(); return 0;
}
Explanation / Answer
This is a Rubix 2D game, where we have 4*4 matrix grid with each cell numbered from 1 to 15 and first box left empty.
+----+----+----+----+
| | 1 | 2 | 3 |
+----+----+----+----+
| 4| 5 | 6 | 7 |
+----+----+----+----+
| 8 | 9 | 10 | 11 |
+----+----+----+----+
| 12 | 13 | 14 | 15 |
+----+----+----+----+
We shuffle the matrix such that all numbers in the cell goes to other cells. Like shown below.
+----+----+----+----+
| 7 | 4 | 8 | 15 |
+----+----+----+----+
| 6 | 1 | 12 | 10 |
+----+----+----+----+
| 5 | 3 | 11 | |
+----+----+----+----+
| 14 | 2 | 9 | 13 |
+----+----+----+----+
The puzzle is move the numbers through the empty boxes, such that whole matrix goes to original state of 1-15 shown above. Then the player can win the game.
Application :
Game for Some Hobbyist/Mathematicians who loves to play with numbers
Advantage :
Low on memory, easy to code and entertaintaining game
Disadvantage :
Sometimes solution for the puzzle may get too tedious and may go into many iterations. Difficult to land to solutions
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.