L-25 MCS 260 Wed 17 Oct 2001
In this lecture we build up the game of paper, rock, scissors
with enumeration types.
/* L-25 MCS 260 Wed 17 Oct 2001 : hand = paper, rock, or scissors
In the program below we show how to define an enumeration type to
play the game of paper, rock, scissors.
In the game, we use our hand to represent three objects:
paper, rock, or scissors. Therefore we define an enumeration
type "hand" which can take three values. In addition, we define
user friendly input/output routines. */
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 */
#include<stdio.h>
int main(void)
{
hand player;
player = make_hand();
show_hand(player); printf("\n");
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");
}
}
After defining the enumeration type with its input/output operations,
we can implement the rules of the game, extending the program above.
/* L-25 MCS 260 Wed 17 Oct 2001 : rules in paper, rock, scissors game
In the program below we implement the rules in the game of paper, rock,
and scissors :
paper covers the rock
rock breaks the scissors
scissors cut the paper
The user of this program types in the hands of one player and
the adversary, whereafter the rule is executed. */
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 */
#include<stdio.h>
int main(void)
{
hand player, adversary;
printf("Making the hand of the player...\n");
player = make_hand();
printf("Making the hand of the adversary...\n");
adversary = make_hand();
printf("\nplayer's "); show_hand(player);
printf("\nand adversary's "); show_hand(adversary);
printf("\nyields "); print_outcome(player,adversary);
printf("\n\n");
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");
}