用C语言编写一个游戏:剪刀,石头,布的,用随即数来实现。哪位大大帮帮

不用太复杂,简单的实现就行了。
2024-12-24 16:47:22
推荐回答(4个)
回答1:

/************************************************
* Copyright(C) 2007 babybubble
* Author: babybubble
* Date: 2007-9-20
* Description: 0表示剪刀,1表示石头,2表示布
**********************************************/
#include
#include
#include

/***********************
* a与b游戏
* 如果a赢,返回1
* 如果a输,返回-1
* 如果相同,返回0
***********************/
int fight(int a, int b)
{
int result;
result=a-b;
if (result==2) result=-1;
else if (result==-2) result=1;
return result;
}
int main()
{
int computer,player;
srand(time(0));
while (1)
{
printf("0 剪刀\n1 石头\n2 布\n3 退出\n");
printf("please input your choice number: ");
scanf("%d",&player);
if (player>=0&&player<=2)
{
computer=rand()%3;
if (computer==0) printf("我出剪刀!\n");
else if (computer==1) printf("我出石头!\n");
else printf("我出布!\n");
if (fight(computer,player)==1) printf("I win!!! ^_^\n");
else if (fight(computer,player)==-1) printf("You win... T_T\n");
else printf("The same\n");
}
else if (player==3) break;
else printf("wrong input!\n");
}
return 0;
}

回答2:

相辅相存

回答3:

#include
#include
void main()
{
int computer;
int player;
int tmp;
char *v[3]={"shitou","jiandao","bu"};
char ch;

srand(time(NULL));

while(1)
{
system("cls");/**/
computer=rand()%3;
printf("\n(0)shitou\t(1)jiandao\t(2)bu\n");
printf("Please input your choice:");
scanf("%d", &player);
if(player<0 || player>2) continue;
printf("\nComputer:\t%s",v[computer]);
printf("\nYou:\t\t%s\n\n",v[player]);

tmp = player - computer;
if(tmp == 1 || tmp == -2) printf("Computer Win!\n\n");
if(tmp == -1 || tmp == 2) printf("You Win!\n\n");

printf("\n****************************************************");
printf("\npress 'n' or 'N' to quit! any other key to continue!");
ch =getch();

if(ch=='n' || ch=='N') return;
}
}

回答4:

同意楼上的,不过需要再加入两个头文件
#include
#include