急需一个C语言 猜拳游戏的源代码!!!!
急需一个C语言猜拳游戏的源代码!!!!急需一个C语言猜拳游戏的源代码!!!!急需一个C语言猜拳游戏的源代码!!!!急需一个C语言猜拳游戏的源代码!!!!急需一个C语言猜拳...
急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!急需一个C语言 猜拳游戏的源代码!!!!
展开
3个回答
展开全部
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 two 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");
}
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 two 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");
}
展开全部
偶就会VB的 要吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给我多加点分OK?呵呵
#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 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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询