剪刀石头布游戏。

Write a program that pits the human user against the computer in a game of “Rock, Paper, Scissors” as in the output below.
Computer and user each choose one: R for Rock, P for Paper, S for Scissor.
Each round, your program declares one the winner (ask user to enter his/her name; give the computer a name)
At end of game, winner is the one with the most wins.

Output of the game should be following:
在这里插入图片描述
方法一:

#include<cstdio>
#include<cstdlib>
#include<ctime>

#define types_length 3

char types[]={'R','P','S'};//系统出的字符 
char types_beat[]={'S','R','P'};//玩家输入的字符 
//比较玩家和系统的字符情况,石头R克剪刀S,布P克石头R,剪刀S克布P 
int match(char a,char b)//形参a接受系统随机产生的字符,形参b接受玩家输入的字符 
{
	if(a==b)//如果统随机产生的字符等于玩家输入的字符  
		return 0;//则返回0 
	for(int i=0;i<types_length;i++)//遍历数组types  
	{
		if(a==types[i])//如果系统随机产生的字符是字符R、P、S中的一个时
			return b==types_beat[i]?1:-1;//判断玩家输入的字符是不是S、R、P,如果是,则a克b,就返回1,否则返回-1 
	}
	return 0;
}

int main() 
{
    char name[500],input[500],p_choice,c_choice;//p_choice是玩家输入的字符,c_choice是系统输入的字符 
	int p_count=0,c_count=0;//p_count是玩家赢的次数,c_count是系统赢的次数,初始化这两个变量 
	srand((unsigned int)(time(NULL)));//用time(0)初始化种子,这个种子值就为time(0),也就是randon接收到的值
	//time(0)是1970年1月1日零时零分零秒到目前为止所经过的秒数,然后用unsigned int把它转化为无符号整形值,这个值是不断变化的
	//所以rand接收到的种子time(0)也是随机变化的								
    printf("My name is GameBot,what is your name? ");
    scanf("%s",name);//输入玩家姓名 
    while(true)//无限循环 
	{
        printf("Choose a hand to play:\n");
        printf(" Type R for rock, P for Paper, or S for Scissors.\n");
        printf(" When you want to stop, type E for end instead.\n");
        printf(" Your Choice: ");
        getchar();//吸收输入姓名所产生的的回车符 
        scanf("%c",&input[0]);//输入数组首元素 
		if(input[0]!='E')//当输入数组input的首元素不是字符E的时候,执行下面的操作 
		{
			p_choice=input[0];//把输入的数组首元素的ASCII码赋值给p_choice 
			c_choice=types[rand()%3];//rand()是种子time(0)的值,rand%3是rand÷3得到的余数,而这个数只能是012三者中的一个
			//types[rand()%3]的意思是字符R、P、S三者中的一个,c_choice的值就是R、P、S三者中的一个任一一个ASCII的值 
			printf(" You picked %c, I picked %c.\n",p_choice,c_choice);//输出你之前输入的字符和还有系统随机生成的字符R、P、S中的一个 
			switch(match(c_choice,p_choice))//调用match函数 
			{
				case 1:
					printf(" I win! HAHAHAHA!!");c_count++;break;//这种情况系统赢,系统赢的次数+1,跳出switch选择结构继续执行下一个for循环 
				case 0:
					printf(" We are tired. No Winner. Try again.");break;//这种情况是平局,跳出switch选择结构继续执行下一个for循环 
				case -1:
					printf(" %s, YOU WIN !!!!", name);p_count++;break;//这种情况玩家赢了,玩家赢的次数+1,跳出switch选择结构继续执行下一个for循环
			}			
		}
		else//当输入字符串(数组input)首元素不是字符E的时候,跳出while循环 
			break;		
    }
    printf("Thank you for playing, %s\n",name);
	printf("You won %d hands.\n",p_count); 
	printf("I won %d hands.\n",c_count);
	if(p_count>c_count)//若玩家赢的次数>系统赢的次数
		printf("You won the game!\n");//输出玩家赢了 
	else if(c_count>p_count)//若系统赢的次数>玩家赢的次数
		printf("I won the game!\n");//输出系统赢了 
	else//若玩家赢的次数=系统赢的次数
		printf("We tied!\n");//则输出是平局 
}

在VS2019下,需将方法一的scanf做一些修改,所以VS2019下的方法一为:

#include<cstdio>
#include<cstdlib>
#include<ctime>

#define types_length 3

