Write a program for playing the game of nim

Assignment Help Computer Engineering
Reference no: EM132210515

Question :

Write a program for playing the game of NIM. Our version of the game starts with up to 20 pegs and up to 10 discs in each peg.

Two players take turns removing discs from the pegs. On a player's turn, she chooses a peg and removes one or more discs from that peg.

She can remove all discs from the peg. The player who takes the last disc from the pegs (so that all pegs are empty) loses. This version of the game is called misere.

Implement the follwing pseudo code.

i. Prompt and read the number of pegs

ii. Prompt and read the number of discs in each peg;

iii. Draw the pegs with percentages;

iv. Display statistics;

v. while (some peg is NOT empty) do

vi. Prompt and read the next player's move (Prompt and read the peg to modify and the number of discs to remove from the peg);

vii. Remove the specied number of discs from the specied peg;

viii. if (all pegs are empty) then

ix. Print a message congratulating the winning player.

x. else

xi. Redraw the pegs with percentages;

xii. Display statistics;

xiii. Change to the other player;

xiv. end

xv. end

1- In addition to implementing the above algorithm in the main function, you must define the following constants and variables in the main function:

- Define two constants to hold the maximum number pegs (15) and the maximum number of discs per peg (10).

- The list of pegs will be stored in an array of integers, where each integer represents the number of discs in that peg. Define a pointer variable for this array (see the lecture notes on "Arrays and Pointers"). Also define an integer to hold the size of this array.

- Define a variable that represents the current player (1 or 2).

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 pegs (see the constant defined above) and return an integer. Prompt the user for the number of pegs used in the game. If the number entered is not between 1 and the maximum number of pegs 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 pegs (see description above) to have the same size as the number of pegs 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 pegs, the size of the array, and the maximum number of discs per peg (see the constant defined above).

This procedure will traverse the array of pegs and call the following helper procedure for each peg in the list. 3 - Write a helper procedure that will prompt the user for the number of discs to place on this peg.

If the number of discs entered for the peg is not between 1 and the maximum number of discs allowed then display a warning message and prompt again. Define this helper procedure to have three input parameters: the array entry for this peg (use pass by reference), the array index for this peg, and the maximum number of discs per peg. 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.

11. Write a procedure to draw the pegs to implement steps (iii) and (xi) of the algorithm. Draw each peg on its own row. In the row for peg i, there is a label "peg i: " followed by n %'s where n is the number of discs in peg i. The procedure should output an empty line before and after the first and last rows, respectively.

For instance if peg 0 has 3 discs, peg 1 has zero discs, and peg 2 has 8 discs, the procedure (and helper procedures) should output: Peg 0: %%% (27.273%) Peg 1: (0.000%) Peg 2: %%%%%%%% (72.727%) Define the procedure to have two input parameters: the array of pegs and the size of the array. First, calculate the total number of discs in the array of pegs. Next, traverse the array of pegs and call a helper procedure to display each complete row.

- Write the helper procedure to have three input parameters: the array index for this peg, the number of discs for this peg, and the total number of discs in the array of pegs. The percentage displayed at the end of each row is the fraction of discs on this peg given the total number of discs in the array of pegs (you just computed). Format each row using setw in iomanip and avoid using hard coded spaces. When there are ten or more pegs to display, your output must align the labels "peg ?:" and "peg ??:" appropriately, where "?" is a digit. Use setw to format the text between "peg" and the ":".

Using setw, format the %'s in a column of size ten that is left justified (look up the "left" specifier under iomanip). There needs to be five spaces between the column of %'s and the percentage. Do NOT hard code these five spaces. Instead, use setw to format the last column for the percentage. Look up how to use the "right" specifier in iomanip to help you solve this problem. Also, format the percentage to show three digits after the decimal place. Decide if additional helper functions/procedures would be useful here.

12. Write a procedure to display the statistics for the list of pegs that will be called in steps (iv) and (xii) of the algorithm. The statistics are

1) The pegs with the smallest number of discs,

2) The pegs with the largest number of discs, and

3) The average number of 4 discs per peg taking into account only pegs with discs. Define the procedure to have two input parameters: the array of pegs and the size of the array. This procedure will call three helper procedures.

- Write a helper procedure to display the pegs with the smallest number of discs. This procedure will have two input parameters: the array of pegs and the size of the array. Note that there may be more than one peg with the smallest number of discs.

- Write a helper procedure to display the pegs with the largest number of discs. This procedure will have two input parameters: the array of pegs and the size of the array. Note that there may be more than one peg with the largest number of discs.

Write a helper procedure to display the average number of discs per peg, but only taking into accout pegs with at least one disc. The average must be formatted to display two digits after the decimal place.

13. Write a function which returns true (boolean) if all the pegs have zero discs and returns false otherwise. Call this function to implement steps (v) and (viii) of the algorithm. Note that the same function will be used for both steps.

14. Write a procedure to implement step (vi) of the algorithm where the current player makes her next move. Define the procedure to have five input parameters: the array of pegs, the size of the array, player id, which peg chosen by the player on this turn, and how many discs the player would like to remove from this peg. The player id is either the integer 1 or 2 to indicate which player is taking a turn. This procedure will perform two steps with the help of helper functions/procedures as specified below:

1) Using the following helper functions and procedures, prompt and read for the peg from which to remove discs. If the player inputs an invalid peg id or chooses a peg with no discs then display a warning message and prompt again. These three helper functions/procedures will be called from this procedure. In addition, the last two helper procedures will call the first helper function.

