#include
#include
#include
#include

const char* SIGN[] = {"scissors", "paper", "rock"};

void printStat(const int s) {
int i;
for (i = 0; i < s + 3; i++)
printf(" ");
printf(" f\n");
for (i = -3; i < 4; i++)
printf(": %2d ", i);
printf(":\n");
}

int readPlayer(void) {
int value = 0, ret;
do {
printf("Enter your play:scissors<1>,paper<2>,or rock<3> ");
switch (ret = scanf("%d", &value)) {
case EOF:
return -1;
case 0:
{
int c;
for (c = getchar(); c != '\n'; c = getchar());
}
break;
}
if (value >=1 && value <= 3)
break;
printf("Unsupported input! Try again.\n");
} while (1);
return value;
}

int whoWin(const int p, const int c) {
if (p == c)
return 0;
if (p == c - 1 || p == c + 2)
return 1;
return -1;
}

int main(int argc, char *argv[])
{
int stat = 0;
int player, computer;
srand((unsigned int) time(NULL));

while (abs(stat) <= 3) {
printStat(stat);
player = readPlayer();
if (player == -1)
break;
computer = rand() % 3 + 1;
printf("computer play: %s\n", SIGN[computer-1]);
switch (whoWin(player, computer)) {
case 1:
stat++;
printf("------------PLAYER WIN-------------\n");
break;
case -1:
stat--;
printf("------------COMPUTER WIN-----------\n");
break;
default:
printf("------------TIE--------------------\n");
}
}
system("PAUSE");
return 0;
}

創作者介紹
創作者 jethro0119 的頭像
jethro0119

滷蛋與貢丸

jethro0119 發表在 痞客邦 留言(0) 人氣()