用C语言编写一个石头剪子布游戏,具体要求如下,高分急求
admin
2023-07-06 09:48:13
0
用C语言编写一个石头剪子布游戏,具体要求如下,高分急求
#include
#include
#include

int main()
{
char name[100];
int usr_select;
int system_select;
int num;
int usrwin=0,syswin=0,nowin=0;

printf("please in put you name:\n");
scanf("%s",name);
printf("please input how many games do you want to play:\n");
scanf("%d",#);

while (num == 0 || (num %2) == 0)
{
printf("the num must be singular,please input a right number again:\n");
scanf("%d",#);
}
int temp = 1;
for (temp;temp<=num; temp++)
{
printf("%d: input you chocice:(0:shi tou 1:jian zi 2:bu)",temp);
scanf("%d",&usr_select);
while( usr_select <0 || usr_select >2 )
{
printf("your choice is error,please input again(0:shi tou 1: jian zi 2:bu)\n");
scanf("%d",&usr_select);

}
srand(time(0));
system_select= rand()%3;
if(usr_select - system_select == -1 || usr_select-system_select == 2)
{
usrwin++;
printf("you win!\n");
}
else if(usr_select == system_select)
{
nowin++;
printf("no one win\n");
temp--;
}
else
{
syswin++;
printf("system win\n");
}
}
if(usrwin > syswin)
printf("%s is winner!\n",name);
else
printf("system is winner!\n");

}
#include
# include "time.h"

int getComputerType() {
srand((unsigned)time(NULL));
return rand()%3+1;
}

void printType(int type) {
switch (type) {
case 1:
printf("石头\n");
break;
case 2:
printf("剪刀\n");
break;
case 3:
printf("布\n");
break;
}
}

int Compare(int userType, int computerType) {
if (userType == computerType) {
return 0;
}

switch (computerType) {
case 1:
if (userType == 2) {
return -1;
} else {
return 1;
}
break;
case 2:
if (userType == 1) {
return 1;
} else {
return -1;
}
break;
case 3:
if (userType == 1) {
return -1;
} else {
return 1;
}
break;
}
}

int main() {
int i;
char name[20];
int rule;
int userType;
int computerType;
int userWinNum = 0;
int computerWinNum = 0;

while (1) {
printf("输入玩家名字:");
gets(name);
printf("选择模式1. 三局两胜;2. 五局三胜:");
scanf("%d", &rule);

if (rule != 1 && rule != 2) {
printf("模式选择错误!请输入1或2进行选择。\n\n");
continue;
}

printf("1.石头;2.剪刀;3. 布");

for (i=1; i computerType = getComputerType();
do{
printf("第%d局,请出:");
scanf("%d", &userType);
} while(userType == 1 || userType == 2 || userType == 3);

printf("计算机:");
printType(computerType);
printf("%s:", name);
printType(userType);

switch (Compare(userType, computerType)) {
case 0:
i--;
printf("平局,此局重来\n");
break;
case 1:
userWinNum++;
printf("你赢了!\n");
break;
case -1:
computerWinNum++;
printf("你输了!\n");
break;
}

printf("当前比分:\n%s:%d\n电脑:%d\n", name, userWinNum, computerWinNum);

if (userWinNum == (rule*2+1)/2 + 1) {
printf("恭喜你,你赢了!!\n");
break;
} else if (computerWinNum == (rule*2+1)/2 + 1) {
printf("很不幸,你输了!!\n");
break;
}
}
}
}

相关内容

热门资讯

原神静态风景后续宝箱在哪里(原... 原神游戏是一款非常好玩的自由世界冒险游戏,旅行者们需要通过收集宝箱来获得资源奖励,这些宝箱的寻找难度...
原神失梦的苗圃怎么做(原神3.... 在原神游戏中,玩家们可以自由地做着任何自己喜欢的事情,在高自由度的游戏玩法下,也不能偷懒不提升自己的...
原神放松疗法成就怎么做(原神3... 在原神3.0版本里面,玩家们迎来了许多全新的角色与任务活动,许多的任务难倒了玩家们,其中就有原神放松...
这就是12代平台的红利?i5 ... 文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章...
分享RTX4090和4080的... 文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章...
专注读与写,汉王n10电纸本—... 文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章图片文章...
普通高中|海报丨今年27所军队... 【 普通高中|海报丨今年27所军队院校计划招普通高中毕业生1.3万余人】摘要:我们欢迎广大考生携笔从...