开发者

How to design this particular finite state machine?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