char types[] = { 'R','P','S' };//系统出的字符 
char types_beat[] = { 'S','R','P' };//玩家输入的字符 
//比较玩家和系统的字符情况,石头R克剪刀S,布P克石头R,剪刀S克布P 
int match(char a, char b)//形参a接受系统随机产生的字符,形参b接受玩家输入的字符 
{
	if (a == b)//如果统随机产生的字符等于玩家输入的字符  
		return 0;//则返回0 
	for (int i = 0; i < types_length; i++)//遍历数组types  
	{
		if (a == types[i])//如果系统随机产生的字符是字符R、P、S中的一个时
			return b == types_beat[i] ? 1 : -1;//判断玩家输入的字符是不是S、R、P,如果是,则a克b,就返回1,否则返回-1 
	}
	return 0;
}

int main()
{
	char name[500], input[500], p_choice, c_choice;//p_choice是玩家输入的字符,c_choice是系统输入的字符 
	int p_count = 0, c_count = 0;//p_count是玩家赢的次数,c_count是系统赢的次数,初始化这两个变量 
	srand(unsigned int(time(NULL)));//用time(0)初始化种子,这个种子值就为time(0),也就是randon接收到的值
	//time(0)是1970年1月1日零时零分零秒到目前为止所经过的秒数,然后用unsigned int把它转化为无符号整形值,这个值是不断变化的
	//所以rand接收到的种子time(0)也是随机变化的								
	printf("My name is GameBot,what is your name? ");
	scanf_s("%s", name, unsigned int(sizeof(name)));//输入玩家姓名 
	while (true)//无限循环 
	{
		printf("Choose a hand to play:\n");
		printf(" Type R for rock, P for Paper, or S for Scissors.\n");
		printf(" When you want to stop, type E for end instead.\n");
		printf(" Your Choice: ");
		getchar();//吸收输入姓名所产生的的回车符 
		scanf_s("%c", &input[0], unsigned int(sizeof(input[0])));//输入数组首元素 
		if (input[0] != 'E')//当输入数组input的首元素不是字符E的时候,执行下面的操作 
		{
			p_choice = input[0];//把输入的数组首元素的ASCII码赋值给p_choice 
			c_choice = types[rand() % 3];//rand()是种子time(0)的值,rand%3是rand÷3得到的余数,而这个数只能是012三者中的一个
			//types[rand()%3]的意思是字符R、P、S三者中的一个,c_choice的值就是R、P、S三者中的一个任一一个ASCII的值 
			printf(" You picked %c, I picked %c.\n", p_choice, c_choice);//输出你之前输入的字符和还有系统随机生成的字符R、P、S中的一个 
			switch (match(c_choice, p_choice))//调用match函数 
			{
			case 1:
				printf(" I win! HAHAHAHA!!"); c_count++; break;//这种情况系统赢,系统赢的次数+1,跳出switch选择结构继续执行下一个for循环 
			case 0:
				printf(" We are tired. No Winner. Try again."); break;//这种情况是平局,跳出switch选择结构继续执行下一个for循环 
			case -1:
				printf(" %s, YOU WIN !!!!", name); p_count++; break;//这种情况玩家赢了,玩家赢的次数+1,跳出switch选择结构继续执行下一个for循环
			}
		}
		else//当输入字符串(数组input)首元素不是字符E的时候,跳出while循环 
			break;
	}
	printf("Thank you for playing, %s\n", name);
	printf("You won %d hands.\n", p_count);
	printf("I won %d hands.\n", c_count);
	if (p_count > c_count)//若玩家赢的次数>系统赢的次数
		printf("You won the game!\n");//输出玩家赢了 
	else if (c_count > p_count)//若系统赢的次数>玩家赢的次数
		printf("I won the game!\n");//输出系统赢了 
	else//若玩家赢的次数=系统赢的次数
		printf("We tied!\n");//则输出是平局 
}

方法一变形:

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

#define types_length 3

using namespace std;

char types[] = { 'R','P','S' };//系统出的字符 
char types_beat[] = { 'S','R','P' };//玩家输入的字符
//比较玩家和系统的字符情况,石头R克剪刀S,布P克石头R,剪刀S克布P
int match(char a, char b)//形参a接受系统随机产生的字符,形参b接受玩家输入的字符 
{
	if (a == b)//如果统随机产生的字符等于玩家输入的字符  
		return 0;//则返回0 
	for (int i = 0; i < types_length; i++)//遍历数组types 
	{
		if (a == types[i])//如果系统随机产生的字符是字符R、P、S中的一个时
			return b == types_beat[i] ? 1 : -1;//判断玩家输入的字符是不是S、R、P,如果是,则a克b,就返回1,否则返回-1 
	}
	return 0;
}

