How to resolve the algorithm 15 puzzle game step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm 15 puzzle game step by step in the C++ programming language
Table of Contents
Problem Statement
Implement the Fifteen Puzzle Game.
The 15-puzzle is also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 15 puzzle game step by step in the C++ programming language
The provided code is a C++ implementation of the classic 15-puzzle game. The game is played on a 4x4 grid, with 15 numbered tiles and one empty space. The objective of the game is to slide the tiles around until they are arranged in numerical order, with the empty space in the bottom-right corner.
The code begins by including several standard libraries and defining a class called p15
that contains the game logic. The p15
class has several public and private member functions, which are described below:
play()
: This function is the main entry point for the game. It initializes the game board, repeatedly prompts the user for moves, and checks for a win condition.createBrd()
: This function initializes the game board with the tiles arranged in a random order. It also sets the position of the empty space (x, y).move(int d)
: This function moves the empty space in the specified direction (d), by moving the tile to the left, right, up, or down.getCandidates(std::vector<int>& v)
: This function gets a list of possible moves for the empty space based on its current position.drawBrd()
: This function draws the game board to the console.getMove()
: This function prompts the user for a move and checks if it is valid.getTiles(std::vector<int>& p, std::vector<int>& v)
: This function gets a list of tiles that can be moved into the empty space based on the given list of possible moves (v).isDone()
: This function checks if the game has been won by checking if all the tiles are in the correct order.
In the main
function, the game is initialized and the play()
function is called to start the game. The game runs until the user wins or quits.
Source code in the cpp programming language
#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 << "\n\nCongratulations!\nPlay 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 << "\n\n";
for( int y = 0; y < 4; y++ ) {
std::cout << "+----+----+----+----+\n";
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 << "|\n";
}
std::cout << "+----+----+----+----+\n";
}
void getMove() {
std::vector<int> v; getCandidates( v );
std::vector<int> p; getTiles( p, v ); unsigned int i;
while( true ) {
std::cout << "\nPossible 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;
}
You may also check:How to resolve the algorithm Mutual recursion step by step in the PHP programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Kotlin programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Wren programming language
You may also check:How to resolve the algorithm Jaro-Winkler distance step by step in the J programming language