- Home /
Need help finding why my onGUI() sliding image puzzle has errors!
Hey guys! So I am in the process of programming a game which is probably well beyond what I should be trying to complete in the grand scheme of things given my lack of previous programming experience. And my team had an idea that we should have a puzzle with sliding pieces similar to many other mini-games or some mobile apps.
So it's an extremely simple puzzle. Just 9 pieces with 1 empty piece. The player can move pieces around as long as they are moving into the empty slot.
For some reason I decided to do this puzzle with all the visuals and user input inside OnGUI() with just one separate function that is called when a piece is eligible to move.
So I made it my goal to figure out how to do this myself I figured it would be a good mental exercise for me since I never programmed anything like this before. So I didn't look up any solutions or ideas for how people have done this before, which is likely why my code is so long and unusual.
But given that this is just a little mini-game within a much larger action RPG (and not even really necessary to be in the game to begin with) the amount of trouble and time I've spent trying to get this working is a bit ridiculous. I've spent so much time on this and I can' figure it out! So here I am hoping someone can isolate the problem in my code, or suggest a solution.
I'm going to post my entire code which is pretty long and poorly written, like I said I'm pretty new to programming still and this kind of logic just really isn't my strong point obviously.
I'm sure there are much better solutions to do this overall, but I'd really like someone just to isolate the problem if possible. About 90% or more of the possible move combinations work for the pieces but I can't seem to find the reason why a very small number of them fail :(
So the pieces are arranged in a grid logically like so:
1, 2, 3
4, 5, 6
7, 8, 9
Puzzle piece 7 if the one I am starting empty and it seems to be the cause of the few errors out of all the pieces that do work. I'm not even trying to add randomization for the starting positions or a victory check function because I haven't been able to get the pieces to move flawlessly in all positions.
I may post a youtube video of this issue later if it may help people see why almost everything works just a few spots don't :(
Wow didn't know you could post such a big code segment here! Well if someone can solve this problem you would be my hero for real. I just need another pair of eyes for this one. It seems for some reason I cannot find the proper conditions to control the very few piece combinations that can fail to move correctly.
An example of a spot where it fails can be seen at line #985
Answer by RyanZimmerman87 · Aug 24, 2013 at 11:05 PM
OK wow I finally just figured out the problem and it was sooo simple I want to slap myself silly at the moment!
So the problem this whole time was that I needlessly included the empty puzzle piece position in my code! I originally was actually swapping this position with the moving pieces position but then I decided to scrap that and simply move the one piece to make it easier to work with the conditions and order of operations when swapping pieces.
However when I decided to only move the one piece I forgot to delete all the code references referring to the empty pieces position.
Therefor in certain circumstances the code would recognize the position of the empty piece and move that instead of moving the actual piece since only one move was possible per function call.
So simply deleting all the code for the empty puzzle piece solved the problem! Now I simply mark the 1-9 int for where the empty piece is but the piece doesn't actually exist and cannot interfere with the real pieces.
Not sure if that makes sense but what a strange problem and I can't believe the solution was so simple this whole time!
Your answer
Follow this Question
Related Questions
Easy way to generate 8 piece sliding puzzle so that it is always solveable? 4 Answers
How to scramble image pieces? 1 Answer
How to dispaly list of images from server on GUI? 1 Answer
Sliding a sprite inside a grid 0 Answers
Is there a way to add transition puzzle image in 2d sliding puzzle to another image once solved? 0 Answers