Advertisement
Cobble5tone

Educational dice game c++

Aug 3rd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. /*
  2. This is code for a two player dice game. It simulates rolling 5 dice, appending the result to a vector, then repeating that for ten rounds.
  3. At the end, we iterate through and compare rounds to see which player has won the round.
  4. /**/
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     vector<int> playerOne;
  14.     vector<int> playerTwo;
  15.  
  16.     cout << "Welcome to the dice game!\n" << endl;
  17.  
  18.     string playerOneName;
  19.     cout << "Enter player 1's name: ";
  20.     getline(cin, playerOneName);
  21.  
  22.     string playerTwoName;
  23.     cout << "Enter player 2's name: ";
  24.     getline(cin, playerTwoName);
  25.  
  26.     cout << "Welcome " << playerOneName << " and " << playerTwoName << endl;
  27.  
  28.     int roundCounter = 0;
  29.  
  30.     while (roundCounter < 11)
  31.     {
  32.         int playerOneRoll = rand() % 30 + 1;  // Generates a random number between 1-30
  33.         int playerTwoRoll = rand() % 30 + 1;
  34.  
  35.         playerOne.push_back(playerOneRoll);  // Pushes players rolls to vector
  36.         playerTwo.push_back(playerTwoRoll);
  37.  
  38.         ++roundCounter;  // Add one to round counter
  39.  
  40.     }
  41.  
  42.     for (int i = 0; i < playerOne.size(); ++i)  // Iterates through playerOne's vector and rolls for each round
  43.     {
  44.         for (int a = 0; a < playerTwo.size(); ++a)  // Iterates through playerTwo's vector and rolls for each round
  45.         {
  46.             if (i = a) // compares rolls by index within the vectors
  47.             {
  48.                 if (playerOne[i] > playerTwo[a])  // Player 1 victory condition
  49.                 {
  50.                     cout << "Round: " << i << " Player 1 Wins! with " << playerOne[i] << " Over " << playerTwo[a] << endl;
  51.                    
  52.                 }
  53.                 else if (playerTwo[a] > playerOne[i])  // Player 2 victory condition
  54.                 {
  55.                     cout << "Round: " << a << " Player 2 Wins! with " << playerTwo[a] << " Over " << playerOne[i] << endl;
  56.                    
  57.                 }
  58.                 else  // Tie condition
  59.                 {
  60.                     cout << "Round: " << i << " Was a tie!" << endl;
  61.                    
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.    
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement