内存:128  时间:1

题目描述

输入

输出

样例输入

样例输出

提示

代码如下

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main_menu();
void sub_menu();
void AtoB(char roleA,char roleB,int n);
void show_score(int mode,int score1,int score2);
void show_winner(int mode,int score1,int score2);
int one_round(char roleA,char roleB);
char make_choice(char role);
void show_choice(char ch,char choice);
int pk(char ch1,char ch2);
char regular(char ch1,char ch2);
int mode,count;
char A,B,C,D;
void main_menu()
{
  printf("		 -------------------------------------------
");
  printf("		|               石头剪刀布游戏              |
");
  printf("		|        ----------------------------       |
");
  printf("		|                                           |
");
  printf("		|请选择对战模式:                           |
");
  printf("		|         人人对战        1                 |
");
  printf("		|         人机对战        2                 |
");
  printf("		|                                           |
");
  printf("		|注:                                       |
");
  printf("		| 用户操作:                                 |
");
  printf("		|                                           |
");
  printf("		|         1-石头  2-剪刀  3-布              |
");
	printf("		 -------------------------------------------


");
}
void sub_menu()
{
	
	printf("		 -------------------------------------------
");
  printf("		|               请选择对战局数              |
");
  printf("		|        ----------------------------       |
");
  printf("		|                                           |
");
  printf("		|请选择对战模式:                           |
");
  printf("		|         三局两胜        3                 |
");
  printf("		|         五局三胜        5                 |
");
  printf("		|          自定义	(输入你希望的局数) |
");
  printf("		|注:                                       |
");
  printf("		| 用户操作:                                 |
");
  printf("		|                                           |
");
  printf("		|         1-石头  2-剪刀  3-布              |
");
	printf("		 -------------------------------------------
");
	return;
 } 
void AtoB(char roleA,char roleB,int n)
{
  int i,f,score1=0,score2=0;
  for(i=0;i<n;i++)
  {
  	f=one_round(roleA,roleB);
  	if(f==1)
  	score1++;
  	if(f==2)
  	score2++;
	} 
	show_score(mode,score1,score2);
	show_winner(mode,score1,score2);
}
void show_winner(int mode,int score1,int score2) 
{
	if(score1==score2)
		printf("		平局
");
	else if(score1>score2)
		printf("		玩家A获胜
");
	else if(mode==1)
		printf("		玩家B获胜
");
	else if(mode==2)
		printf("		电脑获胜
");
}
int one_round(char roleA,char roleB)
{
	int winner;
	char choiceA,choiceB;
	choiceA=make_choice(roleA);
	show_choice(roleA,choiceA);
	choiceB=make_choice(roleB);
	show_choice(roleB,choiceB);
	winner=pk(choiceA,choiceB); 
	return winner;
}
char make_choice(char role)
{
	char c;
	printf("请输入你的选择:	");
	if(role=='A'||role=='B')
			while(1)
			 {
			 	c= getchar();
               if(c=='1'||c=='2'||c=='3')
                    return c;
                }
	if(role=='C')
		{
		srand(time(NULL));
		c= rand()%3+1+48;
		return c;}
}
void show_choice(char ch,char choice)
{	
		if(ch=='A'||ch=='B')
			printf("玩家%c的选择是:	",ch);
		if(ch=='C')
			printf("电脑的选择是:	");
		if(choice=='1')
			   printf("石头 
 ");
		if(choice=='2')
        	printf("剪刀 
");
       	if(choice=='3')
            printf("布 
");
}
int pk(char ch1,char ch2)
{
	char f;
	f=regular(ch1,ch2);
	if(f=='A')
			return 1;
	if(f=='B')
			return 2;
	if(f=='D')
			return 0;
}
void show_score(int mode,int score1,int score2)
{
  printf("


");
  printf("		 -------------------TOTAL-------------------
");
  if(mode==1)
  printf("		|          人人对战您一共玩了%d次           |
",count);
  if(mode==2) 
  printf("		|          人机对战您一共玩了%d次           |
",count);
  printf("		 ---------------------|-------------------- 
");
  printf("		|        胜利         |         %d 次       |
",score1);
  printf("		 ---------------------|-------------------- 
");
  printf("		|        失败         |         %d 次       |
",score2);
  printf("		 ---------------------|-------------------- 
");
  printf("		|        平手         |         %d 次       |
",count-score1-score2);
  printf("		 ---------------------|-------------------- 
");
 } 
char regular(char ch1,char ch2)
{ 
	switch(ch1-ch2)
          {
               case 0:
         return 'D';
               case -1: case 2:
      
      	return 'A';
               case 1: case -2:
      	return 'B';
      			default:
      	return 'D';
          }
}
int main()
{
	main_menu();
	printf("		请输入您的选择:	");
	scanf("%d",&mode);
	sub_menu();
	printf("		请输入您的选择:	");
	scanf("%d",&count);
	if(mode==1)
	AtoB('A','B',count);
	if(mode==2)
	AtoB('A','C',count);
	return 0;
}

代码来源于互联网,仅供参考!