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

//// I need some help on this exact program!! //// Write a program for playing a

ID: 3762098 • Letter: #

Question

//// I need some help on this exact program!! ////

Write a program for playing a variation of the Chinese game ”Tsyan-shizi” called the game of NIM. Our version of the game starts with up to 20 rods and up to 10 stones in each rod. Two players take turns removing stones from the rods. On a player’s turn, she chooses a rod and removes one or more stones from that rod. She can remove all stones from the rod. The player who takes the last stone from the rods (so that all rods are empty) wins. You will implement the following algorithm in the main function. Each line in the algorithm will correspond to a function call. Important! Write your code incrementally. This means you should write each function one at a time, check that it compiles and runs properly, test it, and then implement the next function.
i. Prompt and read the number of rods
ii. Prompt and read the number of stones in each rod;
iii. Draw the rods with percentages;
iv. Display statistics;
v. while (some rod is NOT empty) do
vi. Prompt and read the next player’s move (Prompt and read the rod to modify
and the number of stones to remove from the rod);
vii. Remove the specified number of stones from the specified rod;
vii. if (all rods are empty) then
ix. Print a message congratulating the winning player.
x. else
xi. Redraw the rods with percentages;
xii. Display statistics;
xiii. Change to the other player;
xiv. end
xv. end

///// Here is a template ////

#include <iostream>

#include <manip>

using namespace std;

// FUNCTION PROTOTYPES HERE

int main () {

// Define variables

// algorithm:

// prompt and read number of rods

// prompt and read the number of objects in each rod// draw the rods with percentages

// display statistics

// WHILE some rod is NOT empty DO

// prompt and read the nect player's move

// remove the specified number of objects from the specified rod

// IF all the heaps are empty, THEN

      // Print a message congratulating the winning player

// ELSE

      // Redraw the rods with percentages

      // Display statistics

             // change to the other player

// END IF

// END WHILE

return 0;

}

// FUNCTION DEFINITIONS GO HERE:

//////// Here is a sample output ///////

How many rods are in this game? 2

[space line]

How many stones are on rod1: 2

How many rods are on rod 2: 3

[space line]

Rod 1: **                   (40.000%)

Rod 2: ***                 (60.000%)

[space line]

Rod 1 has the smallest number of stones with 2 object(s).

Rod 2 has the largest number of stones with 3 object(s).

The average number of stones per rod (i.e., rods with stones) is 2.50 stones.

[space line]

Player (1) : Which rod would you like to play? 2

Enter number of stones to remove (3 or less) from rod 2: 3

[space line]

Rod 1: **                          (100.000%)

Rod 2:                              (0.000%)

[space line]

Rod 1 has the smallest amount of stones with 2 object(s).

Rod 1 has the largest amount of stones with 2 object(s).

The average number of stones per rod (i.e., rods with stones) is 2.00 stones.

[space line]

Player (2) : Which rod would you like to play? 1

Enter the number of stones to remove (2 or less) from rod 1: 2

[space line]

Congratulations! Player 2 wins.

Explanation / Answer

Here is the code for the question along with output. Please don't forget to rate the answer if it helped. Thank you very much.

#include <iostream>

#include <iomanip>

using namespace std;

int getNumberOfRods();

int getRodDetails(int rods[], int n);

void drawRods(int rods[], int n, int total);

void displayStats(int rods[], int n, int total);

void promptPlayer(int player, int rods[], int &rod, int &stones);

void removeStones(int rods[], int rod, int stones, int &leftout);

// FUNCTION PROTOTYPES HERE

