Sunday, September 5, 2010

Organised randomness

The boxes aka the kids were in the prototype placed by hand on the scene in a grid. I did this so I could easily check if my method for making the kids near a crying child sad work. It did. So today I've worked briefly on placing the boxes/kids randomly through the code thus making the game a bite more exciting.

When placing the boxes/kids I didn't want complete randomness it would stupid if all the boxes ended up on top of each other or only in one side of the screen. I needed to organise the randomness a bit. So I've combined the random placement with a grid structure.

In the code this is accomplished with the help of a couple of for-loops. The illustration above shows the concept. For each loop A, B and C in the example there are inner loops (here illustrated with 1, 2 and 3) that picks random numbers of kids to place (here either 3 or 4) and places them at random coordinates within the intervals defines by the two for-loops. Anyway, this is how I visualised the "organised randomness" in my head before I started coding. Perhaps the code is easier to read :)

4 comments:

  1. But in this case you get a random number of kids (27-36). Is this intended?
    As I see it, it would be better to create the list of Kid objects in another class and send that list to this function. I would be cleaner code, as this function then only do what its name implies, and you could let data on each kid affect its positioning (I could for example imagine kids having a higher probability of being closer to certain other kids).

    ReplyDelete
  2. Okay so you think it would be better with a set number of kids the whole time. I see you point. That would probably be better. And placing this in the Kid class is cleaner. I get your point.

    So you would make the list of kids in the Kids class and eg. "tag" them with different abilities and then "send" this info with them to the KidPlacer class?

    Also, I plan to emplement a method that ensures the kids don't end up on top of each other. For this i going to reuse some of the coded I'm using to check if a kid is crying close to a kid. This is my next step.

    Thanks a lot for your input. I'm new to all this coding so great with some help ü

    ReplyDelete
  3. As I see it, you need the following classes at least:
    - Level (or maybe "Classroom"?)
    - Kid
    - Position
    Where a Level class contains the list of Kid objects. Each Kid object contain a Position object (containing coordinates, heading and such)
    The Level class could then have at least two functions like: "createListOfKids(int amount)" and "placeKids()" (the functions are a part of the Leve class, so they can operate directly on the list of Kids, so no need to send the list along)
    If you still want to have a random number of kids, then simple send a random amount to the "createListOfKids" function.

    Your code should generally speaking contain a class for each "Noun-element" present in your game, if that element contains more than one piece of information.
    As you might have noticed, classes are named with nouns (what they are) and functions are named with verbs (what they do).
    You can rarely have too many functions, but often too few. If a function only does one thing (what its name says), it will be much easier for you (and others) to read your code (remember that you won't always be able to remember what each line of code was supposed to do)

    ReplyDelete
  4. "Where a Level class contains the list of Kid objects. Each Kid object contain a Position object (containing coordinates, heading and such)" - You mean where the position class.. right?

    I'll be rewriting the code. It may take a while for my - the noob. Also, I just got introduce the model-viewer-controller pattern by martin in my latest post. This is so nice getting help via the blog. Thanks Ü

    ReplyDelete