06CE Attention Game
Contents
After we have represented people in the computer last time, we want to test the user’s attention this time.
0. Our goal
|
|
When you reach 10 points, you win the game.
When you fall below -10 points, you lose the game.
1. How to present 15 random numbers to the user?
1.1 Generating random numbers
You can do this with random()
but it produces a random number between 0 and RAND_MAX
. If you want a random number between 0 and 10, you could, e.g. use random()%10
. Let us put that in a function.
|
|
But in order to give the random number generator a good start, we need to seed (加种子) it, e.g. with the current time as follows:
|
|
1.2 How do you make that 15 digits?
|
|
1.3 Reading an answer from the user
The core is cin>>input
, but before you do that, you want the user to know that you expect input, e.g. as follows:
|
|
1.4 Deciding whether the user is right
For that we need two things. First, we need to store the digits, e.g. in a vector<short>
and secondly, we need to decide whether the input is a digit or something else.
|
|
1.5 How do we count (计数) how often a number occurs?
In other words, we need to implement the function count
. Obviously it takes 2 arguments: The choice
of the user and the numbers
(that is why we stored them beforehand). Then we run over all numbers and compare whether the current number equals the choice.
|
|
1.6 What if the user passes (通过)?
Then we should show them how many points they already reached. For that we have counted the number of points.
|
|
1.7 The user wants to play more than 1 round
So how many rounds? Since we don’t know, we will put that in an infinite loop.
|
|
1.8 But somewhen the user should stop
So how do we want to decide that? We could say that the user wins if they reach 10 points. And we could say that the user loses if they drop to -10 points.
|
|
2. How many numbers do we have to show?
This is a more tricky question. But we can describe the limits of that.
Obviously, we need at least 3 numbers, otherwise there cannot be 3 equal numbers.
If we show 11 numbers, then we know by the pigeon hole principle (鸽子洞原则) that there will be at least 1 number twice – you have 11 pigeons and 10 holes, so 2 pigeons will go in 1 hole.
If we show 21 numbers, then we know by the same pigeon hole principle that there will be at least 1 number three times.
So the answer is somewhere between 3 and 21.
You can experiment with the number of numbers (the upper limit for n
) in order to see what produces 3 equal numbers in many times, but not too many 3-fold numbers. I thought that 15 is a reasonable choice, but maybe 12 is already sufficient.
Maybe the following table helps:
N | P(>2 equal) | P(multiple groups) |
---|---|---|
9 | 48.0% | 4.7% |
10 | 60.9% | 9.5% |
11 | 71.3% | 17.8% |
12 | 81.1% | 27.2% |
13 | 88.4% | 39.6% |
14 | 93.3% | 54.4% |
15 | 96.8% | 67.3% |
16 | 98.6% | 78.3% |
17 | 99.5% | 87.1% |
18 | 99.9% | 93.2% |
19 | 1- 2E-04 | 96.7% |
20 | 1- 4E-16 | 98.6% |
21 | 100% | 99.5% |
Probabilities for 10 different possible digits.
The middle column tells us how likely it is to have at least 1 multiple times repeating digit, i.e. the chance that the user can find an answer. The last column tell us how likely it is that there are multiple multi-repeated digits, i.e. that there are multiple correct answers. We want the second column to be somewhat high, but the third column not too high. So I would recommend for (int n=1; n<=12; n++)
(on line 16).
3. What if we want to punish reluctant (勉强的) users?
Ok, so the user could always choose ‘-’ and never get forward or backwards. If the user does not choose any number, then we could check whether there is actually a number more than twice and if so, punish the user (deduct a point, 扣一分). How do we do that?
3.1 Counting which number occurs how often
We need a kind of bucket counting / bucket sort (桶排序), i.e. we iterate over all numbers and increment the bucket for the current number. In the end if any bucket reaches more than 2, then we return this bucket, otherwise return -1.
An easy version of such buckets is a map (映射), i.e. map<short, int>
which assigns to a short
number an int
amount. We access the buckets with [num]
.
|
|
3.2 How do we use that in the main loop?
|
|
9. Try it for Yourself
We have been talking about a couple of points, but do you know if the game can be played well? Did you test that the game can be won? Did you ever lose in this game?
You won’t know for sure, before you tried that. Careful testing is part of software development.
In the Software industry there are at least 2 roles: coders and testers. Both can be Software engineers and in many companies the same people have to take on both roles. Even if you are just assigned the role of a coder, you should always test your code. Think about each extreme case, think about what a user can do wrong, what could be misunderstood, …