int main () {

// Define variables

int n; // the number of rods

int rods[20];

int total;

int leftout ;

int player = 1;

int rod, stones;

// algorithm:

  

// prompt and read number of rods

n = getNumberOfRods();

  

// prompt and read the number of objects in each rod

total = getRodDetails(rods, n);

leftout = total;

// draw the rods with percentages

drawRods(rods, n, total);

// display statistics

displayStats(rods, n, leftout);

// WHILE some rod is NOT empty DO

while(leftout > 0)

{

  

// prompt and read the next player's move

promptPlayer(player, rods, rod, stones);

  

// remove the specified number of objects from the specified rod

removeStones(rods, rod, stones, leftout);

  

  

// IF all the heaps are empty, THEN

if(leftout == 0)

{

// Print a message congratulating the winning player

cout << " Congratulations! Player " << player << " wins." << endl;

}

// ELSE

else

{

// Redraw the rods with percentages

drawRods(rods, n, leftout);

// Display statistics

displayStats(rods, n, leftout);

// change to the other player

if(player == 1)

player = 2;

else

player = 1;

} // END IF

}

// END WHILE

return 0;

}

// FUNCTION DEFINITIONS GO HERE:

int getNumberOfRods()

{

int n;

while(true)

{

cout << "How may rods are in this game? ";

cin >> n;

if(n < 0 || n > 20)

continue;

else

break;

}

  

return n;

}

int getRodDetails(int rods[], int n)

{

int total = 0;

cout << endl;

for(int i = 0; i < n; i++)

{

cout << "How many stones on rod " << (i+1) << ": ";

cin >> rods[i];

total += rods[i];

}

return total;

}

void drawRods(int rods[], int n, int total)

{

cout << endl;

  

for(int i = 0 ; i < n; i++)

{

cout << "Rod " << setw(2) << (i+1) << ": ";

string str(rods[i], '*'); //a string with * , as many as current value of rods[i]

cout << left << setw(25) << str;

cout << fixed << setprecision(3) << "( " << (rods[i] * 100.0 / total ) << "% )" <<endl;

}

}

void displayStats(int rods[], int n, int total)

{

cout << endl;

  

int smallestRod = 0, largestRod = 0;

  

for (int i = 1; i < n; i++)

{

if(rods[i] < rods[smallestRod])

smallestRod = i;

if(rods[i] > rods[largestRod])

largestRod = i;

}

cout << "Rod " << (smallestRod + 1) << " has the smallest number of stones with " << rods[smallestRod] << " object(s)." << endl;

cout << "Rod " << (largestRod + 1) << " has the largest number of stones with " << rods[largestRod] << " object(s)." << endl;

cout << "The average number of stones per rod (i.e., rods with stones) is " ;

cout << fixed << setprecision(2) << (total * 1.0 / n) << " stones." << endl << endl;

}

void promptPlayer(int player, int rods[], int &rod, int &stones)

{

while(true)

{

// prompt and read the nect player's move

cout <<"Player ("<< player << ") : Which rod would you like to play? ";

cin >> rod;

if(rods[rod-1] < 1)

cout << "There are no stones on rod " << rod << endl;

else

break;

}

  

while(true)

{

cout << "Enter number of stones to remove (" << rods[rod-1] << " or less) from rod " << rod << ": ";

cin >> stones;

if(stones <= rods[rod-1])

break;

else

cout << "Rod " << rod << " does not have " << stones << " stones!" << endl;

}

}

void removeStones(int rods[], int rod, int stones, int &leftout)

{

rods[rod - 1] -= stones;

leftout -= stones;

  

}

output

How may rods are in this game? 2

How many stones on rod 1: 2
How many stones on rod 2: 3

Rod 1: ** ( 40.000% )
Rod 2 : *** ( 60.000% )

Rod 1 has the smallest number of stones with 2 object(s).
Rod 2 has the largest number of stones with 3 object(s).
The average number of stones per rod (i.e., rods with stones) is 2.50 stones.

Player (1) : Which rod would you like to play? 2
Enter number of stones to remove (3 or less) from rod 2: 3

Rod 1 : ** ( 100.000% )
Rod 2 : ( 0.000% )

Rod 2 has the smallest number of stones with 0 object(s).
Rod 1 has the largest number of stones with 2 object(s).
The average number of stones per rod (i.e., rods with stones) is 1.00 stones.

Player (2) : Which rod would you like to play? 1
Enter number of stones to remove (2 or less) from rod 1: 2

Congratulations! Player 2 wins.