int main()
{
	char name[500], input[500], p_choice, c_choice;//p_choice是玩家输入的字符,c_choice是系统输入的字符 
	int p_count = 0, c_count = 0;//p_count是玩家赢的次数,c_count是系统赢的次数,初始化这两个变量 
	srand((unsigned int)(time(NULL)));//用time(0)初始化种子,这个种子值就为time(0),也就是randon接收到的值
	//time(0)是1970年1月1日零时零分零秒到目前为止所经过的秒数,然后用unsigned int把它转化为无符号整形值,这个值是不断变化的
	//所以rand接收到的种子time(0)也是随机变化的								
	cout << "My name is GameBot,what is your name? ";
	cin >> name;//输入玩家姓名 
	while (true)//无限循环 
	{
		cout << "Choose a hand to play:\n";
		cout << " Type R for rock, P for Paper, or S for Scissors.\n";
		cout << " When you want to stop, type E for end instead.\n";
		cout << " Your Choice: ";
		getchar();//吸收输入姓名所产生的的回车符
		cin >> input[0];//输入数组首元素 
		if (input[0] != 'E')//当输入数组input的首元素不是字符E的时候,执行下面的操作  
		{
			p_choice = input[0];//把输入的数组首元素的ASCII码赋值给p_choice 
			c_choice = types[rand() % 3];//rand()是种子time(0)的值,rand%3是rand÷3得到的余数,而这个数只能是012三者中的一个
			//types[rand()%3]的意思是字符R、P、S三者中的一个,c_choice的值就是R、P、S三者中的一个任一一个ASCII的值 
			cout << " You picked " << p_choice << ", I picked " << c_choice << ".\n";
			switch (match(c_choice, p_choice))//调用match函数  
			{
			case 1:
				cout << " I win! HAHAHAHA!\n"; c_count++; break;//这种情况系统赢,系统赢的次数+1,跳出switch选择结构继续执行下一个for循环
			case 0:
				cout << " We are tired. No Winner. Try again.\n"; break;//这种情况是平局,跳出switch选择结构继续执行下一个for循环
			case -1:
				cout << " " << name << ", YOU WIN !!!!\n"; p_count++; break;//这种情况玩家赢了,玩家赢的次数+1,跳出switch选择结构继续执行下一个for循环
			}
		}
		else
			break;
	}
	cout << "\nThank you for playing, " << name << "\n";
	cout << "You won " << p_count << " hands.\n";
	cout << "I won " << c_count << " hands.\n";
	if (p_count > c_count)//若玩家赢的次数>系统赢的次数
		cout << "You won the game!\n";//输出玩家赢了 
	else if (c_count > p_count)//若系统赢的次数>玩家赢的次数
		cout << "I won the game!\n";//输出系统赢了 
	else//若玩家赢的次数=系统赢的次数
		cout << "We tied!\n";//则输出是平局 
}

方法二:

#include<iostream>
#include<cstdlib>
#include<ctime>
#include <stdio.h>

using namespace std;

