Computer Science Canada

8 Puzzle Game

Author:  A.J [ Thu Mar 12, 2009 9:48 pm ]
Post subject:  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 Very Happy

Author:  A.J [ Fri Mar 13, 2009 5:37 pm ]
Post subject:  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 Sad. If people want to see another version, please let me know....it looks like that people aren't interested Sad

Author:  Insectoid [ Fri Mar 13, 2009 5:41 pm ]
Post subject:  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?

Author:  A.J [ Fri Mar 13, 2009 5:51 pm ]
Post subject:  RE:8 Puzzle Game

Try it and tell me if it does...Saad was telling it doesn't

Author:  A.J [ Sat Mar 14, 2009 12:45 pm ]
Post subject:  RE:8 Puzzle Game

It is the "#include<conio.h>" that causes problem in mac/linux. I use that for keyboard input.

Author:  Insectoid [ Sun Mar 15, 2009 9:45 am ]
Post subject:  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

Author:  saltpro15 [ Sat Mar 21, 2009 6:57 pm ]
Post subject:  RE:8 Puzzle Game

very cool Very Happy I love how it can solve it for you and show you the necessary number of steps, very impressive A.J.

Author:  A.J [ Sat Mar 21, 2009 7:10 pm ]
Post subject:  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).

Author:  hkbnp [ Sat Jul 11, 2009 6:17 am ]
Post subject:  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?

Author:  A.J [ Sat Jul 11, 2009 4:11 pm ]
Post subject:  RE:8 Puzzle Game

well, the ASCII value of '0' is 48. I use to convert between char and int.

Author:  wtd [ Sat Jul 11, 2009 6:58 pm ]
Post subject:  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.

Author:  hkbnp [ Sat Jul 11, 2009 9:30 pm ]
Post subject:  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 << "Solving Puzzle..." << endl;

gameTree.push(currentState);

found = false;

while(!gameTree.empty())
{
int up = gameTree.size();

for (int i = 1; i <= up; i++)
{
string next = gameTree.front();

if (next == goalState)
{
found = true;
break;
}

gameTree.pop();

if(moveUp(next) && !used[encode(movedState)])
{
gameTree.push(movedState);
backTrack[encode(movedState)] = next;
}

if(moveDown(next) && !used[encode(movedState)])
{
gameTree.push(movedState);
backTrack[encode(movedState)] = next;
}

if(moveRight(next) && !used[encode(movedState)])
{
gameTree.push(movedState);
backTrack[encode(movedState)] = next;
}

if(moveLeft(next) && !used[encode(movedState)])
{
gameTree.push(movedState);
backTrack[encode(movedState)] = next;
}

used[encode(next)] = true;
}

if (found)
break;

moves ++;
}

string backTrackState = goalState, parentState[moves + 1];
int index = moves;

while(true)
{
parentState[index] = backTrackState;
backTrackState = backTrack[encode(backTrackState)];

if (backTrackState == currentState)
{
parentState[--index] = backTrackState;
break;
}

--index;
}

output(parentState);
}

Author:  A.J [ Sun Jul 12, 2009 12:15 am ]
Post subject:  RE:8 Puzzle Game

