用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;
}
}
}
}
相关内容