8. Working with random numbersWhen you make a game, you wouldn't normally want it to go the same way every time it is played. A good way to get some variation into a game is by making use of so-called random numbers. In Java you can make a "random" number with the help of the method random(), which is part of the Math class. This method returns a random (more or less unpredictable) broken number which is at least 0 and smaller than 1. We could produce such a random number r in the following way:
After this line, r could (for instance) have thefollowing value: 0.4303072576758019 By the way, double is a type specially used for broken numbers which must be very accurate. A variable of the double type has a very large precision. But let's return to our random number r. Sometimes you don't want broken numbers, but something like a number produced by throwing dice (typically, the numbers 1 through 6). Let's try to emulate the throw of a die. We can multiply the expression above by 6:
The number produced is still a broken number (or floating point number, as it is called) and its minimum is still 0, but its maximum is almost 6 now (5 point .....). It is possible to cut off the fraction part of this number with a so-called cast. A cast looks like a type in parentheses, for instance like this: (char)a or (int)b. As we saw before, it is used to change one type of variable into another one. In this case we can use this cast:
What we are left with is a number which is at least 0 and whose maximum is 5. The numbers on a die, however, are at least 1 and have a maximum of 6. We can obtain this by simply adding 1, so:
Now that we can make random numbers, we could put together a simple game. The game we are going to make works like this: the program takes "in mind" a random number (1 through 100), and the player may try to to find out what number the program "thinks of". After each guess, the program will issue one of only three answers too high, too low and correct. The program has a number of well-defined stages: 1. explanation about the game In order to know which stage the program is in, we shall use a variable called round, which will be increased every round. We could check the value of round like this:
But for cases like this, Java has a construction which is shorter, easier to use and which looks neater as well. This is what it looks like:
When the program encounters a 'hit', all the following lines would be executed. This is prevented by the addition of break. The expression default refers to all other values, in this case all other values than 1 and 2. We can use another variable times to count the number of guesses needed, so that after the game we can issue a statement like:
When the player has to type in a new number, it would be handy if the cursor is already in the textfield and the number typed previously does not have to be erased first. This can be done with:
If the player wants to play again, it would be nice if the new number is not thesame as the number that was used in the previous game. This can be done like this:
And here is the complete program:
Exercise 8.1 Please write an applet in which you see a new proverb after each click (at least five times) on a button. After the last proverb the program starts again with proverb no 1.
Exercise 8.2 Please rewrite the Guessing applet so that the roles are reversed: the player takes a number in mind and the computer must try to find it. You could use three buttons called Higher, Lower and Correct. If the player tries to cheat, the game must be ended (see the example below).
(c) 2003-2008, Thomas J.H.Luif |