what error do you get? (sry, don't hav compiler/IDE on me right now...)

we can continue this convo by pm'ing each other, k?

sry for not being able to help you sooner....

Author:  andrew. [ Sun Jul 12, 2009 11:08 am ]
Post subject:  RE:8 Puzzle Game

I don't do much with C++ so I don't know about the IO modules, but I did some research about conio.h and it turns out that conio was used in the MS-DOS days and is no longer a standard library in C. The websites I've been reading recommend to use stdio.h instead. Like I said before, I don't do much with C++, so don't get mad at me if I'm totally wrong with this information.

Author:  saltpro15 [ Sun Jul 12, 2009 11:31 am ]
Post subject:  RE:8 Puzzle Game

even better, use #include <cstdio>
stdio.h is for C
cstdio is for C++

same goes for
math.h
cmath

string.h
cstring

etc.

Author:  hkbnp [ Sun Jul 12, 2009 10:16 pm ]
Post subject:  Re: 8 Puzzle Game

it can compile. Also move up/down/left/right is no problem. but when i press "space" to solve the puzzle. it get error.
pls find the error as attachment.

Author:  hkbnp [ Sun Jul 12, 2009 10:17 pm ]
Post subject:  RE:8 Puzzle Game

it seem i get trouble in function "encode"
i want to write a 6x6 puzzle game for my friend.
should i modify the encode function?
so i am modifying the 3x3 puzzle code.

Author:  wtd [ Mon Jul 13, 2009 12:01 pm ]
Post subject:  Re: RE:8 Puzzle Game

andrew. @ Mon Jul 13, 2009 12:08 am wrote:
I don't do much with C++ so I don't know about the IO modules, but I did some research about conio.h and it turns out that conio was used in the MS-DOS days and is no longer a standard library in C. The websites I've been reading recommend to use stdio.h instead. Like I said before, I don't do much with C++, so don't get mad at me if I'm totally wrong with this information.


The "conio.h" header was never a part of any language standard.

Author:  hkbnp [ Mon Jul 13, 2009 10:14 pm ]
Post subject:  RE:8 Puzzle Game

"conio.h" use for windows input, it can't be missed.

Author:  A.J [ Tue Jul 14, 2009 10:54 pm ]
Post subject:  RE:8 Puzzle Game

like I said in the pm i sent you, I don't think it is a good idea just trying to update my code for a 6*6.....it is too inefficient to do that....

try coding up an A* algorithm for the program trying to make.....

Author:  ahneh [ Mon Jul 27, 2009 5:23 am ]
Post subject:  RE:8 Puzzle Game

hi A.J,

i have seen your source codes. But i cant analyzed whether is A star search or breadth first search.

Anyone can tell me ?

thanks a lot.

Author:  bbi5291 [ Mon Jul 27, 2009 11:12 am ]
Post subject:  Re: 8 Puzzle Game

It uses a simple FIFO queue, so it would be breadth-first search. A* is an improved version of Dijkstra's, so it would use a priority queue.

Note that there are only 8!/2 = 20160 possible configurations of the 8-puzzle board. Using A* would be overkill. (In the 15 puzzle, this increases to 15!/2 = 653837184000, so then A* or something similarly efficient becomes necessary.)

Author:  ahneh [ Mon Jul 27, 2009 8:39 pm ]
Post subject:  RE:8 Puzzle Game

Thanks bbi5291 XD
you leaded me

Author:  A.J [ Tue Jul 28, 2009 3:15 pm ]
Post subject:  RE:8 Puzzle Game

thanks brian

I am at MathCamp, so I can't get the time to answer all of the questions.

thanks =)

Author:  bbi5291 [ Tue Jul 28, 2009 9:34 pm ]
Post subject:  Re: 8 Puzzle Game

Is that the USA-Canada Mathcamp? The one with the qualifying quiz?
I was invited to that in grade 9 and grade 10 (but I can't go because I'm too poor Sad ) but they didn't invite me this year. Either they decided I sucked too much (unlikely considering that they invited me before) or they realized I was ignoring them...

Author:  A.J [ Wed Jul 29, 2009 11:48 pm ]
Post subject:  RE:8 Puzzle Game

u serious?
wow...I'll ask them why

i am pretty sure that you should hav gotten in

Author:  HRI [ Thu Apr 28, 2011 12:26 am ]
Post subject:  Re: 8 Puzzle Game

Great work! I must bring up a point, however.

c++:

while (true)
{
    if (kbhit())
        in = getch();
...
}


That loop uses a ton of CPU and if you happen to have a computer where the fan is loud when the CPU is being used, it gets really annoying to do input.

A less-CPU-intensive, and shorter too, way is as follows:
c++:

while (in = getch())
{
...
}

Author:  Insectoid [ Thu Apr 28, 2011 2:20 pm ]
Post subject:  RE:8 Puzzle Game

I feel I should point out that the time-stamp on the last post is from 2009. OP has 5 posts total. Safe to assume he won't read your post and wouldn't care if he did.

Author:  A.J [ Thu Apr 28, 2011 9:28 pm ]
Post subject:  RE:8 Puzzle Game

Well, I coded this a long time ago. It is currently in my USB under 'Ancient Projects'. But thanks for pointing that out, HRI. However, like Insectoid mentioned, make sure you reply to recent threads.

Author:  Velocity [ Wed Jan 18, 2012 6:20 pm ]
Post subject:  RE:8 Puzzle Game

great game, for an hours work, i love it +karma

Author:  Aange10 [ Wed Jan 18, 2012 7:50 pm ]
Post subject:  Re: RE:8 Puzzle Game

Velocity @ 18/1/2012, 5:20 pm wrote:
great game, for an hours work, i love it +karma



-karma for necroing a 9 month old post.


: