猜拳代码(排行榜,对战记录,开始游戏)

#include <stdio.h>

#include <stdlib.h>

int userchoice() {

 int userchoice;

 do {

  scanf("%d",&userchoice);

  if(userchoice>0&&userchoice<4) {

   break;

  } else {

   printf("输入有误,请再次输入");

  }

 } while(1);

 return userchoice;

}

 

int computerchoice() {

 int computerchoice;

 computerchoice = rand()%3+1;

 return computerchoice;

}

 

choice(int choice) {

 switch(choice) {

  case 1:

   printf("石头");

   break;

  case 2:

   printf("剪刀");

   break;

  case 3:

   printf("布");

   break;

 }

 printf("\n");

 return 0;

}

int result(int p,int Playerscore[],int Computerscore[],int rankinglist[]) {

 int a=0;

 int userwin=0;

 int computerwin=0;

 int userChoice,computerChoice;

 while(1) {

  printf("\n请你选择你要出的拳头[1-石头,2-剪刀,3-布]\n");

  userChoice = userchoice();

  computerChoice = computerchoice();

  printf("你的出拳是:");

  choice(userChoice);

  printf("电脑的出拳是:");

  choice(computerChoice);

  if(userChoice == computerChoice) {

   printf("这把平局,再来一次\n");

  } else if(userChoice == 1 && computerChoice == 2 || userChoice == 2 && computerChoice == 3 || userChoice == 3 && computerChoice == 1) {

   userwin++;

   printf("这把你赢了!太强了!\n");

  } else {

   computerwin++;

   printf("这把电脑赢了!别灰心!\n");

  }

  printf("\n");

  do {

   printf("请输入序号来选择继续或者退出游戏\n1-继续\n2-停止\n");

   scanf("%d",&a);

   if(a==1) {

    break;

   } else if(a==2) {

    Playerscore[p]=userwin;

    Computerscore[p]=computerwin;

    rankinglist[p] =userwin;

 

    return 0;

   } else {

    printf("输入错误,请再次输入\n");

   }

  } while(a!=1||a!=2);

 

 }

 

 

 

}

int main () {

 int p=0;

 int a;

 int t;

 int x,y; //进入游戏

 int i=0,j=0;

 char Playername[10];

 int Playerscore[20];

 int Computerscore[20];

 int rankinglist[100]= {0};

 printf("===========请输入您的用户名============\n\n");

 printf("您的用户名是:");

 scanf("%s",&Playername);

 while(1) {

  int type;

  printf("请输入你要选择的菜单:\n");

  printf("----------------游戏菜单----------------\n");

  printf("-------------1.开始游戏-----------------\n");

  printf("-------------2.对战记录-----------------\n");

  printf("-------------3.排行榜-------------------\n");

  printf("-------------4.退出游戏-----------------\n");

  while(1) {

   scanf("%d",&type);

   printf("\n\n");

   if(type==1||type==2||type==3||type==4) {

    break;

   } else {

    printf("输入有误,再试一遍\n");

   }

  }

  switch(type) {

   case 1:

    printf("你的选择是开始游戏,现在你已经进入游戏\n");

    printf("=============开始游戏=============\n");

    result(p,Playerscore,Computerscore,rankinglist);

    p++;

    break;

   case 2:

    i=0;

    printf("你的选择是对战记录,以下是您的对战记录\n");

    printf("=============对战记录=============\n");

    printf("用户名为: %s\n",Playername);

    if(p==0) {

     printf("暂无对战记录,快去打把游戏吧!\n");

     break;

    }

    printf("---------玩家vs电脑\n");

    for(i=0; i<p; i++) {

     printf("%d.--------%d vs %d\n",i+1,Playerscore[i],Computerscore[i]); 

    }

    printf("\n\n");

    break;

   case 3:

    printf("你的选择是排行榜,以下是您的排行榜\n");

    printf("==============排行榜=============\n");

    printf("用户名为:%s\n",Playername);

    if(p==0) {

     printf("暂无分数记录,快去打把游戏吧!\n");

     break;

    }

    for(i=1; i<150; i++) {

     for(j=1; j<p+1; j++) {

      if(rankinglist[j-1]<rankinglist[j]) {

       t=rankinglist[j-1];

       rankinglist[j-1]=rankinglist[j];

       rankinglist[j]=t;

      }

     }

    }

    i=j=0;

    for(i=0; i<p; i++) {

     printf("%d.-----%d\n",i+1,rankinglist[i]);

    }

    printf("\n\n");

 

    break;

   case 4:

    printf("您已退出游戏\n");

    return 0;

 

 

 

 

 

  }

 }

}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值