题目:现有21根火柴,两人轮流取,每人每次可以取走1至4根,不可多取,也不能不取,谁取最后一楰火柴谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。
/*
题目:现有21根火柴,两人轮流取,每人每次可以取走1至4根,不可多取,也不能不取,谁取最后一楰火柴谁输。
请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。
by as1138 2013-06-01
*/
#include <iostream>
using namespace std;
int main()
{
int sum = 21;//tick num
int input = 0;//for input
bool bBreak = false;//if user input the wrong num is true
//game run
do
{
cout << "now the number of ticks is: " << sum << endl;
cout << "turn to your choice(range:1~4): " << endl;
cin >> input;
if(input < 1 || input > 4)
{
cout << "your choice is out of the range(1~4)!" << endl;
bBreak = true;
break;
}
cout << "computer choices the num is: " << 5-input <<endl;
sum -= 5;
}while(sum > 4);
cout << (bBreak?"because you input the wrong num," : "the num of tick is less than 4,") <<" computer is the winner!" << endl;
return 0;
}