Hi, I\'m trying to write a C program that focuses on using enum. Thing is, I\'m
ID: 671839 • Letter: H
Question
Hi, I'm trying to write a C program that focuses on using enum. Thing is, I'm not sure if I'm doing it right. The program runs correctly, I just don't know what to put after i type "enum choices{ r, p, s }; //rock=0, paper = 1, scissors = 2
enum choices choice;" because the focus of the assignment is to do enum.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void start_play();
int main(int argc, char* argv[])
{
start_play();
return 0;
}
void start_play()
{
enum choices{ r, p, s }; //rock=0, paper = 1, scissors = 2
enum choices choice;
{
char p1, p2, p3;
printf("Player-1 it is your turn! ");
printf("Please enter your choice (p)aper. (r)ock, or (s)cissors: ");
p1 = getchar();
scanf("%c", &p3);//to avoid the buffer
printf("Player-2 it is your turn! ");
printf("Please enter your choice (p)aper. (r)ock, or (s)cissors: ");
scanf("%c", &p2);
if (p1 == p2){
printf("Draw, nobody wins ");
}
else{
if (p1 == 'p'){
if (p2 == 'r'){
printf("Player1 wins! paper covers rock! ");
}
else if (p2 == 's'){
printf("Player2 wins! Scissors cut paper! ");
}
}
else if (p1 == 'r'){
if (p2 == 's'){
printf("Player1 wins! Rock breaks scissors! ");
}
else if (p2 == 'p'){
printf("Player2 wins! Paper covers rock! ");
}
}
else if (p1 == 's'){
if (p2 == 'r'){
printf("Player2 wins! Rock breaks scissors! ");
}
else if (p2 == 'p'){
printf("Player1 wins! Scissors cut paper! ");
}
}
}
}
}
Explanation / Answer
#include <stdio.h>
using namespace std;
enum objectType {ROCK, PAPER, SCISSORS};
void displayRules();
objectType retrievePlay(char selection);
bool validSelection(char selection);
void convertEnum(objectType object);
objectType winningObject(objectType play1, objectType play2);
void gameresult(objectType play1, objectType play2, int& winner);
void displayResults(int gCount, int wCount1, int wCount2);
int main()
{
int gameCount;
int winCount1;
int winCount2;
int gamewinner;
char response;
char selection1;
char selection2;
objectType play1;
objectType play2;
gameCount = 0;
winCount1 = 0;
winCount2 = 0;
displayRules(0;
cout <> response;
cout << endl;
while(respone == 'y' || response == 'Y')
{
cout <> selection1;
cout << endl;
cout <> selection2;
endl;
if(validSelection(selection1) && validSelection(selection2))
{
play1 = retrievePlay(selection1);
play2 = retrievePlay(selection2);
gameCount++;
gameResults(play1, play2, gameWinner);
if(gameWinner == 1)
winCount1++;
else if(gameWinner == 2)
winCount2++;
}
displayResults(gameCount, winCount1, winCount2);
return 0;
}
}
void displayRules()
{
cout << "Welcome to the game of Rock, Paper and Scissors," << endl;
cout << "This is a game for two players. For each game, each " << endl;
cout << "player selects one of the objects Rock, Paper or Scissors" << endl;
cout << "The rules for winning the game are: " << endl;
cout << "1. If both players selects the same object it is a tie" << endl;
cout << "2. Rock breaks Scissors" << endl;
cout << "3. Paper covers Rock" << endl;
cout << "4. Scissors cuts Paper" << endl;
cout << "Enter R/r for Rock, P/p for Paper and S/s for Scissors" << endl;
}
bool validSelection(char selection)
{
case 'R':
case 'r':
case 'P':
case 'p':
case 's':
case 'S':
return true;
default:
return false;
}
objectType retrievePlay(char selection)
{
objectType object;
switch(selection)
{
case 'r':
case 'R':
object = ROCK;
break;
case 'p':
case 'P':
object = PAPER;
break;
case 's':
case 'S':
object = SCISSORS;
}
return object;
}
void gameResults(objectType play1, objectType play2, int& winner)
{
if(play1 == play2)
{
winner = 0;
cout << "Both Player selected";
convertEnum(play1);
cout << "This game is a tie" << endl;
}
else
{
winnerObject = winningObject(play1, play2);
cout << "Player 1 selected ";
convertEnum(play1);
cout << " and player 2 selected ";
convertEnum(play2);
cout << ". ";
if(play1 == winnerObject)
winner = 1;
else if(play2 == winnerObject)
winner = 2;
cout << "Player " << winner << " wins the game." << endl;
}
}
void convertEnum(objectType object)
{
switch(object)
{
case ROCK:
cout << "Rock";
break;
case PAPER:
cout << "Paper";
break;
case SCISSORS:
cout << "Scissors";
}
}
objectType winningObject(objectType play1, objectType play2)
{
if((play1 == ROCK && play2 == SCISSORS) || (play2 == ROCK && play1 == SCISSORS)
return ROCK;
else if((play1 == ROCK && play2 == PAPER) || (play2 == ROCK && play1 == PAPER)
return PAPER
else
return SCISSORS;
}
void displayResults(int gCount, int wCount1, int wCount2)
{
cout << "The total number of play: " << gCount << endl;
cout << "The number of plays won by player 1: " << wCount1 << endl;
cout << "The number of plays won by player 2: " << wCount2 << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.