Prog 1: Dice
Changes made 8/30 shown in blue.
Write a C++ program to implement a dice game, where each player chooses one of Red, Green and Blue dice and rolls them to see who wins.
What You Need to Know
Environment: How to use the codio.com environment to edit, compile and run a program. Piazza contains a link you can use to get into codio.
To use codio for this class you need to point your browser to codio.com and create an account, if you haven't already done so. From your student's dashboard, you can then join our class by entering the two word phrase:
brush-mono
or you can simply use the following URL: https://codio.com/p/signup?classToken=brush-mono
Once you are in codio, you should use the project called "Program 1 Dice to Submit"
C++ Language: simple input ( cin) and output (cout); Using variables; The assignment statement (=); How to compare two variable values; Repeating a group of statements using a while loop; Choosing between alternatives using an if statement; Using #include <cstdlib> so that the random number generator rand() works; Using the mod (%) operator
Notes
Read the parts of our online Zybook that you need to use in this program.
See the sample program prog1Step0.cpp, which gives you a good starting point, as well as illustrates various programming concepts you need for this program.
You may work ahead and use concepts we have not yet discussed in class if you would like to. Though not necessary, dividing up your code using functions would simplify your solution.
Make sure you read the syllabus regarding the grading criteria. Particularly on the first program, students tend to lose points for not documenting adequately, for not choosing meaningful variable names, and for not including the required header within their code. Make your program output look exactly as specified. That way we can give you a good grade.
Steps
Work on your program one step at a time as described below. For this program you may assume the user provides perfect input.
Getting Started
Create a program with just an int main() function. Add the header documentation, so you don't forget later!
Add code inside main() to display your ID information (program, class, author, lab, etc). Add code inside main() to display the instructions. Display the prompt for user input, but don't yet read in anything. Running your program at this point should give you the following:
Class: CS 141 Lab: Tues 5am System: C++ in codio.com Welcome to the dice game, where you and your opponent each choose one of the red, green or blue dice and roll them. The dice contain the following sides: Red: 1 4 4 4 4 4 Green: 2 2 2 5 5 5 Blue: 6 3 3 3 3 3 Enter the die color (R G or B):
Single Player
Read in the first user input die letter, which should be an upper-case R, G, or B, and use that letter to determine which die should be used. Use the random number generator rand() along with the mod (%) function to get a random value 0..5 representing which die face is chosen. Use the die color (R, G, or B) along with which face was randomly selected (0..5) to determine the numerical value. For instance, if the Red die was selected, the six values for each face are 1 4 4 4 4 4. The first face (face 0) corresponds to the first value (1), while all the other 5 face values all have the value 4. Running your program at this point should give you the following:
Class: CS 141 Lab: Tues 5am System: C++ in codio.com Welcome to the dice game, where you and your opponent each choose one of the red, green or blue dice and roll them. The dice contain the following sides: Red: 1 4 4 4 4 4 Green: 2 2 2 5 5 5 Blue: 6 3 3 3 3 3 Enter the die color (R G or B): R Value: 4
Giving different user input (G or B) will give different values, corresponding to the options available on those dice.
Two Players
Prompt not only for the die color for player 1, but for player 2 as well. You will need to now call the random number generator rand() a second time, to find out which die face to use for the second player. Display both the player 1 and the player 2 values. Declare two variables to keep track of how many times each player has won. Compare the player1 and player2 values and increment the score for the player who has the highest value of the two, displaying the current score. Running your program at this point should give you the following:
Class: CS 141 Lab: Tues 5am System: C++ in codio.com Welcome to the dice game, where you and your opponnent each choose one of the red, green or blue dice and roll them. The dice contain the following sides: Red: 1 4 4 4 4 4 Green: 2 2 2 5 5 5 Blue: 6 3 3 3 3 3 Enter the die colors (R G or B): RG R:4 G:5, Score: 0 to 1
This should work at this point for any user input combination of two different letters chosen from R, G, and B.
Repetition
Add a loop so that the two selected dice are "thrown" and compared 50 times, with the number of wins for each player updated as you go. Add a move number. Move numbers and scores should now be right-justified in a field of 2 characters as shown below. Running your program at this point should give you the following:
Class: CS 141 Lab: Tues 5am System: C++ in codio.com Welcome to the dice game, where you and your opponnent each choose one of the red, green or blue dice and roll them. The dice contain the following sides: Red: 1 4 4 4 4 4 Green: 2 2 2 5 5 5 Blue: 6 3 3 3 3 3 Enter the die colors (R G or B): BG 1. B:3 G:5, Score: 0 to 1 2. B:3 G:2, Score: 1 to 1 3. B:3 G:2, Score: 2 to 1 4. B:3 G:2, Score: 3 to 1 5. B:3 G:2, Score: 4 to 1 6. B:3 G:2, Score: 5 to 1 7. B:3 G:2, Score: 6 to 1 8. B:3 G:5, Score: 6 to 2 9. B:6 G:2, Score: 7 to 2 10. B:3 G:5, Score: 7 to 3 11. B:3 G:2, Score: 8 to 3 12. B:3 G:5, Score: 8 to 4 13. B:3 G:2, Score: 9 to 4 14. B:3 G:2, Score: 10 to 4 15. B:3 G:2, Score: 11 to 4 16. B:3 G:2, Score: 12 to 4 17. B:6 G:2, Score: 13 to 4 18. B:3 G:5, Score: 13 to 5 19. B:3 G:5, Score: 13 to 6 20. B:3 G:5, Score: 13 to 7 21. B:3 G:5, Score: 13 to 8 22. B:3 G:5, Score: 13 to 9 23. B:3 G:2, Score: 14 to 9 24. B:3 G:2, Score: 15 to 9 25. B:3 G:2, Score: 16 to 9 26. B:3 G:5, Score: 16 to 10 27. B:3 G:2, Score: 17 to 10 28. B:3 G:5, Score: 17 to 11 29. B:3 G:2, Score: 18 to 11 30. B:3 G:2, Score: 19 to 11 31. B:3 G:5, Score: 19 to 12 32. B:3 G:5, Score: 19 to 13 33. B:3 G:5, Score: 19 to 14 34. B:3 G:5, Score: 19 to 15 35. B:3 G:5, Score: 19 to 16 36. B:6 G:5, Score: 20 to 16 37. B:6 G:5, Score: 21 to 16 38. B:3 G:5, Score: 21 to 17 39. B:3 G:2, Score: 22 to 17 40. B:3 G:2, Score: 23 to 17 41. B:3 G:2, Score: 24 to 17 42. B:6 G:2, Score: 25 to 17 43. B:3 G:2, Score: 26 to 17 44. B:3 G:5, Score: 26 to 18 45. B:3 G:2, Score: 27 to 18 46. B:6 G:2, Score: 28 to 18 47. B:3 G:2, Score: 29 to 18 48. B:3 G:5, Score: 29 to 19 49. B:3 G:2, Score: 30 to 19 50. B:6 G:5, Score: 31 to 19
As in the previous step, the output should handle all combinations of two different dice colors chosen from among R, G, B.
Thinking Beyond the Requirements:
Once you have it working, can you win more than half of the time? What happens if you roll two different dice of each selected color for each move? In other words, if you select Red and your opponent selects Green, each of you would have two dice of the same configuration.
Grading Rubric
Inside your program itself you must include the following grading rubric, just below your header documentation, as shown below. We will use this when grading your program as a place to annotate and list any deductions taken off within each category for program style. Note that this is in your code but is within a comment block so should not appear on the screen when you run your program.
/* prog1.cpp * Play the dice game, where each user chooses the Red, Green or Blue die. * * Class: CS 141 * Author: Dale Reed * Lab: Tues 5am * System: C++ online in codio.com * * 45 Programming Style Rubric points (Given only if program runs substantially correctly) * 10 Variable names are meaningful and use camel-case (e.g. totalCount) * 10 Comments: Has header. Comments on each significant block of code * 15 Appropriate data and control structures * 10 Code Layout: Appropriate indentation and blank lines */
The 45 points for program style will be allocated as described below. (While your personal style may disagree with the standards shown, it is important that be able to follow a coding standard that is given to you.)
Variable names are meaningful. Identifier names should indicate their purpose. Names should be in lowerCamelCase, where the first letter is lower-case and the first letter of each new word is upper case, and the words are concatenated together, e.g. gradesSum.
3 points: Most variable names are not meaningful
6 points: Most variable names are meaningful
10 points: All variable names, including function names (if other functions are used) are meaningful and written in lowerCamelCase
Comments. Every section of code (but not every line!) must have a short description stating its purpose. Comments should be easily identifiable. A person should be able to understand your program by reading only the comments.
3 points: Program has a header, but very few other comments
6 points: Program has header and some other comments
10 points: Program has header and each major section of code is appropriately commented. Any functions have their use documented, along with the role of any parameters.
Appropriate data and control structures: Appropriate looping and decision structures are used.
3 points: Global variables are used, declared outside of main()
6 points: if-else statements are not structured optimally
10 points: if-else statements are structured optimally, and a loop is used to repeat code
15 points: There is a minimum of code duplication
Code Layout. Different nested levels should have different indentation, where statements at the same level should have the same indentation. Indent at least 3 spaces. Use either spaces or tabs consistently, otherwise printouts will be messed up. Leave at most a single blank line between sections of code, and at most two blank lines between functions.
3 points: Indentation is inconsistent throughout program, or code indentation does not reflect the logical organization of program
6 points: There are extra blank lines in sections of code, or large sections of commented-out code, or the program explanatory header is missing
10 points:
Turning in your Program
Use the project called "Program 1 Dice to Submit", copying into it any work done in the old project. Within this project as you write your code for each of the four steps, you can test it and see the results of running each stage of your program. After the last step, you must press the button "Mark as Completed", after which you will no longer be able to edit your project, but will retain read access to it. Once you have marked it as complete, then we can grade it.