Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I started to develop a starter code for a sliding puzzle assignment but now am not sure how to format it to meet the needs of this assignment. If anyone is able to give me insight on how to fix my code up that would be appreciated.

Assignment:
The Rules
Each 8-puzzle has tiles labeled 1-8 and one empty space denoted by a 0.
The commands given signify where you should attempt to move the empty space in relation to its current position.
The only moves you are allowed to make are swapping the empty space with an adjacent tile to it.
If asked to make an impossible swap (ie. move the blank tile out of the grid bounds), don't terminate the program, simply ignore that command.
The Assignment
For this assignment, we are asking you to take an 8-puzzle and follow a sequence of movement commands UP, DOWN, LEFT, and RIGHT. At the end of the sequence, you should check whether the puzzle has been solved.

Input
As input you will be given some initial board state in the form of a sequence of numbers where 0 corresponds to the "blank" tile. The example below would be the form of the "initial state" example above. After that will be a sequence of commands "UP", "DOWN", "LEFT", and "RIGHT" represented by characters on a single line separated by spaces. (The example command sequence below has no connection to the example graphic)

1 8 2 0 4 3 7 6 5
D R R R U L
Your Task
After storing the initial board state, you will perform the commands given (so long as they are allowed, as specified in the rules section). Run through all the commands, then check whether the puzzle is solved correctly.

Output
Your output should be Solution is good! if the puzzle is in solved order as specified above, and Wrong solution! if the puzzle is not in the "goal state" specified above.

What I have tried:

#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;
}
Posted
Updated 14-Nov-19 19:12pm
Comments
Stefan_Lang 15-Nov-19 3:36am    
The required output is either "Solution is good!" or "Wrong solution!" as per the requirements. No special formatting required. So, what is your problem, really?
Nick McCaffery 15-Nov-19 13:49pm    
Oh ok so this code is good enough and will just have to add that output?
Stefan_Lang 20-Nov-19 3:07am    
You tell me. It's your task to write that program. If you wrote it, you should know what it currently does or doesn't do. If you didn't, you failed the assignment from the start.

You have been given this assignment to help you learn programming. You can't learn programming by having others write or complete your program.
Stefan_Lang 20-Nov-19 3:07am    
You tell me. It's you task to write that program. If you wrote it, you should know what it currently does or doesn't do. If you didn't, you failed the assignment from the start.

You have been given this assignment to help you learn programming. You can't learn programming by having others write or complete your program.

1 solution

Quote:
How do I fix this sliding puzzle code in C++ to match my assignment?

You should describe the problem you encounter.

Quote:
am not sure how to format

Code formatting matters because it help making you code easier to read.
Never pack things in a single line of code, it is difficult to reread:
C++
for( i = 0; i < p.size(); i++ )
    if( z == p[i] ) { move( v[i] ); return; }

for( i = 0; i < p.size(); i++ )
    if( z == p[i] ) {
        move( v[i] );
        return;
    }

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#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;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
Stefan_Lang 15-Nov-19 3:38am    
I really don't think there was any question on code formatting. ;-) But otherwise good advice.
Patrice T 15-Nov-19 6:55am    
I know, but packing on single line makes the code really ugly :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900