- First, write a helper function to have one input parameter, the player id. Prompt the player to choose a peg (peg id) and then return this value. Thus, this helper function has a return type that is NOT void. Do not perform any input validation in this function.

- Next, write a second helper procedure to have three input parameters: the player id, the peg selected by this player, and the total number of pegs (i.e., the size of the array of pegs). This procedure will validate the peg selected by this player, i.e. the peg selected must be between 0 and n - 1 where n is the total number of pegs. If the peg selected is invalid then the procedure displays a warning message and prompts for peg selection again by calling the first helper function.

- Finally, write a third helper procedure to have three input parameters: the array of pegs, the player id, and the peg selected by this player. This procedure will validate 5 whether the peg selected has at least one disc.

If the peg selected is invalid then the procedure displays a warning message and prompts for peg selection again by calling the first helper function. 2) Using the following helper function, prompt and read how many discs to remove from the chosen peg. If the player inputs an invalid number of discs then display a warning message and prompt again.

The number of discs to remove must be positive and must not exceed the number of discs on the chosen peg. The following helper function will be called from this procedure and will perform the following task described below. This procedure will check if the returned value from the helper function is valid and prompt the user again if necessary.

- Write a helper function to have two input parameters: the number of discs on the chosen peg and the peg id. The function will prompt the player for the number of discs to remove from the indicated peg and returns this value. Note this helper function will NOT perform any input validation checks. Instead the calling procedure will perform validation on the returned value from this helper function.

15. Write a procedure to implement step (vii) of the algorithm. Define the procedure to have three input parameters: the array of pegs, the peg id of the chosen peg, and the number of discs to remove. This function will modify the array of pegs by subtracting the specified number of discs from the chosen peg.

16. Write a procedure to implement step (ix) of the algorithm. Define the procedure to have a single input parameter that holds the player id of the current player. The procedure will print a message congratulating the winning player. The message should identify who won (player 1 or player 2).

17. Write a procedure to implement step (xiii) of the algorithm. Define this procedure to have a single input parameter that holds the player id. This procedure will switch the turn to the other player. In other words, if the player indicated in the input parameter is 1, then the procedure should change this value to 2. If the player indicated in the input parameter is 2, then the procedure should change this value to

18. Be sure that there is a comment documenting each variable.

19. Be sure that your if statements, for and while loops and blocks are properly indented.

Reference no: EM132210515

Questions Cloud

Write a program to print the following table : Write a program to print the following table. Each row in the table has an integer, its square, its cube, and then its square root to 2 decimal places.
Write a program which prompts a user for 5 2-d : Write a program which prompts a user for 5 2-D (x,y) coordinates and writes them to a file "points.txt", one point (x,y) coordinate pair per line.
Case study - local area network design : Detail explanation of the planning and designing of network. It is recommended that bullet points are included whenever necessary
What is the expected value and standard deviation : What are the investment proportion in the minimum variance portfolio of the selected five risky funds, and what is the expected value and standard deviation
Write a program for playing the game of nim : Write a program for playing the game of NIM. Our version of the game starts with up to 20 pegs and up to 10 discs in each peg.
Write a program to perform rounding on a floating point : Write a program to perform rounding on a floating point number input by the user, based on the fractional part of the number.
Compute the day of the week for any date entered by the user : Write a program named DayOfWeek that computes the day of the week for any date entered by the user.
Write function that creates string of path to a directory : Write a function that creates a string of the path to a directory. Use makeEmptyPicture to create a blank picture in the correct size.
Write a program to populate an array of size ten : Write a program to populate an array of size ten with ten numbers obtained through user input. Cycle through the array and generate two sums.

Reviews

Write a Review

Computer Engineering Questions & Answers

  What do you mean by addressable memory capacity

What do you mean by addressable memory capacity.

  Find all of the substrings that form numbers

Given a string of numbers, identify all of substrings that form numbers that are divisible by 3. For instance, applying the algorithm on the string 37540 should produce the following substrings (not necessarily in this order): 0; 3; 75; 54; 375; 5..

  Draw the logic diagram of a 2-bit demultiplexer

Draw the logic diagram of a 2-bit demultiplexer, a circuit whose single input line is steered to one of the four output lines depending on the state of the two control lines.

  Make an html document that includes a javascript

make an html document that includes a JavaScript program that creates a new constructor function named Automobile in the document head.

  Write a program to run a tournament

Write a program to run a tournament. Start with your project 3 program for creatures. If you had issues with project 3 and need to begin fresh.

  Prepare the project work breakdown structure and resource

you are a small project management consulting company called big-proj that is looking for that big break. you are one

  Write a program that retrieves values stored in a text file

Write a program that retrieves the values stored in a text file. The file should contain 10 different rows of data with five values per line.

  How is polymorphism like a switch statement

How is Polymorphism like a Switch statement? Explain what are the key characteristics of Big O.

  How cool ninjas and how they would like to become a ninja

Ninja Tactics Now that your friend is one the path to becoming a ninja, it's time for them to learn about some of the sneaky tactics that ninjas employ.

  Create a decision tree illustrating the cost and probability

Create a decision tree illustrating the cost and probability of each option and then identify the best decision for the project.

  Calculate the alternating sum of the first digits

Do a search for how to write a for-loop in R. Practice some simple examples from the internet.

  Questiondesign a tips class that calculates the gratuity on

questiondesign a tips class that calculates the gratuity on a restaurant meal. its only class member variable taxrate

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd