给个方法

王朝知道·作者佚名  2011-05-10
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

我学习了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 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");

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航