Question: Programming is to practice how to think like a computer, so we can tell the computer what to do. Please always allocate enough time to
Programming is to practice how to think like a computer, so we can tell the computer what to do. Please always allocate enough time to think things over. Hit: Use meaningful variable names can help reduce logic error. For example, if we use a, b, and c to represent roll score, turn score, and game score, it is easier to add a score to a wrong variable when the program gets more complicated. Here is the sequence how a program is usually written:
1. Write sequential statements to get one thing to work first. Add one component at a time and test the result right away.
2. Add selection statements to get the program to respond to any selection once. Remember to implement one selection at a time and test every selection we implement.
3. Add loops to repeat certain parts of the program automatically. If we have loops inside loops, it is best to add from the inner most loop, validate results, and gradually add a layer of loop at a time until the outer most loop.
4. Add safety checks to avoid crash, out of range inputs, or infinite loops.
Homework 3 Pseudo Code: (There are many ways to write a program. Here is one of the potential steps to write Homework 3.)
1. Add your name, purpose, and copyright the program.
2. Clear command window and clear all variables.
3. Randomize the pseudorandom number generator with the MATLAB built-in rng function and provide shuffle as the function input.
4. Create a variable that will track the game score. Set the value of this variable to 0.
5. Create another variable that will track whether the player is on the score sheet. Set the value of this variable to 0 (0 means not on the score sheet).
6. Add one or more disp or fprintf function(s) to print a welcome message and briefly explain the game rules and score rules in the command window.
7. Create another variable that will count the number of turns. Set the value of this variable to 1. (A player can keep rolling dice in one turn until the player gets a Farkle or decides to end his current turn.)
8. Add another fprintf function to print the variable that counts the number of turns value on screen, using the variable created from step 7.
9. Create another variable that will track the turn score. Set the value of this variable to 0.
10. Add another fprintf function to print the variable that tracks the turn score value on screen, using the variable created from step 9.
11. Create another variable that will track the number of dice can be rolled. Set the value of this variable to 6.
12. Add another fprintf function to print the variable that tracks the number of dice value on screen, using the variable created from step 11.
13. Add a randi function to generate the number of dice amount of integers between 1 and 6 to represent rolling the number of dice amount of dice. Save the dice values in a new array variable.
14. Add 3 fprintf functions to print the dice values in 1 line: a. The first fprintf function prints the text Dice Values =. b. The second fprintf function prints the dice values with some spaces between each value. c. The third fprintf function moves the prompt to a new line.
15. Add a sort function to sort the dice values in the dice values array variable and save the sorted values into a new sorted dice value array variable.
16. Add 3 fprintf functions to print the sorted dice values in 1 line: a. The first fprintf function prints the text Sorted Dice Values =. b. The second fprintf function prints the sorted dice values with some spaces between each value. c. The third fprintf function moves the prompt to a new line.
17. Add an input function to ask the player to enter the score of this roll from keyboard. Save the players keyboard entry in a new variable.
18. If the score of this roll is greater than 0:
a. Update the turn score by adding the score of this roll to the turn score.
b. Add an input function to ask the player to enter the number of dice to set aside. Save the players keyboard entry into a new variable.
c. Update the number of dice by subtracting the number of dice set aside from the number of dice.
d. Add an fprintf function to show the player the number of dice the player can throw.
e. If the value of number of dice is equivalent with 0:
i. Add an fprintf or a disp function to indicate that all 6 dice have been set aside, the player can roll 6 dice again.
ii. Set the number of dice variable value to 6.
f. End.
g. Add an fprintf function to print the turn score in the command window.
h. If the turn score is greater than or equal to 500 AND the value of the variable that will track whether the player is on the score sheet is equivalent with 0:
i. Add an fprintf or disp function to congratulate the player for collecting the first 500 points and can be added to the score sheet.
ii. Add an input function to ask if the player wishes to keep rolling dice (y/n)? Save the players decision to a new variable.
iii. If the players decision is equivalent with n: 1. Set the variable that will track whether the player is on the score sheet value to 1. 2. Update the game score by adding the turn score to the game score.
iv. End.
i. Else if the value of the variable that will track whether the player is on the score sheet is equivalent with 1:
i. Add an input function to ask if the player wishes to keep rolling dice (y/n)? Save the players decision to a new variable.
ii. If the players decision is equivalent with n: 1. Update the game score by adding the turn score to the game score.
iii. End.
j. End.
19. Else:
a. Add an fprintf or a disp function to print Farkle! on screen.
b. Set the turn score to 0.
20. End.
21. Add an fprintf function to print the game score in the command window.
22. Run the program a few times and check the dice value, turn score, game score from the command window. After ensure they are correct, add semicolon (;) to the end of every line with the assignment operation (=) to stop the auto repeating values.
23. Now we want to add a while loop to keep looping steps 12 through 21 while the player chooses to keep rolling and the throw doesnt produce a Farkle.
a. Highlight the code block with codes generated from steps 12 through 21, and click the increase indent button.
b. Add a while right before the fprintf function from step 12. Add an end right after the fprintf function from step 21.
c. Create another new variable that will be used to evaluate the while loop condition right before the while loop. Set the value of this new variable to 1. d. Add the while loop logical expression to loop while the while loop condition variable value is equivalent with 1.
e. Locate the code block generated from step 18. h.iii. Inside the If the players decision is equivalent with n code block, add a new line to set the loop condition variable value to 0.
f. Locate the code block generated from step 18.i.ii. Inside the If the players decision is equivalent with n code block, add another new line to set the loop condition variable value to 0.
g. Locate the Else code block generated from step 19. Inside the Else code block, add a line to set the loop condition variable value to 0.
24. Test the program a few times to ensure the while loop will end when the player enters 0 for the score of the roll and when the player chooses to press n to end the current turn. After ensure they are correct, add semicolon (;) to the end of every line with the assignment operation (=) to stop the auto repeating values in the command window.
25. Add a line to increment the variable that counts the number of turns by 1 after the while..end loop.
26. Now we are ready to add another while loop that will keep looping when the total game score is less than 10000 points.
a. Highlight the code block with codes generated from steps 8 through 25, and click the increase indent button.
b. Add a while before the fprintf function generated for step 8. Add an end after incrementing the variable that counts the number of turns generated for step 25.
c. Add the while loop logical expression to loop while the total game score variable value is less than 10000.
27. Add an fprintf function to congratulate the player for winning after the outer while..end loop. Also print the variable that counts the number of turns value - 1 in the command window to show the player the number of turns took to win the game.
28. Play the game a few times to ensure the game runs correctly. After ensure they are correct, add semicolon (;) to the end of every line with the assignment operation (=) to stop the auto repeating values in the command window.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
