« on: 30 Jul 2015, 00:03 »
(The explicit answer to your question is at the end, but if you feel in over your head, this explanation might help.)
It's not actually particularly difficult, it's just that there's no built-in support for it, so you have to write it all yourself, and computer programming is fiddly. It's a little surprising that no one has made a module for it, but slider puzzles have a pretty bad reputation among many adventure fans, so that could be why.
If you're happy having the puzzle as a room, I would probably just put all the code in the room script.
To approach a task like this, it's often useful to break it down into a few questions or steps:
Q0. How do you actually want it to work, in detail? Particular requirements might affect the best way to write it.
-Is the empty slot (in the solved puzzle) in a particular position, e.g. the last slot?
-Is this definitely just one puzzle, or is it possible that you'll have several different slider puzzles in the game?
-Is the puzzle fixed in a particular location in the game, or is it something the player carries around with them and can attempt anywhere?
-Do you want animations (pieces sliding), or is it OK for them to just jump from one slot to another?
-Do you want to move pieces by clicking on them (easy), or actually have players use drag-and-drop gestures to slide them themselves (harder)?
-Do you want to be able to slide multiple pieces together? (Hardly necessary in a 3x3 puzzle, I'd think.)
-If players leave and come back, should the puzzle remain as they left it, or reset?
-Does the puzzle always start in the same state, or should it be randomized each time?
(Most of these don't make a huge difference to this particular approach, but some answers might require a different solution.)
Q1. How can we get AGS to display what we want and respond to relevant user actions? (In this case, display tiles in different arrangements, and move them when clicked.)
A. Several possibilities, including making the tiles room objects, or buttons on a GUI. In each case, the interaction is most easily handled by using the OnClick event. (This implies that the code starts out knowing which tile was clicked, but not automatically which slot it is in.)
Q2. What do we need to keep track of to make the logic of the puzzle work? What datastructures do we need?
A. Basically, we just need to know the arrangement of the tiles. A plain array of tile indexes (whether as ints or pointers to the objects or buttons) will do fine. Also, while not strictly necessary, it simplifies things a bit to explicitly keep track of the empty slot.
Q3. What are the tasks we need to be able to perform in the puzzle? What are the things we need to be able to deal with?
A. Here are some useful features we need to make a slider puzzle:
-Set up the puzzle in some arrangement; usually this is handled by a function called init()
-You might want to be able to randomize the initial state, but we can leave that for later; to do so, you might have a function called randomize()
-Display the puzzle on screen (arrange the buttons or objects to match the model of the tiles in our array); this is the displayPuzzle() function above
-Check if a particular tile can move
-Move a certain tile into the empty spot
-Check if the puzzle is solved; this is the isPuzzleSolved() function above
-Display the final piece
Each of these functions is essentially some test or update of our variables (the array of tile indexes and the variables to keep track of the empty slot), or our objects/buttons, as the examples in the previous post demonstrate. It's a bit tedious to write, but shouldn't be too difficult, as each task is pretty simple, individually.
Q4. So how do we link those functions together and trigger them to happen at the right time?
A. You can see that if we have all these functions, it's not that difficult to wire up the whole puzzle. We just need to run the right ones when particular events happen:
-User enters room (puzzle starts): init()/randomize(), then displayPuzzle()
-User clicks on a tile: check if tile can move, if yes, move tile and update the display, then check if puzzle is solved, if so, display final tile