猜拳

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int random(int maxlim);

int judgewin(char h1,char h2);

void disphand(char h);

void main()

{

char man,computer;

char wantplay;

/*重置随机数序列*/

srand( (unsigned)time( NULL ) );

printf("-----------猜 拳-------------- ");

do

{

/*你出拳*/

while(1)

{

printf("您出什么拳?(1--石头 2--剪子 3--布):");

man=getche();

if(man<'1'man>'3')

printf("您出的不是拳! ");

else

break;

}

/*显示您出的拳*/

printf(" 您出的是");

disphand(man);

printf(" ");

/*电脑出拳*/

computer=random(3) + '1';

/*显示电脑出的拳*/

printf("我出的是");

disphand(computer);

printf(" ");

/*判定胜败*/

switch(judgewin(man,computer))

{

case 0://平

printf("不分胜败 ");

break;

case 1://您赢

printf("唉! 我输了。 ");

break;

case -1://电脑赢

printf("哈哈! 我赢了。 ");

break;

}

printf("还玩吗?(Y/N)");

wantplay=getche();

printf(" ");

}while(wantplay=='y'wantplay=='Y');

}

/*产生0到maxlim之间的随机数*/

int random(int maxlim)

{

float number;

number=((float)rand()/RAND_MAX)*maxlim;

return((int)number);

}

/*判定h1和h2的胜败*/

int judgewin(char h1,char h2)

{

if(h1==h2)

return 0;//peace

else if( (h1=='1'&&h2=='2') /* h1出石头,h2出剪子*/

(h1=='2'&&h2=='3') /*或者h1出剪子,h2出布*/

(h1=='3'&&h2=='1') ) /*或者h1出布 ,h2出石头*/

return 1;//h1 win

else

return -1;//h2 win

}

/*根据h的值显示"石头"、"剪子"、"布"*/

void disphand(char h)

{

switch(h)

{

case '1':

printf("石头");

break;

case '2':

printf("剪子");

break;

case '3':

printf("布");

break;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航