简体   繁体   中英

Number generator is not functioning properly in my C++ dice game program

I am new to C++ as I am trying to learn from my mistakes. I am writing a program that is supposed to make each player throws a dice, and the one with the largest number gets to start a game by throwing the dice twice. If the dice throw value for both is equal. Then, they throw again until someone gets the largest value. If the player, who started, gets 3-3, 5-5, 6-6, 6-5, the player wins, and if the player gets 1-1, 2-2, 4-4, 1-2, the player loses. If neither, the player who started has his turn finished, and then the second player starts. If he fails to satisfy the same conditions; either losing or winning, then the game is a draw. I have tried to implement that, but it does not work and I do not know why. I am assuming that the random number generator did not work properly. This is my code below:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    srand(time(0));
    cout << " -- Barbooth dice-rolling game with 2 players –" << endl;
    cout << endl;
   
    int numOne = 1;
    int numTwo = 2;
    int numThree = 3;
    int numFour = 4;
    int numFive = 5;
    int numSix = 6;
    int firstThrow = (rand() % 6) + 1; 
    int secondThrow = (rand() % 6) + 1;
    int p1 = (rand() % 6) + 1; //First throw for player one
    int p2 = (rand() % 6) + 1; //First throw for player two
    int p1s = (rand() % 6) + 1; //Second throw for player one
    int p2s = (rand() % 6) + 1; //Second throw for player two
    int thirdThrow = (rand() % 6) + 1; 
    int fourthThrow = (rand() % 6) + 1; 
    int thirdThrows = (rand() % 6) + 1; 
    int fourthThrows = (rand() % 6) + 1; 
    int fivthThrow = (rand() % 6) + 1;
    int sixthThrow = (rand() % 6) + 1; 
    int fivthThrow1 = (rand() % 6) + 1;
    int sixthThrow1 = (rand() % 6) + 1; 
   
    cout << "Player 1, please enter to roll." << endl;
    cout << "Player 1: " << firstThrow << endl;
    cout << "Player 2, please enter to roll." << endl;
    cout << "Player 2: " << secondThrow << endl;
    cout << endl;
   
    if(firstThrow > secondThrow)
    {
        cout << "Player 1, please enter to roll." << endl;
        cout << "Player 1: " << p1 << endl;
        cout << "Player 1, please enter to roll." << endl;
        cout << "Player 1: " << p1s << endl;
    }
    else if(firstThrow < secondThrow)
    {
        cout << "Player 2, please enter to roll." << endl;
        cout << "Player 2: " << p2 << endl;
        cout << "Player 2, please enter to roll." << endl;
        cout << "Player 2: " << p2s << endl;
    }
    else if(firstThrow == secondThrow)
    {
        cout << "Player 1, please enter to roll." << endl;
        cout << "Player 1: " << thirdThrow << endl;
        cout << "Player 2, please enter to roll." << endl;
        cout << "Player 2: " << fourthThrow << endl;
        
        if(thirdThrow > fourthThrow)
        {
            cout << "Player 1, please enter to roll." << endl;
            cout << "Player 1: " << fivthThrow1 << endl;
            cout << "Player 1, please enter to roll." << endl;
            cout << "Player 1: " << sixthThrow1 << endl;
            
        }
        else if(fourthThrow > thirdThrow)
        {
            cout << "Player 2, please enter to roll." << endl;
            cout << "Player 2: " << fivthThrow << endl;
            cout << "Player 2, please enter to roll." << endl;
            cout << "Player 2: " << sixthThrow << endl;
        }
    }
    else
    {
        cout << "Tie!" << endl;
    }
    
    // All game conditions
    if(p1 == numThree && p1s == numThree)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n";
        
      }
     else if(p1 == numFive && p1s == numFive)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n"; 
        
      }
     else if(p1 == numSix && p1s == numSix)
      {
         cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n";
        
      }
     else if(p1 == numSix && p1s == numFive)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n";  
      }
     else if(p1 == numOne && p1s == numOne)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p1 == numTwo && p1s == numTwo)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p1 == numFour && p1s == numFour)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p1 == numOne && p1s == numTwo)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p2 == numThree && p2s == numThree)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p2 == numFive && p2s == numFive)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p2 == numSix && p2s == numSix)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 2 wins\n";
      }
     else if(p2 == numSix && p2s == numFive)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 2 wins\n";
      }
      else if(p2 == numOne && p2s == numOne)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 1 wins\n";
      }
     else if(p2 == numTwo && p2s == numTwo)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 1 wins\n";
      }
     else if(p2 == numFour && p2s == numFour)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 1 wins\n";
      }
     else if(p2 == numOne && p2s == numTwo)
      {
        cout<< "Player 2: " << p2 << "-" << p2s << endl;
        cout << "Player 1 wins\n";
      }
      
      ////////////////////////////////////
      else if(fivthThrow1 == numThree && sixthThrow1 == numThree)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout<< "Player 1 wins\n";
      }
     else if(fivthThrow1 == numFive && sixthThrow1 == numFive)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout<< "Player 1 wins\n";  
      }
     else if(fivthThrow1 == numSix && sixthThrow1 == numSix)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout<< "Player 1 wins\n";  
      }
     else if(fivthThrow1 == numSix && sixthThrow1 == numFive)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout<< "Player 1 wins\n";  
      }
     else if(fivthThrow1 == numOne && sixthThrow1 == numOne)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow1 == numTwo && sixthThrow1 == numTwo)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow1 == numFour && sixthThrow1 == numFour)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow1 == numOne && sixthThrow1 == numTwo)
      {
        cout<< "Player 1: " << fivthThrow1 << "-" << sixthThrow1 << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow == numThree && sixthThrow == numThree)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow == numFive && sixthThrow == numFive)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow == numSix && sixthThrow == numSix)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 2 wins\n";
      }
     else if(fivthThrow == numSix && sixthThrow == numFive)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 2 wins\n";
      }
      else if(fivthThrow == numOne && sixthThrow == numOne)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 1 wins\n";
      }
     else if(fivthThrow == numTwo && sixthThrow == numTwo)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 1 wins\n";
      }
     else if(fivthThrow == numFour && sixthThrow == numFour)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 1 wins\n";
      }
     else if(fivthThrow == numOne && sixthThrow == numTwo)
      {
        cout<< "Player 2: " << fivthThrow << "-" << sixthThrow << endl;
        cout << "Player 1 wins\n";
      }
      ////////////////////////////////
      
      else
      {
          cout<< "Tie! \n";
      }
}

Can someone kindly help me locate the issue? Thank you.

I am assuming that the random number generator did not work properly.

I can assure you that random generator is not your problem.

  1. Write a function that chooses who goes first. Note - your code gives the players two chances, and even if they throw the same number - your code will keep going into a game.

  2. Note that algorithm for checking winning and loosing combinations is the same for each player. Write it once, as a function, and call for the first player and then, if needed - for the second.

  3. Don't pre-throw dice; get the value when needed. Could also be done in a separate, one-line function.

  4. I am all for named constants, but your numOne .. numSix is an overkill.

  5. Wanted to comment on your variable names, but most of them are not needed anyway...

  6. Tip: if you seed the random generator with a known value (like 0, 1, etc.), you will get the same pseudo-random sequence. Great for debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM