Home > front end >  Adding a Play Again Option in a C Dice Game Program that Enables Players to Play until There is a
Adding a Play Again Option in a C Dice Game Program that Enables Players to Play until There is a

Time:01-31

I am new to C as I am trying to learn from my mistakes. I am writing a dice game program that has several conditions and enables the two players to play until there is a winner. I have tried reading about ways to implement the "play until there is a winner rather than stop when it is a draw" option. However, I did not come up with something that worked. I have tried making my else block looking like this:

else
          {
              cout<< "Player 1: " << p1 << "-" << p1s << endl;
              cout<< "Player 2: " << p2 << "-" << p2s << endl;
              cout<< "Tie! \n";
              return main; // I am wanting the game to restart automatically when they draw. So that is what I did.

          }

The reason I did that is because I want it to declare the players drew and wanted it to restart. How should I start writing it? Thanks, and here 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 p1 = (rand() % 6)   1; //First throw for player one
    cout << "Player 1, please enter to roll." << endl;
    cout << "Player 1: " << p1 << endl;
    
    
    int p2 = (rand() % 6)   1; //First throw for player two
    cout << "Player 2, please enter to roll." << endl;
    cout << "Player 2: " << p2 << endl;
    
    int p1s = (rand() % 6)   1; //Second throw for player one
    int p2s = (rand() % 6)   1; //Second throw for player 4
    cout << endl;
    
    if(p1 > p2)
    {
        cout << "Player 1, please enter to roll." << endl;
        cout << "Player 1: " << p1s << endl;
    }
    else
    {
        cout << "Player 2, please enter to roll." << endl;
        cout << "Player 2: " << p2s << endl;
    }
    
    cout << endl; 
    

    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 2 wins\n";
      }
     else if(p1 == numTwo && p1s == numTwo)
      {
        cout << "Player 2 wins\n";
      }
     else if(p1 == numFour && p1s == numFour)
      {
        cout << "Player 2 wins\n";
      }
     else if(p1 == numOne && p1s == numTwo)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numThree && p2s == numThree)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numFive && p2s == numFive)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numSix && p2s == numSix)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numSix && p2s == numFive)
      {
        cout << "Player 2 wins\n";
      }
      else if(p2 == numOne && p2s == numOne)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numTwo && p2s == numTwo)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numFour && p2s == numFour)
      {
        cout << "Player 2 wins\n";
      }
     else if(p2 == numOne && p2s == numTwo)
      {
        cout << "Player 2 wins\n";
      }
      
      else
      {
          cout<< "Player 1: " << p1 << "-" << p1s << endl;
          cout<< "Player 2: " << p2 << "-" << p2s << endl;
          cout<< "Tie! \n";
      }
}

CodePudding user response:

You can get desired effect using simple while loop with boolean variable. This loop will continue until you don't change gamefinished variable in one of your winning states.

bool gamefinished = false
while(!gamefinished) {

    int p1 = (rand() % 6)   1; //First throw for player one
    cout << "Player 1, please enter to roll." << endl;
    cout << "Player 1: " << p1 << endl;
    
    
    int p2 = (rand() % 6)   1; //First throw for player two
    cout << "Player 2, please enter to roll." << endl;
    cout << "Player 2: " << p2 << endl;
    
    int p1s = (rand() % 6)   1; //Second throw for player one
    int p2s = (rand() % 6)   1; //Second throw for player 4
    cout << endl;

    if(p1 == numThree && p1s == numThree)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n";
        gamefinished = true;
      }
     else if(p1 == numFive && p1s == numFive)
      {
        cout<< "Player 1: " << p1 << "-" << p1s << endl;
        cout<< "Player 1 wins\n";  
        gamefinished = true;
      }
/// rest of your non draw cases
.
.
.
      else
      {
          cout<< "Player 1: " << p1 << "-" << p1s << endl;
          cout<< "Player 2: " << p2 << "-" << p2s << endl;
          cout<< "Tie! \n";
          //don't change your bool variable here so loop will go for
          //another iteration
      }
}

Also. I'm not sure why you defined integers as variables

if(p1 == 1 && p1s == 5)

is as valid as your code but shorter

CodePudding user response:

I made a little dice game for you and it was fun. It will go on until someone wins, goes around if it is a tie. Also number of players can be increased with just a variable. Please don't be scared and try to understand which modern c features I used and why. They will be helpful if you want to proceed in c .

#include <iostream>
#include <time.h>
#include <vector>
#include <thread>
#include <chrono>

int main()
{
    std::cout << ":::\tdice game\t:::\n\npress enter to roll a dice:\n";
    int n_of_players = 2;
    int whose_turn = 0;
    
    std::vector <int> scores;
    
    for(int i = 0; i < n_of_players; i  ) {
        scores.push_back(0);
    }
    
    while(true) {
        std::cin.ignore();
        
        for(int i = 0; i < 3; i  ) {
            std::cout << "player " << whose_turn   1 << " rolls";
            switch(i) {case 0: std::cout << ".\r"; break; case 1: std::cout << "..\r"; break; case 2: std::cout << "...\r"; break;}
            std::cout << std::flush;
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
        std::cout << std::endl;
        
        srand(time(nullptr));
        
        scores[whose_turn] = rand() % 6   1;
        
        /**  1 is just for naming. nobody likes to see player 0 */ 
        std::cout << "player " << whose_turn   1 << " rolled " << scores[whose_turn] << std::endl;
        
        if(whose_turn == scores.size() - 1) {
            std::vector <int> winners;
            int max_score = 0;
            for(int i = 0; i < scores.size(); i  ){
                if(scores[i] > max_score) {
                    max_score = scores[i];
                    winners.clear();
                    winners.push_back(i);
                }
                else if(scores[i] == max_score) {
                    winners.push_back(i);
                }
            }
            
            if(winners.size() > 1) {
                std::cout << "it's a tie, let's try again" << std::endl;
                continue;
            }
            else {
                std::cout << "player " << winners[0]   1 << " won!" << std::endl;
                break;
            }
            whose_turn = 0;
        }
        else whose_turn  ;
    }
    
    return 0;
}
  •  Tags:  
  • Related