首先先看下面这个C++代码,它实现了剪刀石头布的游戏,并且在退出游戏时会打印游戏结果。其设计思路是将rock,paper和scissors分别初始化为0,1,2后(由C++11的强类型枚举enum
实现,而且三者出现的先后顺序会影响后面的判断条件),程序则利用rand()
和srand()
随机地做出响应。程序还引入了名为Choice
的namespace
,这不仅是为了之后使用方便,而且也能预防之后程序扩充时引起名字冲突
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace { enum class Choice { rock, paper, scissors }; };
// 用户和电脑的选择
Choice player_choice;
Choice computer_choice;
// 记录用户和电脑的游戏结果
int player_wins = 0;
int computer_wins = 0;
string words[3] = { "rock", "paper", "scissors" };
Choice get_computer_choice();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);
int main(int argc, char* argv[])
{
srand(time(NULL)); // 设置随机种子
string input_str;
int c;
while (true)
{
cout << "Enter Rock, Paper, Scissors, or Exit: ";
getline(cin, input_str);
// 对输入进行校验
if (input_str.size() < 1)
{
cout << "Invalid input." << endl;
continue;
}
c = input_str[0];
if (c == 'R' || c == 'r')
player_choice = Choice::rock;
else if (c == 'P' || c == 'p')
player_choice = Choice::paper;
else if (c == 'S' || c == 's')
player_choice = Choice::scissors;
else if (c == 'E' || c == 'e')
break;
else
{
cout << "Invalid input." << endl;
continue;
}
// 电脑做出随机选择,并判断游戏结果
computer_choice = get_computer_choice();
int p = (int)player_choice;
int c = (int)computer_choice;
cout << "You chose " << words[p];
cout << "," << endl;
cout << "I chose " << words[c];
cout << "," << endl;
decide_winner();
}
// 退出游戏后,显示用户和电脑各自的得分
cout << "player wins: " << player_wins << endl;
cout << "computer wins: " << computer_wins << endl;
return EXIT_SUCCESS;
}
// 电脑等概率地做出选择
Choice get_computer_choice()
{
int n = rand0toN1(3);
if (n == 0)
return Choice::rock;
if (n == 1)
return Choice::paper;
return Choice::scissors;
}
// 判断谁是胜利者
void decide_winner()
{
if (player_choice == computer_choice)
{
cout << "Result is a tie." << endl << endl;
return;
}
int p = (int)player_choice;
int c = (int)computer_choice;
if (p - c == 1 || p - c == -2)
{
cout << get_msg(player_choice);
cout << "YOU WIN!" << endl;
player_wins++;
}
else
{
cout << get_msg(computer_choice);
cout << "I WIN!" << endl;
computer_wins++;
}
cout << endl;
}
// 打印相应的提示信息
string get_msg(Choice winner)
{
if (winner == Choice::rock)
return string("Rock smashes scissors... ");
else if (winner == Choice::paper)
return string("Paper covers rock... ");
else
return string("Scissors cuts paper... ");
}
// 随机地返回0~n-1中的任一整数
int rand0toN1(int n)
{
return rand() % n;
}
在上面的代码中,电脑每次都是等可能的从rock,paper和scissors中做出选择,为了让程序更好玩,下面又对get_computer_choice()
函数进行修改,使得程序在每次运行时都会载入一种偏好,而每种偏好出现的概率又不相同。
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace { enum class Choice { rock, paper, scissors }; };
// 用户和电脑的选择
Choice player_choice;
Choice computer_choice;
// 设置电脑的偏好
Choice comp_first_choice = Choice::rock;
Choice comp_second_choice = Choice::scissors;
Choice comp_third_choice = Choice::paper;
// 记录用户和电脑的游戏结果
int player_wins = 0;
int computer_wins = 0;
string words[3] = { "rock", "paper", "scissors" };
Choice get_computer_choice();
void assign_computer_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);
int main(int argc, char* argv[])
{
srand(time(NULL)); // 设置随机种子
assign_computer_prefs(); // 设置电脑的偏好
string input_str;
int c;
while (true)
{
cout << "Enter Rock, Paper, Scissors, or Exit: ";
getline(cin, input_str);
// 对输入进行校验
if (input_str.size() < 1)
{
cout << "Invalid input." << endl;
continue;
}
c = input_str[0];
if (c == 'R' || c == 'r')
player_choice = Choice::rock;
else if (c == 'P' || c == 'p')
player_choice = Choice::paper;
else if (c == 'S' || c == 's')
player_choice = Choice::scissors;
else if (c == 'E' || c == 'e')
break;
else
{
cout << "Invalid input." << endl;
continue;
}
// 电脑做出随机选择,并判断游戏结果
computer_choice = get_computer_choice();
int p = (int)player_choice;
int c = (int)computer_choice;
cout << "You chose " << words[p];
cout << "," << endl;
cout << "I chose " << words[c];
cout << "," << endl;
decide_winner();
}
// 退出游戏后,显示用户和电脑各自的得分
cout << "player wins: " << player_wins << endl;
cout << "computer wins: " << computer_wins << endl;
return EXIT_SUCCESS;
}
// 设置电脑相应的偏好
void assign_computer_prefs()
{
int n = rand0toN1(3);
if (n == 0)
{
comp_first_choice = Choice::rock;
comp_second_choice = Choice::scissors;
comp_third_choice = Choice::paper;
}
else if (n == 1)
{
comp_first_choice = Choice::scissors;
comp_second_choice = Choice::paper;
comp_third_choice = Choice::rock;
}
else
{
comp_first_choice = Choice::paper;
comp_second_choice = Choice::rock;
comp_third_choice = Choice::scissors;
}
}
Choice get_computer_choice()
{
int n = rand0toN1(10);
if (n <= 4) // 50%的几率选择comp_first_choice
return comp_first_choice;
else if (n <= 7) // 30%的几率选择comp_second_choice
return comp_second_choice;
return comp_third_choice; //20%的几率选择comp_third_choice
}
// 判断谁是胜利者
void decide_winner()
{
if (player_choice == computer_choice)
{
cout << "Result is a tie." << endl
<< endl;
return;
}
int p = (int)player_choice;
int c = (int)computer_choice;
if (p - c == 1 || p - c == -2)
{
cout << get_msg(player_choice);
cout << "YOU WIN!" << endl;
player_wins++;
}
else
{
cout << get_msg(computer_choice);
cout << "I WIN!" << endl;
computer_wins++;
}
cout << endl;
}
// 打印相应的提示信息
string get_msg(Choice winner)
{
if (winner == Choice::rock)
return string("Rock smashes scissors... ");
else if (winner == Choice::paper)
return string("Paper covers rock... ");
else
return string("Scissors cuts paper... ");
}
// 随机地返回0~n-1中的任一整数
int rand0toN1(int n)
{
return rand() % n;
}
再上面的基础上再更进一步,让程序在每次运行时都记录用户的选择,在若干回合后,程序根据统计得到用户的偏好后,重新调整自己的偏好,这样会让玩家觉得更有挑战性。
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace { enum class Choice { rock, paper, scissors }; };
// 用户和电脑的选择
Choice player_choice;
Choice computer_choice;
// 设置电脑的偏好
Choice comp_first_choice = Choice::rock;
Choice comp_second_choice = Choice::scissors;
Choice comp_third_choice = Choice::paper;
// 记录用户和电脑的游戏结果
int player_wins = 0;
int computer_wins = 0;
// 记录用户输入的选择
int player_n_rock = 0;
int player_n_scissors = 0;
int player_n_paper = 0;
string words[3] = { "rock", "paper", "scissors" };
Choice get_computer_choice();
void assign_computer_prefs();
void reset_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);
int main(int argc, char* argv[])
{
srand(time(NULL)); // 设置随机种子
assign_computer_prefs(); // 设置电脑的偏好
string input_str;
int c;
while (true)
{
int m = rand0toN1(10);
// 10个回合后,电脑重置偏好
if (m == 0)
reset_prefs();
cout << "Enter Rock, Paper, Scissors, or Exit: ";
getline(cin, input_str);
// 对输入进行校验
if (input_str.size() < 1)
{
cout << "Invalid input." << endl;
continue;
}
// 统计用户的选择
c = input_str[0];
if (c == 'R' || c == 'r')
{
player_choice = Choice::rock;
player_n_rock++;
}
else if (c == 'P' || c == 'p')
{
player_choice = Choice::paper;
player_n_paper++;
}
else if (c == 'S' || c == 's')
{
player_choice = Choice::scissors;
player_n_scissors++;
}
else if (c == 'E' || c == 'e')
{
break;
}
else
{
cout << "Invalid input." << endl;
continue;
}
// 电脑做出随机选择,并判断游戏结果
computer_choice = get_computer_choice();
int p = (int)player_choice;
int c = (int)computer_choice;
cout << "You chose " << words[p];
cout << "," << endl;
cout << "I chose " << words[c];
cout << "," << endl;
decide_winner();
}
// 退出游戏后,显示用户和电脑各自的得分
cout << "player wins: " << player_wins << endl;
cout << "computer wins: " << computer_wins << endl;
return EXIT_SUCCESS;
}
// 设置电脑相应的偏好
void assign_computer_prefs()
{
int n = rand0toN1(3);
if (n == 0) {
comp_first_choice = Choice::rock;
comp_second_choice = Choice::scissors;
comp_third_choice = Choice::paper;
} else if (n == 1) {
comp_first_choice = Choice::scissors;
comp_second_choice = Choice::paper;
comp_third_choice = Choice::rock;
} else {
comp_first_choice = Choice::paper;
comp_second_choice = Choice::rock;
comp_third_choice = Choice::scissors;
}
}
// 重置电脑相应的偏好
void reset_prefs()
{
if (player_n_rock > player_n_paper && player_n_rock > player_n_scissors)
{
comp_first_choice = Choice::paper;
comp_second_choice = Choice::rock;
comp_third_choice = Choice::scissors;
}
else if (player_n_paper > player_n_scissors)
{
comp_first_choice = Choice::scissors;
comp_second_choice = Choice::paper;
comp_third_choice = Choice::rock;
}
else
{
comp_first_choice = Choice::rock;
comp_second_choice = Choice::scissors;
comp_third_choice = Choice::paper;
}
}
Choice get_computer_choice()
{
int n = rand0toN1(10);
if (n <= 4) // 50%的几率选择comp_first_choice
return comp_first_choice;
else if (n <= 7) // 30%的几率选择comp_second_choice
return comp_second_choice;
return comp_third_choice; //20%的几率选择comp_third_choice
}
// 判断谁是胜利者
void decide_winner()
{
if (player_choice == computer_choice)
{
cout << "Result is a tie." << endl
<< endl;
return;
}
int p = (int)player_choice;
int c = (int)computer_choice;
if (p - c == 1 || p - c == -2)
{
cout << get_msg(player_choice);
cout << "YOU WIN!" << endl;
player_wins++;
}
else
{
cout << get_msg(computer_choice);
cout << "I WIN!" << endl;
computer_wins++;
}
cout << endl;
}
// 打印相应的提示信息
string get_msg(Choice winner)
{
if (winner == Choice::rock)
return string("Rock smashes scissors... ");
else if (winner == Choice::paper)
return string("Paper covers rock... ");
else
return string("Scissors cuts paper... ");
}
// 随机地返回0~n-1中的任一整数
int rand0toN1(int n)
{
return rand() % n;
}
参考资料:《C++ Without Fear 2nd Edtion》,中文译名为《好学的C++》