Game Development Reference
In-Depth Information
X wins 5 games (50.0%), O wins 4 games (40.0%),
ties for 1 games (10.0%) of 10.0 games total.
Because the algorithm does have a random part, your run might not have the exact same
numbers as above.
Printing things out to the screen slows the computer down, but now that we have
removed that code, the computer can run an entire game of Reversi in about a second or
two. Think about it. Each time our program printed out one of those lines, it ran through an
entire game (which is about fifty or sixty moves, each move carefully checked to be the one
that gets the most points).
Percentages
Percentages are a portion of a total amount, and
range from 0% to 100%. If you had 100% of a pie,
you would have the entire pie. If you had 0% of a
pie, you wouldn't have any pie at all. 50% of the
pie would be half of the pie. A pie is a common
image to use for percentages. In fact, there is a
kind of chart called a pie chart which shows how
much of the full total a certain portion is. Here is a
pie chart with 10%, 15%, 25%, and 50% portions
below. Notice that 10% + 15% + 25% + 50% adds
up to 100%.
We can calculate the percentage with division.
To get a percentage, divide the part you have by
the total, and then multiply by one hundred. For example, if X won 50 out of 100 games,
you would calculate the expression 50 / 100 , which would evaluate to 0.5 . We
multiply this by 100 to get a percentage (in this case, 50%). Notice that if X won 100 out
of 200 games, we could calculate the percentage with 100 / 200 , which would also
evaluate to 0.5 . When we multiply 0.5 by 100 to get the percentage, we get 50%.
Winning 100 out of 200 games is the same percentage (that is, the same portion) as winning
50 out of 100 games.
Figure 16-1: A pie chart with 10%,
15%, 25%, and 50% portions.
Division Evaluates to Floating Point
It is important to note that when you use the / division operator, the expression will
always evaluate to a floating point number. For example, the expression 10 / 2 will
evaluate to the floating point value 5.0 and not to the integer value 5 .
This is important to remember, because adding an integer to a floating point value with
the + addition operator will also always evaluate to a floating point value. For example, 3
+ 4.0 will evaluate to the floating point value 7.0 and not to the integer 7 .
 
Search WWH ::




Custom Search