How to win the beans game

Source: Internet
Author: User

This is how the app game is played. There is a certain number of beans (for example, there are 100), and then two people are required to take some number of beans in turn, the maximum number of items to be taken each time (for example, 3), and at least one. Finally, whoever takes the last pile of beans is the one who loses (of course, you can also set the final winner ).

This is actually an interesting question about the number of students. It cannot be done in high school, but now I think it's easy to do it. It seems that there is indeed progress.

Assuming that the game's final method is who takes the last pile of beans and determines who loses (this will be analyzed, and whoever takes the last pile of beans will be able to easily handle the winning game ), let me analyze the mysteries of this game. QUIT is EASY! Follow me:
First, analyze the individual situations (and then promote it to the general situation). Assume that each time the number of sub-items can be 3, and whoever takes the last pile of beans is counted as who loses.
The first hand wins, the first hand wins, and the first hand loses, and the first hand loses. (You will understand why you want to do this later)
ATTENTION:
We only win or lose the first hand, because when the first hand wins, the latter will lose; and vice versa.
You can get up to three pills each time.

1: Are you a pioneer? Oh, unfortunately, you will lose. 1
2: Are you a pioneer? Hehe, take a grain, and the opponent faces a grain, you will win, and he will lose. 0
3: With the lessons learned, you should be smart first. By the way, take the two. You will be 0
Win.
4: It's up to you. If you get 3, your opponent will lose. 0
5: Sorry, it's terrible. When taking 1, 2, and 3, the number of opponents is 4, 3, 2, and 1.
If you win, you will lose.
6: As long as you take one tablet and let the opponent get only 5, the opponent will lose. 0
7: 2 for the same reason. 0
8: 3 for the same reason. 0
9: When the opponent gets 6, 7, and 8, the opponent wins and you lose. 1
10: 1, so that the opponent will lose 10. 0
11: Do you see the rule? By the way, take 2. 0
...

To sum up, summarize the winning algorithm of this game:
Who took the last heap and who lost: when the first hand encounters 1 + k * (n + 1) (where k is an integer, n is the maximum number of sub-accounts that can be obtained at a time. In other cases, the first hand wins. Therefore, both sides of the game have only one purpose, add the child to the opponent to 1 + k * (n + 1.

Further, the opponent will win. For you, you will lose and return to it.
1:1
2: returns the inverse of 1 to 0.
3: returns the inverse of the preceding two results and then returns the intersection. (1 & 0) = 0
4: return the inverse of the preceding three results and then return the intersection. (0 & 1 & 1) = 0
5: return the inverse of the preceding three results and then return the intersection. (1 & 1 & 1) = 1.
... Got it?

Who took the last heap and who won the game: The analysis method is the same (Omitted) as above. The conclusion is that if the number of sub-numbers is k * (n + 1), it will be defeated.

The following is an AI class that implements this effect (C ++ language description)
Class AI {
Public:
Int HighAI (int winMode, int netNum, int limitNum );
Int LowAI (int netNum, int limitNum );
Int AIThinking (int winMode, int netNum, int limitNum, int aiLevel );

};
Int AI: HighAI (int winMode, int netNum, int limitNum ){
Int outNum = 0;
Int t = 0;
Switch (winMode) {// 1 who takes the last bet and 0 who takes the last bet and wins
Case 1:
If (t = (netNum-1) % (limitNum + 1) = 0) {// helpless, This is the defeat situation, had to wait for the opponent to go wrong.
OutNum = LowAI (netNum, limitNum );
}
Else {// Yes
OutNum = t;
}
Break;
Case 0:
If (t = (netNum % (limitNum + 1) = 0) {// helpless, This is a defeat, so I had to wait for the opponent to make a mistake.
OutNum = LowAI (netNum, limitNum );
}
Else {
OutNum = t;
}
Break;
}
Return outNum;
}
Int AI: LowAI (int netNum, int limitNum ){
Int outNum = 0;
Randomize ();
Do {
OutNum = random (limitNum) + 1;
} While (outNum> netNum );
Return outNum;
}
Int AI: AIThinking (int winMode, int netNum, int limitNum, int aiLevel ){
Int outNum = 0;
Switch (aiLevel) {// an adjustable AILEVEL.
Case 0: outNum = LowAI (netNum, limitNum); break;
Case 1: outNum = HighAI (winMode, netNum, limitNum); break;
}
Return outNum;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.