views:

98

answers:

1

Hey peeps,

I am trying to get my head around how to design the following system, which I think can be defined as a finite state machine:

Say we have a pile of 16 building blocks (towers, walls, gates) together forming a castle. The player can drag the blocks to 16 places on a floorplan and if done right they will see the whole castle. All towers (there's four of them) are equal so they can go on any of the four corners. Same goes for some of the walls.

All in all there are 16 spots on the floorplan where you can put a building block and each of the spots can have 17 "states": empty + either one of the 16 building blocks. Doing some maths this leads to 17^16=a LOT of combinations.

The program starts with an empty floorplan and a pile of building blocks. It should then show a message like "build your own castle, start with the tower". When the user places a tower correctly, it should say "well done, now build all four towers". You get the idea.

Problem is: there are so many things a player can do. Put a block at the wrong place, remove a block, correctly put walls or towers all over the floorplan ignoring the directions given to them, etc.

It would be awesome if I could avoid having to use thousands of if-then statements to decide wether I should take the next step, show an error message or go back to the previous step based on what the player is doing.

How would you describe the NEXT, PREVIOUS and ERROR conditions for every step of the building sequence? Are there any design methods for this? Thanks a lot for your input.

A: 

Try to do this declaratively. Define an enum (or possibly classes) describing the kinds of blocks. Define and construct a 4x4 2D array describing the sets of permissible kinds of blocks in each position (implement the sets as lists, bitfields, whatever suits you best). Whenever a player tries to place a block in a position, check whether it is permissible against the 2D array. If you want to have particular messages for a position being correctly filled in, also put those in the same an array.

I don't know if a FSM is really what you are after: what kinds of sequencing constraints are you looking to verify? Does it matter whether towers are built first? From the rest of your description, it sounds like the above goal state description would be more suitable.

Pontus Gagge
Thanks for your reply. I have been discussing this problem with another programmer and he also talked about bitfields. But in this case we want the player to follow a fixed building sequence (it is an educational program) while still allowing them to move the blocks around the way they seem fit.
Droozle
After reading a bit more about state machines, I do think this is exactly the kind of problem that can be solved using FSM's. So far I have been able to deduce that:-I have 16 inputs (the spots on the floorplan)-Each input has only 3 states: EMPTY, CORRECT, INCORRECT-The current state of the building is used as another input-I need to define a condition that advances to the next state-I need to define a condition that returns to a previous state-I need to define a condition that keeps the machine in its current state (that won't be hard, hehe)
Droozle
-If none of the above conditions are met, I can warn the player to revise his strategy, or I could try to find out to what state the current situation belongs to and jump to there (but that might violate FSM design principles, I guess).
Droozle