石头剪刀布

本文介绍了一个使用C++实现的无限循环石头剪刀布游戏,AI与玩家进行对决,根据游戏规则判断胜负并进行201积分制计分。玩家输入与AI的随机选择进行比较,输出每轮结果并更新分数,直至游戏结束。同时,预告下期将分享一个计算器(栈)程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

输出分为几个部分:

1:说明

石头(0)剪刀(1)布(2)

石头0>剪刀1

剪刀1>布 2

布2>石头0

2:游戏

无限循环,

AI随机出一个,

接着再由你出,

两者PK,

输出AI和你的输入

 输出胜负

3:计分

201积分制

胜利+2分,失败不加,平局各加1分

每次把分数输出一遍

导入库

<iostream> 输入,输出

<ctime> 随机数辅助库

<cstdlib> 随机数

#include <iostream>
#include <ctime>
#include <cstdlib>

 变量

a(AI),b(你),n(胜负),s1(你的分数),s2(AI分数)

int a,b,n,s1=0,s2=0;

判断胜负(用函数)

int PSS(int a,int b){
	if (a==b) return 1;
	if (a==0){
		if (b==1) return 0;
		else return 2;
	}
	if (a==1){
		if (b==0) return 2;
		else return 0;
	}
	if (a==2){
		if (b==0) return 0;
		else return 2;
	}
}

其中2代表胜利,1平局,0失败

输入和比较

cout<<endl;
srand(time(0));
a=rand()%3;
cin>>b;
b=b%3;
n=PSS(a,b);

计分板操作

switch (n){
   	case 1:s1++;s2++;break;
    case 2:s1+=2;break;
    case 0:s2+=2;break;
}

输出操作

cout<<"COMPUTER>> "<<a<<"VS"<<b<<" <<YOU"<<endl;
switch (n){
	case 1:cout<<"平局";break;
	case 2:cout<<"胜利";break;
	case 0:cout<<"失败";break;
}
cout<<endl;
switch (n){
	case 1:s1++;s2++;break;
	case 2:s1+=2;break;
	case 0:s2+=2;break;
}
cout<<"COMPUTER>> "<<s2<<":"<<s1<<" <<YOU"<<endl;

代码展示

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int PSS(int a,int b){
	if (a==b) return 1;
	if (a==0){
		if (b==1) return 0;
		else return 2;
	}
	if (a==1){
		if (b==0) return 2;
		else return 0;
	}
	if (a==2){
		if (b==0) return 0;
		else return 2;
	}
}
int main(){
	cout<<"石头=0,剪刀=1,布=2"<<endl;
    cout<<"石头WIN剪刀,剪刀WIN布,布WIN石头"<<endl;
    int a,b,n,s1=0,s2=0;
    while (1){
    	cout<<endl;
    	srand(time(0));
    	a=rand()%3;
    	cin>>b;
    	b=b%3;
    	n=PSS(a,b);
    	cout<<"COMPUTER>> "<<a<<"VS"<<b<<" <<YOU"<<endl;
    	switch (n){
    		case 1:cout<<"平局";break;
    		case 2:cout<<"胜利";break;
    		case 0:cout<<"失败";break;
		}
		cout<<endl;
		switch (n){
    		case 1:s1++;s2++;break;
    		case 2:s1+=2;break;
    		case 0:s2+=2;break;
		}
		cout<<"COMPUTER>> "<<s2<<":"<<s1<<" <<YOU"<<endl;
	}
	return 0;
}

(作者第二次发IT文章,希望大家能承蒙关照)

你们还想让我编点什么?

在QQ邮箱3547877477@qq.com告诉我吧!

下期预告:

计算器(栈)程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoliuAH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值