mercredi 15 novembre 2017

My if/else functions are not working

So using a class, I created a program for a game in which the player would bet whether the sum of 2 rolls would be even or odd. They would then decide how many rolls of the 2 dice would happen. If they were correct in guessing either even or odd, they would get a point. Whoever has more points at the end of the game (player or dealer) wins.

However, when I run the game, the program will seemingly arbitrarily reward all of the point to one player or the other. The problem is almost definitely in my if/else statements on lines 55-64, and 99-108.

Here is my game program:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Pgm8Class.cpp"                                    //New Class

using namespace std;

int Dice()                                                  //Initializes dice rolls
{

    Die die1;                                               //Die roll1
    Die die2;                                               //Die roll2
    int sum;                                                //sum of die rolls
    int even;
    int odd;
    int eventotal=0;
    int idie1;                                              //Defining die rolls as ints
    int idie2;                                              //Defining die rolls as ints

Die die;
    cout << "Die 1: ";
    cout << die1.roll(); 
    cout << endl; 
    cout << "Die 2: ";
    cout << die2.roll();
    cout << endl;

    die1 = idie1;                                           //Setting Die die1 equal to int idie2
    die2 = idie2;                                           //Setting Die die2 equal to int idie2
    sum = idie1 + idie2;                                    //Getting the sum of the 2 dice

}

void gameOdd()                                                  
    {
    int times;
    cout << "How many times should the dice be rolled? ";    
    cin >> times;
    cout << endl;
    srand(time(NULL));                                      //Seeds new number generation

    int idie1;
    int idie2;
    int sum;
    int even = 0;
    int odd = 0;
    int evenOrOdd;

    while(times > 0)
    {
        cout << "Rolls Left: " << times << endl;
        Dice();
        times--;

        if((idie1 + idie2) % 2 == 0)                    //If sum of rolls is Even
            {
            cout << "Dealer Won." << endl << endl;
            ++even;
            }
        else                                            //If sum of rolls is Odd
            {
            cout << "You Won!" << endl << endl;
            ++odd;
            }
    }

    cout << endl;
    cout << "Your Points: " << odd << endl;
    cout << "Dealer Points: " << even << endl << endl;

    if(odd > even)
        cout << "You Won the Game!";
    else
        cout << "The Dealer Won the Game.";
}
void gameEven()
    {
    int times;
    cout << "How many times should the dice be rolled? ";    
    cin >> times;
    cout << endl;
    srand(time(NULL));                                      //Seeds new random number generation


    int idie1;
    int idie2;
    int sum;
    int even = 0;
    int odd = 0;
    int evenOrOdd;


    while(times > 0)
    {
        cout << "Rolls Left: " << times << endl;
        Dice();
        times--;

        if((idie1 + idie2) % 2 == 0)                    //If sum of rolls is even
            {
            cout << "You Won!" << endl <<endl;
            ++even;
            }
        else                                            //If sum of rolls is not even
            {
            cout << "Dealer Won." << endl << endl;
            ++odd;
            }
}

    cout << endl;
    cout << "Your Points: " << even << endl;
    cout << "Dealer Points: " << odd << endl << endl;

    if(even > odd)
        cout << "You Win the Game!";
    else
        cout << "The Dealer Won the Game";
}
int main()
    {
    time_t seconds;
    time(&seconds);
    int evenOrOdd;
    cout << "Will the sum of the rolls be Odd(1) or Even(2)? ";
    cin >> evenOrOdd;
    if(evenOrOdd == 1)
        {gameOdd();}                                        //Intitalizes Game 
    if Odd
    if(evenOrOdd == 2)
        {gameEven();}                                       //Initializes game 
    if Even
}

And here is the Class that I used:

#include<iostream>
#include <cstdlib>

using namespace std;

class Die
{
    public:
        Die();
        Die(int numSide);
        virtual int roll();

    private:
        int side;
};

Die::Die():side(6){}

Die::Die(int numSide):side(numSide){}

int Die::roll()
{
    return rand() % side + 1;
}

Aucun commentaire:

Enregistrer un commentaire