
-----------------------------------
A.J
Thu Mar 12, 2009 9:48 pm

8 Puzzle Game
-----------------------------------
This is a basic 8 Puzzle Game that I have been working on for the last hour or so. It is pretty rudimentary, so please bare with me.

A cool feature, I think, is the fact that you can have the computer solve it for you at any point in the game (by pressing the spacebar).

I have included a Puzzle Generator for the puzzle, 'puzzle' text file (that stores the puzzle), and the Game.

Please comment, as I would like to know how it is.

Thanks :D

-----------------------------------
A.J
Fri Mar 13, 2009 5:37 pm

RE:8 Puzzle Game
-----------------------------------
You need the puzzle.txt in order for the game to work. Plus I forgot to mention that it only works on windows :(. If people want to see another version, please let me know....it looks like that people aren't interested :(

-----------------------------------
Insectoid
Fri Mar 13, 2009 5:41 pm

RE:8 Puzzle Game
-----------------------------------
Mac version. Send me the source, I can compile it myself.

EDIT: woops, that is the source. Why won't it work on mac/linux?

-----------------------------------
A.J
Fri Mar 13, 2009 5:51 pm

RE:8 Puzzle Game
-----------------------------------
Try it and tell me if it does...Saad was telling it doesn't

-----------------------------------
A.J
Sat Mar 14, 2009 12:45 pm

RE:8 Puzzle Game
-----------------------------------
It is the "#include" that causes problem in mac/linux. I use that for keyboard input.

-----------------------------------
Insectoid
Sun Mar 15, 2009 9:45 am

RE:8 Puzzle Game
-----------------------------------
would it be possible to change that? Is there a different IO class that's multi platform?

EDIT: btw, command line compilers don't like your file names

-----------------------------------
saltpro15
Sat Mar 21, 2009 6:57 pm

RE:8 Puzzle Game
-----------------------------------
very cool :D I love how it can solve it for you and show you the necessary number of steps, very impressive A.J.

-----------------------------------
A.J
Sat Mar 21, 2009 7:10 pm

RE:8 Puzzle Game
-----------------------------------
Thanks saltpro15. It isn't too hard to code this. It actually took me about 20 - 30 minutes. The solving takes the most time (using BFS to work outwards from your state to the goal state).

-----------------------------------
hkbnp
Sat Jul 11, 2009 6:17 am

RE:8 Puzzle Game
-----------------------------------
In Game(UC).cpp line 106, why the integer is 47?

can you talk about how to find out the integer?

-----------------------------------
A.J
Sat Jul 11, 2009 4:11 pm

RE:8 Puzzle Game
-----------------------------------
well, the ASCII value of '0' is 48. I use to convert between char and int.

-----------------------------------
wtd
Sat Jul 11, 2009 6:58 pm

RE:8 Puzzle Game
-----------------------------------
Instantly loses massive points for the use of global variables.

Also, learn to use header files for declarations of functions, and makefiles for compiling your programs.

-----------------------------------
hkbnp
Sat Jul 11, 2009 9:30 pm

RE:8 Puzzle Game
-----------------------------------
OK
i am trying to modify your program to 15 puzzle.i get error when solve the puzzle

void findGoalState()
{
    int sum = 0;

    for (int i = 0; i < 15; i++)
    {
        if (initialState[i] != '0')
        {
            for (int j = i + 1; j < 16; j++)
            {
                if ((initialState[j] != '0') && (initialState[i] > initialState[j]))
                    sum++;
            }
        }
    }

    if (sum % 2 == 0)
        goalState = "12345678abcdf0";
    else
        goalState = "12345678abcdf0";

}

int encode(string board)
{
    int encrypt[16] =
        {
            16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1,15*14*13*12*11*10*9*8*7*6*5*4*3*2*1,14*13*12*11*10*9*8*7*6*5*4*3*2*1,13*12*11*10*9*8*7*6*5*4*3*2*1,12*11*10*9*8*7*6*5*4*3*2*1,11*10*9*8*7*6*5*4*3*2*1,10*9*8*7*6*5*4*3*2*1,9*8*7*6*5*4*3*2, 8*7*6*5*4*3*2, 7*6*5*4*3*2, 6*5*4*3*2, 5*4*3*2, 4*3*2, 3*2, 2, 1
        };
    int value = 0;

    for (int i = 0; i < 16; i++)
    {
        value += (int(board[i]) - 48) * (encrypt[i]);
    }

    return value;
}

void solve()
{
    cout 