急需一个C语言 猜拳游戏的源代码!!!!

急需一个C语言 猜拳游戏的源代码!!!!,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

急需一个C语言 猜拳游戏源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!

解析:

enum p_r_s{

paper,rock,scissors,game,help,instructions,quit

}

#include <stdio.h>

main()

{

enum p_r_s player,machine

enum p_r_s selection_by_player(),selection_by_machine()

int win,lose,tie

win=lose=tie=0

instructions_for_the_player()

while((player=selection_by_player())!=quit)

switch(player){

case paper:

case rock:

case scissors:

machine=selection_by_machine()

if(player==machine){

++tie

printf("\n a tie")

}

else if(you_won(player,machine)){

++win

printf("\n you won")

}

else{

++lose

printf("\n i won")

}

break

case game:

game_status(win,lose,tie)

break

case instructions:

instructions_for_the_player()

break

case help:

help_for_the_player()

break

}

game_status(win,lose,tie)

printf("\n\nBYE\n\n")

}

instructions_for_the_player()

{

printf("\n%s\n\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s",

"PAPER,ROCK,SCISSORS",

"In this game",

"p is for paper,",

"r is for rock,",

"s is for scissors.",

"Both the player and the machine will choose one",

"of p,r,or s. If the o choices are the same,",

"then the game is a tie. Otherwise:",

"\"paper covers the rock\" (a win for paper),",

"\"rock breaks the scissors\" (a win for rock),",

"\"scissors cut the paper\" (a win for scissors).")

printf("\n\n%s\n\n%s\n%s\n%s\n%s\n\n%s\n\n%s",

"There are other allowable inputs:",

"g for game status (the number of wins so far),",

"h for help,",

"i for instructions (reprin these instructions),",

"q for quit (to quit the game).",

"This game is played repeatedly until q is entered.",

"Good luck!")

}

enum p_r_s selection_by_player()

{

char c

enum p_r_s player

printf("\n\ninput p,r,or s:")

while((c=getchar())==''||c=='\n'||c=='t')

switch(c){

case 'p':

player=paper

break

case 'r':

player=rock

break

case 's':

player=scissors

break

case 'g':

player=game

break

case 'i':

player=instructions

break

case 'q':

player=quit

break

default:

player=help

}

return(player)

}

enum p_r_s selection_by_machine()

{

static int i

i=++i%3

return((i==0)? paper:((i==1)? rock:scissors))

}

you_won(player,machine)

enum p_r_s player,machine

{

int victory

if(player==paper)

victory=machine==rock

else if(player==rock)

victory=machine==scissors

else/*player==scissors*/

victory=machine==paper

return(victory)

}

game_status(win,lose,tie)

{

printf("\nGAME STATUS")

printf("\n\n%7d%s\n%7d%s\n%7d%s\n%7d%s",

win,"games won by you",

lose,"games won by me",

tie,"game tied",

win+lose+tie,"games played:")

}

help_for_the_player()

{

printf("\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s",

"the following characters can be used for input:",

" p for paper",

" r for rock",

" s for scissors",

" g to find out the game status",

" h to print this list",

" i to reprint the instructions for this game",

" q to quit this game")

}

这是一个简单的猜拳游戏(剪子包子锤),让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。

下面的代码会实现一个猜拳游戏,让你与电脑对决。你出的拳头由你自己决定,电脑则随机出拳,最后判断胜负。

启动程序后,让用户出拳,截图:

用户出拳,显示对决结果:截图:

代码实现:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

{

char gamer// 玩家出拳

int computer// 电脑出拳

int result// 比赛结果

// 为了避免玩一次游戏就退出程序,可以将代码放在循环中

while (1){

printf("这是一个猜拳的小游戏,请输入你要出的拳头:\n")

printf("A:剪刀\nB:石头\nC:布\nD:不玩了\n")

scanf("%c%*c",&gamer)

switch (gamer){

case 65: //A

case 97: //a

gamer=4

break

case 66: //B

case 98: //b

gamer=7

break

case 67: //C

case 99: //c

gamer=10

break

case 68: //D

case 100: //d

return 0

default:

printf("你的选择为 %c 选择错误,退出...\n",gamer)

getchar()

system("cls")// 清屏

return 0

break

}

srand((unsigned)time(NULL))// 随机数种子

computer=rand()%3// 产生随机数并取余,得到电脑出拳

result=(int)gamer+computer// gamer 为 char 类型,数学运算时要强制转换类型

printf("电脑出了")

switch (computer)

{

case 0:printf("剪刀\n")break//4 1

case 1:printf("石头\n")break//7 2

case 2:printf("布\n")break //10 3

}

printf("你出了")

switch (gamer)

{

case 4:printf("剪刀\n")break

case 7:printf("石头\n")break

case 10:printf("布\n")break

}

if (result==6||result==7||result==11) printf("你赢了!")

else if (result==5||result==9||result==10) printf("电脑赢了!")

else printf("平手")

system("pause>nul&&cls")// 暂停并清屏

}

return 0

}

代码分析

1) 首先,我们需要定义3个变量来储存玩家出的拳头(gamer)、电脑出的拳头(computer)和最后的结果(result),然后给出文字提示,让玩家出拳。

接下来接收玩家输入:

scanf("%c%*c",&gamer)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void main()

{

int rand_0(void)

int game(int inp)

int start,yes=1,inp,inp_1=1 char y

while(yes)/*预防用户输入1或2以外的数据*/

{

printf("1:开始游戏\n2:排行榜\n")

scanf("%d",&start)

if((start!=1)&&(start!=2))

{

printf("请输入1或2\n")

}

else

yes=0

}

start:

if(start==1)/*如果用户选择开始游戏……*/

{

printf("你出?\n1:石头\n2:剪刀\n3:布\n")

while(inp_1) /*预防用户输入别的数据*/

{

scanf("%d",&inp)

if((inp!=1)&&(inp!=2)&&(inp!=3))

{

printf("你出?\n1:石头\n2:剪刀\n3:布\n")

}

else

{

inp_1=0

switch(game(inp))

{

case 1:printf("\n\n恭喜你,你赢了!\n\n")break

case 0:printf("\n\n很遗憾,你输了!\n\n")break

case 2:printf("\n\n平局\n\n")break

}

}

}

}

inp_1=1

printf("\n\n是否重新开始游戏?(y/n)")

scanf("%s",&y)

if((y=='y')||y=='Y')

goto start

else

return 0}

int rand_0(void) /*取随机数*/

{

int i,rand_1

srand((unsigned)time(NULL))

for(i=1i<=10i++)

{

rand_1=rand()%4

if(rand_1==0) continue

return(rand_1)

}

}

int game(int inp)

{

int random,win /*win变量,1是赢,2是平,0是输*/

random=rand_0()

switch(inp)

{

case 1:if(random==3) return win=0

else if(random==2) return win=1

else return win=2

case 2:if(random==3) return win=1

else if(random==2) return win=2

else return win=0 case 3:if(random==3) return win=2

else if(random==2) return win=1

else return win=0

}

}就做了那么点点。。。


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

原文地址: http://outofmemory.cn/yw/12101723.html

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

发表评论

登录后才能评论

评论列表(0条)

保存