2007年12月18日 星期二

C 程式設計作業四,迴圈與函數應用:解題


/* C Programming, Project 4 */
#include <stdio.h>
#include <stdlib.h>

int whoWin(int, int);
int main(void)
{
int position = 0;
int computerPlay, myPlay;
int result;

srand(time(NULL));
while (position>4 && position<-4)
{
printf("%*c\n", (position+4)*5-1, 'f');
printf("| -3 | -2 | -1 | 0 | 1 | 2 | 3 |\n\n");
do
{
printf("Enter your play: scissors(1), paper(2), or rock(3) ");
scanf("%d", &myPlay);
} while (myPlay!=1 && myPlay!=2 && myPlay!=3 );
computerPlay = rand()%3 + 1;
if (computerPlay==1)
printf("Computer play: scissors\n");
else if (computerPlay==2)
printf("Computer play: paper\n");
else if (computerPlay==3)
printf("Computer play: rock\n");

result = whoWin(computerPlay, myPlay);
position += result;
}

system("pause");
return 0;
}

int whoWin(int comp, int me)
{
int winner;
if (comp==me)
{
printf("--------------- MATCH ---------------\n");
winner = 0;
}
else if (comp-me==1 || comp-me==-2)
{
printf("------------ PLAYER WIN -------------\n");
winner=1;
}
else
{
printf("----------- COMPUTER WIN ------------\n");
winner=-1;
}
return winner;
}

作業四題目
回到首頁

沒有留言: