Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 8 Puzzle Game
Index -> Programming, C++ -> C++ Submissions
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
A.J




PostPosted: 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



puzzle.txt
 Description:
Here it is!!!

Download
 Filename:  puzzle.txt
 Filesize:  11 Bytes
 Downloaded:  1218 Time(s)


Puzzle Generator.cpp
 Description:
Here it is!!!

Download
 Filename:  Puzzle Generator.cpp
 Filesize:  2.36 KB
 Downloaded:  1121 Time(s)


Game(UC).cpp
 Description:
Here it is!!!

Download
 Filename:  Game(UC).cpp
 Filesize:  6.98 KB
 Downloaded:  1128 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: 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
Insectoid




PostPosted: 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?
A.J




PostPosted: 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
A.J




PostPosted: 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.
Insectoid




PostPosted: 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
saltpro15




PostPosted: 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.
A.J




PostPosted: 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).
Sponsor
Sponsor
Sponsor
sponsor
hkbnp




PostPosted: 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?
A.J




PostPosted: 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.
wtd




PostPosted: 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.
hkbnp




PostPosted: 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);
}
A.J




PostPosted: 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....
andrew.




PostPosted: 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.
saltpro15




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 31 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: