Odds or Even in C++: How to Program

In this tutorial, we will learn how to program the Odds or Even game, in C++, where the user will play against the computer.

Odds or Even in C ++: Commented Game Code

The variables 'player' and 'computer' will store, respectively, the number of wins for you and the machine, respectively. The scoreboard() function is only used to display the scoreboard, using these variables.

The odds_even() function is used to ask the player if he wants to choose even (must enter 0) or odd (enter 1), and returns that value.

In the human_play() function, the user will choose a number to play.
In the computer_play() function, your machine will draw a number from 0 to 10, using random number generation.

Finally, the play() function will take which option the player chose (even or odd), the number the player launched and the number the machine played. It will add these chosen numbers and test if it is even (rest of the sum must be 0) or odd (rest of the sum must be 1).

He notifies the result he gave, who won and increases the variable 'player' or 'computer' correctly, for display on the scoreboard.

In main(), you have the 'cont' variable, to decide whether the game should run again or not.

The 'oddseven' variable receives the user's option, whether he wants EVEN or ODD.

The number that the player chose is in the variable 'num_player' and the machine drawn is in 'num_comp'.

Now just make the move and display the score.
To ask if the player wants to play again and to ensure that at least one move occurs, we use the DO WHILE loop.

Even or Odd game code in C ++


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int player=0,
    computer=0;

void scoreboard();
int odds_even();
int human_play();
int computer_play();
void play(int oddseven, int play, int comp);

int main()
{
    int cont=1,
        oddseven,
        num_player,
        num_comp;

    do{
        oddseven = odds_even();
        num_player = human_play();
        num_comp = computer_play();
        play(oddseven, num_player, num_comp);

        scoreboard();
        cout<<"\nPlay again?"<<endl;
        cout<<"0. Exit"<<endl;
        cout<<"1. Again"<<endl;
        cin >> cont;

    }while(cont);
    return 0;
}

int odds_even()
{
    int num;

    cout<<"\nOdds or Even? Type:"<<endl;
    cout<<"0 for even"<<endl;
    cout<<"1 for odds"<<endl;
    cin >> num;

    return num;
}

int human_play()
{
    int num;
    cout<<"\nType a number from 0 to 10:"<<endl;
    cin >> num;
    return num;
}

int computer_play()
{
    unsigned seed = time(0);
    srand(seed);

    return rand()%11;
}

void play(int oddseven, int play, int comp)
{
    cout<<"\nMOVE: "<<endl;
    cout<<"Human    = "<<play<<endl;
    cout<<"Machine  = "<<comp<<endl;
    cout<<"Sum      = "<<(play+comp)<<endl;
    cout<<"Result   = ";

    if( (play+comp)%2 == 0 )
        cout<<"EVEN\n";
    else
        cout<<"ODD\n";

    if( (play+comp)%2 == oddseven){
        cout<<"\Human won!"<<endl;
        player++;
    }
    else{
        cout<<"\nMachine won!"<<endl;
        computer++;
    }

}


void scoreboard()
{
    cout <<"\nSCOREBOARD:"<<endl;
    cout <<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
    cout <<"Player: "<<player<<"\t Computer: "<<computer<<endl;
    cout <<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
}

Could you improve and make our game more robust? For example, checking if the user entered a number from 0 to 10? Also checking if he types 0 or 1, to continue playing. Any more flaws or weaknesses, did you notice?

No comments:

Post a Comment