Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates the proverbial game of Russian Roulette. The prog

ID: 3583114 • Letter: W

Question

Write a program that simulates the proverbial game of Russian Roulette. The program will prompt the user with how many players will be in the game where a maximum of 50 players can play. The program will next prompt the user to enter a "bad number" between 1 and 6. Once entered, the program will go in turn to each player that is in the game and assign a random number between 1 and 6 and compare it to the "bad number". If there is a match, then that player is out of the game. If there is no match, then that player remains in the game. Once all the players go this process, the process repeats again in the next round for the remaining players starting again with the first player that is still in the game. Only players that are still in the game will receive a random number and have it compared to the "bad number". If a player is out of the game, then the player is ignored and does not receive a random number. This process continues to repeat this way round by round until only one player remains who will then be the winner. You may want to declare an array representing the players with a bool data type where the value of true means a particular player is in the game and the value of false means they are out of the game. Even though a for-loop is commonly used to maintain arrays, you may want to consider using a do-while loop for this program instead. You may want to declare a variable for the number of players, the number of players still in the game, and a counter variable keeping track of when you have gone through all the players in a round so the index variable can then be reset to the first player for the next round.

Explanation / Answer

Below is the code programmed in C++ language:

************************************************************************************

#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAX 50
using namespace std;

int main()
{
int Players[MAX];
int n, badnum, i, count;

cout<<"Enter the number of players= ";
cin>>n;
cout<<" Enter the bad number between 1 to 6-> ";
cin>>badnum;

if ((badnum <1) || (badnum>6))
{
cout<<" wrong Input bad number";
}

else
{
count = n;
for(i=0;i<n;i++)
{
     Players[i]=1;
}
srand ( time(NULL) );
   do
   {
    for(i=0; i<n; i++)
     {
      if(Players[i]!=0)
      {
      Players[i]= rand()%6 + 1;
      }
     }

    for(i=0; i< n; i++)
    {
     if (Players[i]==badnum)
      {
       Players[i]=0;
       count--;
      }
    }
  
} while (count>1);

cout<<" Player's game end numbers: ";
for(i=0; i<n; i++)
     {
      cout<<Players[i]<<" ";
     
     }
for(i=0; i<n; i++)
{
    if (Players[i]!=0)
    {
     cout<<"***** Player "<<i+1<<" wins******";
    }
}
}

return 0;

}

*************************************************************************************************

INPUT:

6
4

OUTPUT:

Enter the number of players=
Enter the bad number between 1 to 6->
Player's game end numbers:
0
0
0
0
6
0
***** Player 5 wins******

Note: Try for other values also to get random results

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote