#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int gen_num()
{
srand(time(NULL));
int no=rand()%3;
return no;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
int computer_guess;
int player_guess;
int win_cnt;
int guess_cnt=0;
while(guess_cnt<3)
{
printf("請輸入 0:剪刀 1:石頭 2:布 9:離開");
scanf("%d",&player_guess);
if(player_guess==9)
{
printf("Byte bye...");
break;//跳出回圈
}
computer_guess=gen_num();
printf("電腦出拳:%d\n",computer_guess);
if(computer_guess==player_guess)
{
printf("平手\n");
continue;
}
switch(player_guess)
{
case 0:
if(computer_guess==2)
{
printf("玩家贏了\n");
win_cnt++;
}
else
{
printf("玩家輸了\n");
}
break;//遇到 break 後離開 switch 區塊
case 2:
if(computer_guess==1)
{
printf("玩家贏了\n");
win_cnt++;
}
else
{
printf("玩家輸了\n");
}
break;
default:
printf("別在亂出拳了\n");
}
guess_cnt++;
}//end of while
if(win_cnt>=2)
{
printf("player win\n");
}
else
{
printf("computer win\n");
}
return 0;
}
沒有留言:
張貼留言