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

I am designing the game of NIM and am stuck on this part of the code. I need to

ID: 3759965 • Letter: I

Question

I am designing the game of NIM and am stuck on this part of the code. I need to write three functions as described in steps 8, 9 and 10. Part 10 is broken down into two functions. One of the functions is meant to be a helper function. Steps i and ii are what these functions are meant to do in the program itself. Use arrays where intended and pass the parameters as intended! Please write this in c++

P.S. The constant reference in part 8 holds a value of 20

i. Prompt and read the number of rods

ii. Prompt and read the number of stones in each rod;

8. Write a function to implement step (i) of the algorithm. Define the function to have one input parameter that holds the maximum number of rods (see the constant defined above) and return an integer. Prompt the user for the number of rods used in the game. If the number entered is not between 1 and the maximum number of rods then display a warning message and prompt again. This is called input validation on the user’s input.

9. In the main function, allocate an array in the declared array pointer representing the list of rods (see description above) to have the same size as the number of rods returned from the function in step (i).

10. Write a procedure (this is a function that does not return a value and so is declared with a return type of void) that implements step (ii) of the algorithm. Define the procedure to have three input parameters: the array of rods, the size of the array, and the maximum number of stones per rod (see the constant defined above). This 3 procedure will traverse the array of rods and call the following helper procedure for each rod in the list. - Write a helper procedure that will prompt the user for the number of stones to place on this rod. If the number of stones entered for the rod is not between 1 and the maximum number of stones allowed then display a warning message and prompt again. Define this helper procedure to have three input parameters: the array entry for this rod (use pass by reference), the array index for this rod, and the maximum number of stones per rod. Important: Again, a procedure is a function that has no return value. You must define these functions as procedures by defining their return type as void.

Explanation / Answer

StonesinRodGame.cpp

#include <iostream>

#include <iomanip>

using namespace std;

// Global constant

const int NUM_RODS = 3; // Number of rods

// FUNCTION PROTOTYPES GO HERE:

//rod_setup prototype

void Rod_setup(int rodcount[], int NUM_RODS);

//Player turn indicator prototype

void Player_turn(int &player);

//drawing rods as * prototype

void Rod_drawing(int rodsread[], int NUM_RODS);

//turn prototype

void turn(int rodsread[], int player, int NUM_RODS);

//win prototype

void win(int player);

int main()

{

int rod[NUM_RODS]; // rod[i] = Number of elements in ROD i

int player; // Player 1 or 2

int Players = 1; // player indicator


int rodssum;

// Read number of elements in each ROD

Rod_setup(rod,NUM_RODS);

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

{
rodssum = 0;

rodssum += rod[i];

}

Player_turn(Players);

// Draw rods as rows of *'s

Rod_drawing(rod,NUM_RODS);

// WHILE some ROD is not empty DO

do

{

// Read ROD to modify and number of STONES to remove from ROD

turn(rod, Players, NUM_RODS);

// Remove elements from ROD

// IF all the rods are empty, THEN

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

{

rodssum = 0;

rodssum = rod[i];

}

Rod_drawing(rod, NUM_RODS);

Player_turn(Players);

// Player has won.

// Print win message.

// ELSE

// Play continues

// Redraw rods.

// Change to other player.

// END IF

// END WHILE

} while(rodssum>0);

win(Players);

return 0;

}

// FUNCTION DEFINITIONS GO HERE:

void Rod_setup(int rod[], int NUM_RODS)

{

int stones = 0;

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

{

cout << "How many stones are in ROd "<<i<<" : ";

cin >> stones;

rod[i] = stones;

}

}

//Player turn indicator

void Player_turn(int & player)

{

if(player == 1)

{

player=player-1;

}

else

{

player=player+1;

}

if(player == 1)

{

cout << "Player 2's turn" << endl;

}

else

{

cout << "Player 1's turn" << endl;

}

}

//drawing rod as *

void Rod_drawing(int rodsread[], int NUM_RODS)

{

int stonesinrod = 0;

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

{

cout << "ROD " << i << ": ";

stonesinrod = rodsread[i];

for (int star = 0; star < stonesinrod; star++)

{

cout <<"*";

}

cout<<endl;

}

}

//the player's turn

void turn(int rodsread[], int player, int NUM_RODS)

{

int rod_number = 0;

int remove = 0;

if(player == 1)

{

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

}

else

{

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

}

cin >> rod_number;

while(rod_number >= NUM_RODS || rod_number < 0||rodsread[rod_number]==0)

{

cout << " Please try again."<<endl;

cout << "Enter number of objects to remove from rod "<<rod_number<<": ";

cin >> remove;

}

cout << "Enter number of objects to remove from rod "<<rod_number<<": ";

cin >> remove;

while(remove > rodsread[rod_number]|| remove<=0)

{

cout << "Please try again."<<endl;

cout << "Enter number of stones to remove from rod "<<rod_number<<": ";

cin >> remove;

}

rodsread[rod_number]= rodsread[rod_number]-remove;

}
void win(int player)

{

if(player == 1)

{

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

}

else

{

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

}

}

output

How many stones are in ROd 0 : 4
How many stones are in ROd 1 : 3
How many stones are in ROd 2 : 2
Player 1's turn
ROD 0: ****
ROD 1: ***
ROD 2: **
Player (1) : Which rod would you like to play? 0
Enter number of objects to remove from rod 0: 3
ROD 0: *
ROD 1: ***
ROD 2: **
Player 2's turn
Player (2) : Which rod would you like to play? 0
Enter number of objects to remove from rod 0: 1
ROD 0:
ROD 1: ***
ROD 2: **
Player 1's turn
Player (1) : Which rod would you like to play? 1
Enter number of objects to remove from rod 1: 2
ROD 0:
ROD 1: *
ROD 2: **
Player 2's turn
Player (2) : Which rod would you like to play? 1
Enter number of objects to remove from rod 1: 1
ROD 0:
ROD 1:
ROD 2: **
Player 1's turn
Player (1) : Which rod would you like to play? 2
Enter number of objects to remove from rod 2: 2
ROD 0:
ROD 1:
ROD 2:
Player 2's turn
Congratulations! Player 1 wins.

Exit code: 0 (normal program termination)

*****************************ALL THE BEST****************************************************************