Prompt and read the number of discs in each peg

Assignment Help Computer Engineering
Reference no: EM132211171

Assignment 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: EM132211171

Questions Cloud

Print appropriate column headings : 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.
Maslow hierarchy of needs in motivating employees : In what ways might managers make use of Maslow's hierarchy of needs in motivating employees?
Write a program which prompts a user for 5 2-d : Write a second program to open the same file , read the two vectors (10 numbers) back in.
Prepare an unclassified balance sheet in proper form : Prepare an unclassified balance sheet at December 31, 2017 in proper form. Prepare the journal entry for each January transaction.
Prompt and read the number of discs in each peg : 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.
We planned to deliver and what we actually delivered : Using this data, what can we say about the relationship between what we planned to deliver and what we actually delivered?
Find historical stock prices for each firm : Find historical stock prices for each firm from Yahoo! Finance or another source. Use the closing stock prices (not the adjusted close).
Supporting core competencies in employees : When do you think HR professionals should focus on identifying, developing, or supporting core competencies in employees?
Benefits of blogs : Some topics already state a definite position/stance and some are more general where you can pick your own position.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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