what alternate way can i do the do move function?
what alternate way can i do the do move function?
Growing tired of seeing the same question over and over....
Perhaps:
Code:int doMove(int puzzle[][PUZZLE_SIDE], int move) { int looping_variable[6] = {0}; for(looping_variable[0] = 0; looping_variable[0] < 4; looping_variable[0]++) { for(looping_variable[1] = 0; looping_variable[1] < 4; looping_variable[1]++) { if(puzzle[looping_variable[0]][looping_variable[k]] == move) { looping_variable[2] = looping_variable[0]; looping_variable[3] = looping_variable[1]; } else if(puzzle[looping_variable[0]][looping_variable[1]] == EMPTY_SLOT) { looping_variable[4] = looping_variable[0]; looping_variable[5] = looping_variable[1]; } } } if(!puzzle[looping_variable[2]-1][looping_variable[3]] || !puzzle[looping_variable[2]+1][looping_variable[3]] || !puzzle[looping_variable[2]][b-1] || !puzzle[looping_variable[2]][looping_variable[3]+1]) { swap(&puzzle[looping_variable[2]][looping_variable[3]],&puzzle[looping_variable[4]][looping_variable[5]]); return 1; } return 0; }
Which certainly doesn't incorporate what tabstop was saying and I am definitely not saying one should write their code like this. It is just something different.
wow, thanks alot, whenever i replace this with the alternate way (previous in program before, the program crashes after entering a number..
Really? Can you show me your alternate ways? I mean mine wasn't much of an alternative. Its messy to read, and is basically the same thing.
yours was fine, im just trying to code it differently and that helped me alot actually, for some reason i am just having trouble with this stuff and havent gotten the hang of it yet, you thiink you could do something similar to the last function:
int solved(int puzzle[][PUZZLE_SIDE])
I'm still trying to pick myself up off the floor.
Again, the basic algorithm is not really very changeable, so apart from just picking a whole new data structure you aren't going to be able to make a great deal of changes.
I think solved() is written just fine. To be frank, you went the extra nine yards and didn't just loop through values like a lot of folks might (which is an example of a seasoned pro, or a rookie who doesn't quite think in terms of loops just yet).
Your algorithm looks fine, cprogrammer22
hmmm... you think you can show me what that other option would look like, sorry to be so much trouble
You aren't of any trouble at all... But honestly, what I am showing you is not as good as the beautiful piece of software you conjured on your own. But here goes...
You get less of a hand crap typing the loops, but your optimizer may not unroll all those loops, thus you are less with a sub-optimal way of doing this check.Code:int solved(int puzzle[][PUZZLE_SIDE]) { int i, x, y, r; for(r = 1, i = 1, x = 0; x < 4; ++x) for(y = 0; y < 4; ++y) if(puzzle[x][y] != i++) { r = 0; break; } return r; }
Last edited by master5001; 11-05-2008 at 12:31 PM.
when i load this new function into the old program, it will let me move one piece and regardless if the puzzle is in order, it wil say i solved the puzzle and end the program
messing around with it again, i can make the program complete in correct order, but now the program will not say puzzle completed successfully when in correct order
Ooops. I fixed it. I had i = 0 instead of r = 0. My bad. Though you should not be using code that I keep telling is not as optimal as your own.
hmm.. i dont know why i keep having trouble with it.. i added the new code and still it wont complete when the numbers are in order from 1 - 15.. it will just keep allowing me to move the puzzle pieces around