L-26 MCS 260 Fri 19 Oct 2001
In this lecture we continue to build up the game of
paper, rock, scissors with enumeration types.
/* L-26 MCS 260 Fri 18 Oct 2001 : play game of paper, rock, scissors
In the program below we play the game of paper, rock, scissors against
the computer. Each time the user makes a hand, the computer generates
a random choice for the hand. */
enum hand { paper, rock, scissors };
typedef enum hand hand;
hand make_hand ( void );
/* asks the user to supply rock, scissors, or paper */
void show_hand ( hand h );
/* prints the value of the hand */
void print_outcome ( hand player, hand adversary );
/* prints the outcome for a game with hands of player and adversary */
void print_win_or_lose ( int i );
/* reports a win if i != 0, a loss if i == 0 */
hand random_hand ( void );
/* returns at random paper, rock, or scissors */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
hand player, adversary;
int ans;
srand(time(NULL));
do
{
player = make_hand();
adversary = random_hand();
printf("\n player's "); show_hand(player);
printf("\n and adversary's "); show_hand(adversary);
printf("\n yields "); print_outcome(player,adversary);
printf("\n\n");
printf("Exit the game ? (type 1 for yes) ");
scanf("%d", &ans);
} while (ans != 1);
return 0;
}
hand make_hand ( void )
{
int ans;
do
{
printf("Type 0 for paper, 1 for rock, 2 for scissors : ");
scanf("%d", &ans);
switch(ans)
{
case 0 : return paper;
case 1 : return rock;
case 2 : return scissors;
default : printf("Invalid choice. Please try again.\n");
}
} while (1);
}
void show_hand ( hand h )
{
printf("hand is ");
switch(h)
{
case paper : printf("paper"); break;
case rock : printf("rock"); break;
case scissors : printf("scissors"); break;
default : printf("not playing");
}
}
void print_outcome ( hand player, hand adversary )
{
if (player == adversary)
printf("a tie");
else
switch(player)
{
case paper: print_win_or_lose(adversary == rock);
break;
case rock: print_win_or_lose(adversary == scissors);
break;
case scissors: print_win_or_lose(adversary == paper);
break;
default: printf("abnormal situation.\n");
}
}
void print_win_or_lose ( int i )
{
if (i)
printf("a win");
else
printf("a loss");
}
hand random_hand ( void )
{
int r = rand() % 3;
return (hand) r;
}
We extend the program above to keep track of the score :
/* L-26 MCS 260 Fri 18 Oct 2001 : play game of paper, rock, scissors
In the program below we play the game of paper, rock, scissors against
the computer. Each time the user makes a hand, the computer generates
a random choice for the hand. At the end of the game, the number of
wins, losses, and ties are reported. */
enum hand { paper, rock, scissors };
typedef enum hand hand;
enum outcome { loss, win, tie };
typedef enum outcome outcome;
hand make_hand ( void );
/* asks the user to supply rock, scissors, or paper */
void show_hand ( hand h );
/* prints the value of the hand */
outcome compare ( hand player, hand adversary );
/* returns result of comparison of player with adversary */
void print_outcome ( outcome result );
/* reports a win or a loss */
hand random_hand ( void );
/* returns at random paper, rock, or scissors */
void print_score ( int losses, int wins, int ties );
/* prints the number of losses, wins, and ties */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
hand player, adversary;
int ans;
outcome result;
int nblosses = 0;
int nbwins = 0;
int nbties = 0;
srand(time(NULL));
do
{
player = make_hand();
adversary = random_hand();
result = compare(player,adversary);
printf("\n player's "); show_hand(player);
printf("\n and adversary's "); show_hand(adversary);
printf("\n yields "); print_outcome(result);
printf("\n\n");
switch(result)
{
case loss : nblosses++; break;
case win : nbwins++; break;
case tie : nbties++; break;
default : printf("an error");
}
printf("Exit the game ? (type 1 for yes) ");
scanf("%d", &ans);
} while (ans != 1);
print_score(nblosses,nbwins,nbties);
return 0;
}
hand make_hand ( void )
{
int ans;
do
{
printf("Type 0 for paper, 1 for rock, 2 for scissors : ");
scanf("%d", &ans);
switch(ans)
{
case 0 : return paper;
case 1 : return rock;
case 2 : return scissors;
default : printf("Invalid choice. Please try again.\n");
}
} while (1);
}
void show_hand ( hand h )
{
printf("hand is ");
switch(h)
{
case paper : printf("paper"); break;
case rock : printf("rock"); break;
case scissors : printf("scissors"); break;
default : printf("not playing");
}
}
outcome compare ( hand player, hand adversary )
{
outcome result;
if (player == adversary)
result = tie;
else
switch(player)
{
case paper: result = (adversary == rock) ? win : loss;
break;
case rock: result = (adversary == scissors) ? win : loss;
break;
case scissors: result = (adversary == paper) ? win : loss;
break;
default: printf("abnormal situation.\n");
}
return result;
}
void print_outcome ( outcome result )
{
switch(result)
{
case loss : printf("a loss"); break;
case win : printf("a win"); break;
case tie : printf("a tie"); break;
default : printf("should never occur");
}
}
hand random_hand ( void )
{
int r = rand() % 3;
return (hand) r;
}
void print_score ( int losses, int wins, int ties )
{
int total = losses + wins + ties;
printf("\nPlayed %d times :", total);
printf(" %d lost, %d won, and %d tied.\n\n", losses, wins, ties);
}