Is it possible to present class as abstract class ?

The code below for 15 puzzle, my task is to present it as the following syntax, but I am confused that is it possible to represent the code in this way below :

1
2
3
4
5
6
7
8
9
10
11
12
13

class p15

{


public:
 virtual void show(unsigned int steps) = 0; 
virtual unsigned int  GetBlock(unsigned int x,unsigned int y) = 0; 
virtual bool MoveBlock(unsigned int x,unsigned int y) = 0; 

};


// code for 15 puzzle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#include <time.h>
#include <vector>
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
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;
}


Can anyone please help me out ?
p15 p; p.play();

once p15 is presented as an abstract class code such as above would no longer be possible as you can't create instances of an abstract class. The way around that would then be inheritance and pointers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include <iostream>
# include <memory>

struct Abstract
{
   virtual void show() const {std::cout << "abstract class \n";}
   virtual ~Abstract(){}
};

struct AbstractDerived : public Abstract
{
    virtual void show() const override final {std::cout << "derived class \n"; }
};

int main()
{
    std::unique_ptr<Abstract> p = std::make_unique<AbstractDerived>();

    p -> show();
    p -> Abstract::show();
}
The abstract base class p15 could be a framework for solving a number puzzle. The logic for solving the puzzle could be built into the base class, and the data representation and display are up to the developers of the derived class.

In other words, the algorithm to solve the puzzle is either encapsulated in the base class or resides outside of the class completely. All the algorithm knows about the inner workings of the puzzle class is that it can call GetBlock, MoveBlock and show.

Maybe I want to store the data for the puzzle in an array of uchars where each nibble represents one of the blocks, and display information to the console. Maybe my friend want to store the puzzle in a 2-dimensional array of shorts and display on a GUI window. The algorithm to solve the problem does not care as long as GetBlock, MoveBlock and show do what is is expected.
Thanks guys, feeling so depressed because the given syntax for the project must be used to make the puzzle game but I can do it only in my way.
Topic archived. No new replies allowed.