Hardware Reference
In-Depth Information
CHALLENGE
1. The block friend always moves at the same speed: one block about
every 0.25 seconds, or 4 bps (blocks per second). However, a real
friend would speed up the further away her target got. Try changing
the program so that the further the block friend is from the player,
the quicker she moves.
2. Revisit “Adding a Homing Beacon” in Adventure 4 and complete the
challenge again, this time building a better distance estimator using
the distanceBetweenPoints() function.
Using Random Numbers to
Make Your Block Friend
More Interesting
The problem with the block friend program you just created is that it is predictable ;
it's always going to do the same thing. After you've run the program only a couple of
times, you'll know exactly what it's going to do and when, so it's going to get boring
very quickly. To really give your block friend a “mind”, you need to give it an air of
unpredictability.
When something is predictable , it means you are able to foresee what is going
to happen before it does. This isn't great if you want it to act as if it's real. Real
things are not always predictable.
By using random numbers, you can simulate unpredictability—in other words, make
something look as if it's acting unpredictably. You do this by making a program choose
to do things based on a probability ; for example, you might tell it to do a particular
action 1 every 100 times. By adding more rules based on different odds, you can make
the program much more difficult to predict. Before changing the block friend program
to use random numbers, let's explore the code to create random numbers and probabil-
ity checks. You may remember random numbers being introduced in Adventure 3.
A probability is the measurement of how likely it is that something will happen,
i.e. when flipping a coin there is a 50% (or 1 in 2) chance that it will land on heads.
 
Search WWH ::




Custom Search