Just for fun, try this
Code:
#define PAPER 0
#define ROCK 1
#define SCISSORS 2
#define NO_WINNER 3
#define COMPUTER 4
#define HUMAN 5
int Random()
{
srand((unsigned)time(NULL));
return rand()%3;
}
int Find_Winner( int your_choice, int computers_choice )
{
int i;
int winner = NO_WINNER;
int win[] = { PAPER, ROCK, SCISSORS };
int lose[] = { ROCK, SCISSORS, PAPER };
for( i = 0; i < 3; i++)
{
if( ( win[ i ] == your_choice ) && ( lose[ i ] == computers_choice) ) winner = HUMAN;
else
if( ( win[ i ] == computers_choice ) && ( lose[ i ] == your_choice) ) winner = COMPUTER;
}
return winner;
}
int main(int argc, char *argv[])
{
char input[6]; //...a buffer for input...
char *option[] = {
"Paper",
"Rock",
"Scissors"
};
int wins = 0,
losses = 0,
ties = 0;
int your_choice,
computers_choice,
result;
do{
printf("\n\n"
"Choose a weapon!\n\n"
" (1) %s \n"
" (2) %s \n"
" (3) %s \n"
" (4) Quit\n\n",
option[0], option[1], option[2]);
your_choice = atoi( fgets( input, 5, stdin ) );
--your_choice; //...adjust to match options, which start at '0'...
if( your_choice > 2 || your_choice < 0 ) break; //...assume player is trying to quit...
clrscr();
computers_choice = Random();
printf("\n\nYou Chose %s. \n", option[ your_choice ]);
printf("\nComputer Chooses %s. \n\n", option[ computers_choice ]);
result = Find_Winner( your_choice, computers_choice );
switch( result )
{
case COMPUTER: ++losses;
printf("You Lose! %s Beats %s. \n", option[ computers_choice ], option[ your_choice ] );
break;
case HUMAN: ++wins;
printf("You Win! Your %s Beat His %s. \n", option[ your_choice ] , option[ computers_choice ]);
break;
default: ++ties;
printf("It's A Tie! You Both Chose %s. \n", option[ your_choice ] );
}
}while(1);
clrscr();
char *master = "None";
if(wins > losses) master = "You";
else
if(losses > wins) master = "Computer";
printf("\n\n\n\n"
" Winner: %s!\n\n"
" Thanks For Playing!\n\n"
" - Your Score - \n"
" Wins: %3i\n"
" Losses: %3i\n"
" Ties: %3i\n",
master, wins, losses, ties);
getch();
return 0;
}