int main()
{
    int your_win = 0, i_win = 0;//your_win是系统赢的次数,i_win是玩家赢的次数 
    char Y1[500];
    cout << "My name is GameBot,what is your name? ";
    cin >> Y1;//输入名字

    //while循环的思路是:我先出,我出的不是E就给系统出,比较我和系统出的关系,根据关系判断第一次谁赢和赢的次数,然后继续循环,直到输入字符'E'结束 
    while (true)
    {
        enum Choice { rock, scissors, paper, null };//enum Choice为枚举类型,等价于int,定义枚举元素/常量rock,scissors,paper,null,系统默认值分别为0123
        enum Result { you, me, tie };//enum Choice为枚举类型,等价于int,定义枚举元素/常量you,me,tie
        enum Choice yours, mine;//enum Choice为枚举类型,等价于int,定义我输入的数值和系统输入的数值 
        enum Result result;//enum Choice为枚举类型,等价于int 
        string s1;//把字符串s1初始化为空串 
        cout << "Choose a hand to play:" << endl;
        cout << " Type R for Rock,P for Paper,S for Scissors" << endl;
        cout << " When you want to stop,type E for End instead" << endl;
        cout << " Your choice: ";
        cin >> s1;//输入字符串s1 
        switch (s1[0])//玩家情况
        {
        case 's':
        case 'S':yours = scissors; cout << "You picked S. "; break;
        case 'r':
        case 'R':yours = rock; cout << "You picked R. "; break;
        case 'p':
        case 'P':yours = paper; cout << "You picked P. "; break;
        case 'E':yours = null; break;
        }

        if (yours != null)//当输入的字符串s1首字符不是字符E的时候,执行下面的操作 
        {
            srand((unsigned int)time(0));
            int n = rand();
            switch (n % 3)//系统情况
            {
            case 0:mine = scissors; cout << "I picked S." << endl; break;//这种情况系统出剪刀 
            case 1:mine = rock; cout << "I picked R." << endl; break;//这种情况系统出石头 
            case 2:mine = paper; cout << "I picked P." << endl; break;//这种情况系统出布 
            }
            if (mine == scissors && yours == paper || mine == rock && yours == scissors || mine == paper && yours == rock)//这些情况为玩家赢 
            {
                result = you;
            }
            if (mine == paper && yours == scissors || mine == scissors && yours == rock || mine == rock && yours == paper)//这些情况为系统赢 
            {
                result = me;
            }
            if (yours == mine)//这些情况为平局 
            {
                result = tie;
            }
            switch (result)
            {
            case you:cout << Y1 << ",YOU WIN!!!"; your_win++; break;//这种情况玩家赢了,玩家赢的次数+1,跳出switch选择结构继续执行下一个while循环
            case me:cout << "I win! HAHAHAHA!!"; i_win++; break;//这种情况系统赢,系统赢的次数+1,跳出switch选择结构继续执行下一个while循环
            case tie:cout << "We are tied. No winner. Try again."; break;//这种情况是平局,跳出switch选择结构继续执行下一个while循环
            }
        }
        else
            break;
    }
    printf("Think you for playing the game,%s\n", Y1);
    printf("I won %d hands.\n", i_win);
    printf("Your won %d hands.\n", your_win);
    if (i_win > your_win)//若系统赢的次数>玩家赢的次数
    {
        printf("I won the game\n");//输出系统赢了 
    }
    else if (i_win < your_win)//若玩家赢的次数>系统赢的次数
    {
        printf("You won the game\n");//输出玩家赢了 
    }
    else//若系统赢的次数=玩家赢的次数
    {
        printf("We tied\n");//则输出是平局 
    }

    return 0;
}

方法二简化版:

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

using namespace std;

int getIt()
{
	srand((unsigned int)time(NULL));
	char list[] = { 'S', 'R', 'P' };
	char n = list[rand() % 3];
	return n;
}

int main()
{

	int win = 0, lose = 0, draw = 0;
	char a,n;
	cout << "My name is GameBot.What is your name?Nightmare" << endl;

	while (true)
	{
		cout << "Choose a hand to play:" << endl;
		cout << " Type R for Rock,P for Paper,S for Scissors" << endl;
		cout << " When you want to stop,type E for End instead" << endl;
		cout << " Your choice: ";
		cin >> a;
		if (a == 'E') break;
		switch (a)//玩家信息 
		{
		case 'S':cout << "You picked S. "; break;
		case 'R':cout << "You picked R. "; break;
		case 'P':cout << "You picked P. "; break;
		}
		n = getIt();
		switch (n)//系统信息 
		{
		case 'S': cout << "I picked S." << endl; break;
		case 'R': cout << "I picked R." << endl; break;
		case 'P': cout << "I picked P." << endl; break;
		}
		if (a == n)
		{
			cout << "We are tied. No winner. Try again.";
		}
		if ((a == 'S' && n == 'R') || (a == 'R' && n == 'P') || (a == 'P' && n == 'S'))//系统赢 
		{
			cout << "I win HAHAHAHA!!";
			lose++;//系统赢得次数+1 
		}
		if ((a == 'R' && n == 'S') || (a == 'P' && n == 'R') || (a == 'S' && n == 'P'))//玩家赢 
		{
			cout << "Nightmare YOU WIN!!!!";
			win++;//玩家赢得次数+1 
		}
	}
	printf("Thank you for playing,Nightmare\n");
	printf("You won %d hands.\n", lose);
	printf("I won %d hands.\n", win);
	if (lose > win)//若系统赢的次数>玩家赢的次数
	{
		printf("I won the game\n");//输出系统赢了 
	}
	else if (win > lose)//若玩家赢的次数>系统赢的次数
	{
		printf("You won the game\n");//输出玩家赢了 
	}
	else//若系统赢的次数=玩家赢的次数
	{
		printf("We tied\n");//则输出是平局 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值