PCinvent.info

PCinvent Studio Template

Aug 16 2008
C++ Homework: The Game of Rock, Paper, and Scissors Print E-mail
Saturday, 16 August 2008


Read More Articles in: Technology>>>Programming

This Article is written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it


Time to post my old C++ Hoemwork. This one is the Game of Rock, Paper, and Scissors......

 

//File Name:      Game.cpp
//Description:    The Game of Rock, Paper, and Scissors
//Written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it

#include <iostream>
#include <iomanip>
using namespace std;

enum objectType {Rock, Paper, Scissors}; // Rock is 0, Paper is 1, Scissors is 2

//function prototypes

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);

//main function

int main(){
    // declare variables
    int gameCount;
    int winCount1;
    int winCount2;
    int gamewinner;
    char response;
    char selection1;
    char selection2;
    objectType play1;
    objectType play2;

    // Initialization
    gameCount = 0;
    winCount1 = 0;
    winCount2 = 0;

    displayRules ();

    cout << "Enter Y/y to play the game: ";
    cin >> response;
    cout << endl;

    // Processing loop
    while ( response == 'Y' || response == 'y' ){
        cout<<"Enter R/r if u choose Rock, P/p if u choose Paper, S/s if u choose Scissors."<<endl;

        cout << "Player 1 enter your choice: ";
        cin >> selection1;
        cout << endl;

        cout << "Player 2 enter your choice: ";
        cin >> selection2;
        cout << endl;

        if ( validSelection(selection1) && validSelection(selection2) ){
            play1 = retrievePlay(selection1);
            play2 = retrievePlay(selection2);
            gameCount++;
            gameResult( play1, play2, gamewinner );
            if ( gamewinner == 1 )
                winCount1++;
            else if ( gamewinner == 2 )
                winCount2++;
        }//if ( validSelection

        cout << "Enter Y/y to play the game: ";
        cin >> response;
        cout << endl;
    } // while (
    displayResults(gameCount,winCount1,winCount2);
}// main (

//display game rule

void displayRules(){
    cout << "_*_*_*_*_*_*_*  The Game of Rock, Paper, and Scissors *_*_*_*_*_*_*_"<<endl;
    cout << "_*_*_*_*_*_*_*_*_*_*_*_*_*_* Instruction *_*_*_*_*_*_*_*_*_*_*_*_*_*_"<<endl;
    cout << endl;
    cout << "This game has two players, each of whom chooses one of the three objects: "<<endl<<"Rock, Paper, or Scissors."
        <<endl<<endl;
    cout << "If player 1 chooses rock and player 2 chooses paper, player 2 wins the game because paper covers the rock."
        <<endl<<endl;
    cout << "The game is played according to the following rules: "
        <<endl;
    cout <<endl<<setfill('.')<<left<<setw(10)<< "Rule 1: "<<right<<"If both players choose the same object, this play is a tie."<<endl
        <<endl<<setfill('.')<<left<<setw(10)<< "Rule 2: "<<right<<"If one player chooses rock and the other chooses scissors, the player choosing the rock wins this play because the rock breaks the scissors."<<endl
        <<endl<<setfill('.')<<left<<setw(10)<< "Rule 3: "<<right<<"If one player chooses rock and the other chooses paper, the player choosing the paper wins this play because the paper covers the rock."<<endl
        <<endl<<setfill('.')<<left<<setw(10)<< "Rule 4: "<<right<<"If one player chooses scissors and the other chooses paper, the player choosing the scissors wins this play because the scissors cut the paper."<<endl;
    cout<<endl;
}

bool validSelection(char selection){
    switch (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;
            break;
    }
    return object;
}

void convertEnum(objectType object){
    switch(object){
        case 0:
            cout << "Rock"<<endl;
            break;
        case 1:
            cout << "Paper"<<endl;
            break;
        case 2:
            cout << "Scissors"<<endl;
            break;
    }
}

objectType winningObject(objectType play1, objectType play2){
    if ( (play1==0 && play2==2)||(play1==2 && play2==0)||(play1==0 && play2==0) )
        return Rock;
    else if ( (play1==0 && play2==1)||(play1==1 && play2==0)||(play1==1 && play2==1) )
        return Paper;
    //if ( (play1==1 && play2==2)||(play1==2 && play2==1)||(play1==2 && play2==2) )
    else
        return Scissors;
}

void gameResult(objectType play1, objectType play2, int& winner){
    objectType winnerobject;
    winnerobject=winningObject(play1,play2);
    if (play1== play2){
        winner = 0;
        cout << "Tie Game!"<<endl;
        cout << "Two players selected: ";
        convertEnum(winnerobject);
    }
    else if(play1==winnerobject){
        winner=1;
        cout << "Player1 is the winner!!!" <<endl;
        cout << "Player1 select: ";
        convertEnum(play1);
        cout << "Player2 select: ";
        convertEnum(play2);
    }
    else{
        winner=2;
        cout << "Player2 is the winner!!!"<<endl;
        cout << "Player1 select: ";
        convertEnum(play1);
        cout << "Player2 select: ";
        convertEnum(play2);
    }
}

void displayResults(int gCount, int wCount1, int wCount2){
    cout << setfill('.')<<left<<setw(35)<< "Total number of games played: "<<right<<gCount<<endl
         << setfill('.')<<left<<setw(35)<< "Player1 wins: "<<right<<wCount1<<endl
         << setfill('.')<<left<<setw(35)<< "Player2 wins: "<<right<<wCount2<<endl;
}

Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Tags:  Technology Programming C++ Homework The Game of Rock Paper and Scissors
 
< Prev   Next >
Site by PCinvent.com