求大神帮忙编个剪刀石头布的C++程序

求大神帮忙编个剪刀石头布的C++程序,第1张

#include <stdio.h>

#include <iostream>

#include <time.h>

int getrand()

{

//srand((unsigned)time(NULL))

return rand()%3

}

int win(int me,int cpu)

{

if(me==2&&档岩漏cpu==0)

return -1

else if (me==0&&cpu==2)

return 1

else 枣郑if(me==cpu)

return 0

else if(me>cpu)

return 1

else

return -1

}

int exchange(char input)

{

int i=0

switch(input) {

case 'S':

case 's':

i = 2

break

case 行烂'p':

case 'P':

i = 1

break

default:

i = 0

}

return i

}

int main(void)

{

char c='a'

char msg[3][10] = {"rock","paper","scissors"}

int me,cpu

while(1){

printf("Enter R(ock), P(aper), or S(cissors)> ")

flushall()

scanf("%c",&c)

if(c=='q'||c=='Q') break

me = exchange(c)

cpu = getrand()

printf("You played %s.\n",msg[me])

printf("The computer played %s.\n",msg[cpu])

if(win(me,cpu)==1)

printf("You win!\n")

else if(win(me,cpu)==-1)

printf("The computer win!\n")

else

printf("Go go go!\n")

}

return 0

}

source file一点也不类似XP下dos什么的。

#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("-----------猜 拳--------------\n")

do

{

while(1)

{

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

man=getche()

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

printf("您出的不是拳!\n")

else

break

}

printf("\n您出的是")

disphand(man)

printf("\n")

computer=random(3) + '1'

printf("我出的是")

disphand(computer)

printf("\n")

switch(judgewin(man,computer))

{

case 0://平

printf("不分胜负\n")

break

case 1://您赢

printf("唉! 我输了。\n")

break

case -1://电脑赢

printf("哈哈! 我赢了。\n")

break

}

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

wantplay=getche()

printf("\n\n")

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

}

int random(int maxlim)

{

float number

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

return((int)number)

}

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

}

void disphand(char h)

{

switch(h)

{

case '1':

printf("石头")

break

case '灶正败2':

printf("剪子")

break

case '3':

printf("布")

break

}

}

版权隐颤所有!不是我的,先声明

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

{

char m //表示选择的是哪个人铅腊

int a,b //分别表示人和电脑

printf("(J)iandao, (S)hitou, hu(B)u :\n")

scanf("%c",&m)

if(m!='J' &&m!='S'胡蚂 &&m!='B')

printf("Input error!\n")

else //分别用0,1,2代表石头,剪刀,布

{

srand(time(NULL))

a = rand()%3

switch(a)

{

case 0:

printf("人出的是石头\n")

break

case 1:

printf("人出的是剪刀\n")

break

case 2:

printf("人出的是布\n")

break

default:

break

}

b = rand()%3

switch(b)

{

case 0:

printf("电脑出的是石裤激埋头\n")

break

case 1:

printf("电脑出的是剪刀\n")

break

case 2:

printf("电脑出的是布\n")

break

default:

break

}

if(a >b)

printf("人赢了!\n")

else if(a == b)

printf("平局!\n")

else

printf("电脑赢了!\n")

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12439281.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存