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

不用太复杂,简单的实现就行了。

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

/***********************
* 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;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2007-09-20
相辅相存
第2个回答  2007-09-20
#include<stdio.h>
#include<time.h>
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;
}
}
第3个回答  2007-09-20
同意楼上的,不过需要再加入两个头文件
#include<stdlib.h>
#include<conio.h>
相似回答