#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;
}
}
}