Write the game of Nim in C ++)

Source: Internet
Author: User
The game of Nim is a well-known game with many versions. the following version has an interesting winning strategy. the two participants exchange several numbers from a pile of stones, and the number is determined by the participants themselves. however, each participant is required to retrieve at least one, at most half, and the other participant continues. participants who get the last Stone will lose the game.
Write a program that allows computers to compete with people. A random number is generated between 10 and 100 as the initial number of stones. then, 0 or 1 is randomly generated to determine whether the computer is playing first or the person is playing first. then, 0 or 1 is randomly generated to determine whether the computer adopts the "smart" or "stupid" mode. in the stupid mode, when it is the computer's turn, the computer simply removes the random number of stones (between 1 and N/2). In the smart mode, the computer removes a certain number of stones each time, so that the remaining number of stones is a power minus 1 of 2, such as 3, 7, 15, 31, 63. this is always feasible, unless the current number of stones is exactly a power minus 1 of 2. in this case, the computer removes the random number of objective stones.
You will find that when a computer is smart and comes first, it will never be defeated unless the number of original stones is accidentally a power minus 1 of 2. for example, 15,31, 63. of course, if a person is a pioneer and knows the winning strategy, the computer can also be defeated.
The following is the spam code I wrote:
# Include <iostream>
# Include <string>
# Include <cstdlib>
# Include <ctime>
Using namespace STD;
Void rand_seed () // seed by Time
{
Int seed = static_cast <int> (time (0 ));
Srand (SEED );
}
Int rand_int (int A, int B) // generates a random integer between A and B.
{
Return A + rand () % (B-A + 1 );
}
Const int leftnum = 10;
Const int maid = 100;
Const int intarray [] = {63,127,255,511,102 };
Int main (){
Int I = 1, rocknum;
Int priority = 0; // priority 0 computer first
Int acumen_ai = 0; // 0 smart, 1 stupid
Bool flag = false;


Rand_seed ();
Rocknum = rand_int (leftnum, rightnum );
Cout <"Total stones:" <rocknum <Endl;



Priority = rand_int (0, 1 );
Acumen_ai = rand_int (0, 1 );
If (priority = 0) cout <"Computer priority" <Endl;
Else cout <"player first" <Endl;
If (acumen_ai = 0) cout <"Current competitor is smart" <Endl;
Else cout <"Current competitor is stupid" <Endl;
**
While (true)
{

If (rocknum = 1 ){
If (priority = 0) cout <"you win" <Endl;
Else cout <"You lost" <Endl;
Break;
}
If (acumen_ai = 0)
{// Current competitor is smart
If (priority = 0)
{// Play begin on the computer first
If (rocknum = 2)
{
Rocknum-= 1;
Cout <"opponent extracted from the stone: 1 Number of remaining stones:" <rocknum <Endl;
Cout <"You lost" <Endl;
Break;
}

For (I = 0; I <12; I ++)
If (intarray = rocknum)
{
Flag = true;
Break;
}
If (! Flag)
{
For (I = 0; I <12; I ++)
{
Int TMP = rocknum-intarray;
If (TMP> = 1 & TMP <= (rocknum/2 ))
{
Rocknum-= TMP;
Cout <"the opponent extracted from the stone:" <TMP <"number of remaining stones:" <rocknum <Endl;
}
}

}
If (FLAG)
{
Int TMP = rand_int (1, (rocknum/2 ));
Rocknum-= TMP;
Cout <"the opponent extracted from the stone:" <TMP <"number of remaining stones:" <rocknum <Endl;

}
Flag = false;
Priority = 1;
} // Start with end
Else if (priority = 1)
{
Cout <"Enter the number of stones to be taken, ranging from 1 to" <(rocknum/2) <";
Int TMP;
Cin> TMP;
Rocknum-= TMP;
Cout <"you extracted from the stone:" <TMP <"number of remaining stones:" <rocknum <Endl;
Priority = 0;
}
}
Else
{// The current competitor is stupid
If (rocknum = 1 ){
If (priority = 0) cout <"you win" <Endl;
Else cout <"You lost" <Endl;
Break;
}
If (priority = 0)
{// Play begin on the computer first

Int TMP = rand_int (1, (rocknum/2 ));
Rocknum-= TMP;
Cout <"the opponent extracted from the stone:" <TMP <"number of remaining stones:" <rocknum <Endl;
Priority = 1;
} // Start with end
Else if (priority = 1)
{
Cout <"Enter the number of stones to be taken, ranging from 1 to" <(rocknum/2) <";
Int TMP;
Cin> TMP;
Rocknum-= TMP;
Cout <"you extracted from the stone:" <TMP <"number of remaining stones:" <rocknum <Endl;
Priority = 0;
}
}


}
Cin. Get ();
Cin. Get ();
Return 0;
}

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.