c++基础代码
#include<iostream>
using namespace std;
#include<ctime>
#include<string>
void grade(int sum, string computername)
{
cout << "****************************" << endl;
cout << "最后结果为:";
if (sum == 0)
{
cout << "平局" << endl;
}
if (sum >= 1)
{
cout << "你赢了" << endl;
}
if(sum<=-1)
{
cout << computername << "赢" << endl;
}
}
int iswin(int player, int computer, string computername)
{
if (player == computer)
{
cout << "平局" << endl;
return 0;
}
if(player<computer)
{
if(computer!=3)
{
cout<<"你赢了"<<endl;
return 1;
}
cout<<computername<<"赢"<<endl;
return -1;
}
if(player>computer)
{
if(computer!=2)
{
cout<<"你赢了"<<endl;
return 1;
}
cout<<computername<<"赢"<<endl;
return -1;
}
cout << "*****************************" << endl;
}
void game()
{
int sum = 0;
string shoushi[4] = { " ","石头","剪刀","布" };
string computername[4] = { " ","张三","李四","王五" };
srand((unsigned int)time(NULL));
int select = 0;
cout << "猜拳游戏(三局两胜)" << endl;
cout << "玩家:";
cout << "1.张三 2.李四 3.王五" << endl;
int select1;
cout << "请输入对弈的玩家:";
cin >> select1;
cout << "您选择的玩家是:" << computername[select1] << endl;
cout << "1.石头" << endl;
cout << "2.剪刀" << endl;
cout << "3.布" << endl;
for (int i = 1; i <= 3; i++)
{
cout << "请您输入手势:";
cin >> select;
int computer = rand() % 2 + 1;
cout << "您出" << shoushi[select] << endl;
cout <<computername[select1]<< "出" << shoushi[computer] << endl;
int a = iswin(select, computer, computername[select1]);
sum = sum + a;
if (sum == 2 || sum == -2)
{
break;
}
}
grade(sum, computername[select1]);
}
int main()
{
cout << "*************<<猜拳游戏>>*************" << endl;
cout << "*************1.进入游戏****************" << endl;
cout << "*************2.退出游戏****************" << endl;
while (true)
{
cout << "请输入数字进行操作:";
int n = 0;
cin >> n;
if (n==1||n==2)
{
switch (n)
{
case 1:
game();
break;
case 2:
cout << "欢迎下次使用!" << endl;
return 0;
}
}
else
{
cout << "输入错误,请退出重试!" << endl;
return 0;
}
cout << "1.在玩一次" << endl;
cout << "2.退出游戏" << endl;
}
return 0;
}
效果
感兴趣的可以试试!