dice that have numbers from 1 to 11. To win, the player or the computer has to get to 21, or as close as possible without going over. If the player or computer goes over 21, they instantly lose. If there is a tie, the com
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Write a program that plays a dice game called "21"
It is a variation on BlackJack where one player plays against the computer trying to get 21 or as close to 21 without going over.
- Here are the rules of the game:
- You will play with dice that have numbers from 1 to 11.
- To win, the player or the computer has to get to 21, or as close as possible without going over.
- If the player or computer goes over 21, they instantly lose.
- If there is a tie, the computer wins.
-
- Starting the game:
- The player is asked to give the computer a name. For now, we'll simply call the computer opponent, "computer."
- The game starts with rolling four dice.
- The first two dice are for the player. These two dice are added up and the total outputted to the screen.
- The other two dice are for the computer. Likewise, their total is outputted to the screen.
- Starting the game:
-
- Player:
- If a total of 21 has been reached by either the player or the computer, the game instantly stops and the winner is declared.
- Otherwise, the player is asked if they want to roll one die that will be added to their total.
- Or they want to stop.
- The player should enter an 'r' to roll the die or an 's' to stop.
- The player may continue rolling until they have reached 21 or over.
- Player:
-
- Finishing the game:
- If the player total is > 21 then the computer is declared the winner.
- Otherwise, the computer has to make a decision to roll or to stop.
- If the computer total >= the player total, the computer stops.
- If the computer total < the player total then the computer continues to roll one die until the computer total is >= the player total.
- The winner is declared!
- Finishing the game:
-
- Writing this program (Main Assignment)
- Use the top-down
programming technique:
- Use the top-down
- Writing this program (Main Assignment)
-
-
-
- Don't code right away.
- Take a piece of paper and write out the general things that need to happen like we "how to drive a car" in class.
- Break the problem down into smaller pieces.
- Then, turn those smaller pieces into functions and needed variables that are to be passed or referenced.
- This will be worth 30% of the grade (submit a picture of it along with your program). Do not create this after you have written the program. It needs to show your creative thinking and problem solving. If this is done very well, you will receive extra credit.
- Before you write a bunch of code.
- Make sure the general flow of your program works and test more complex
algorithms or unfamiliar c++ functions by themselves. - Use outputs within a function that simply say something like "Function _______ is calculating the random dice totals."
- Then, write the code for the functions.
- Start to address the small pieces, coding and testing things as you go.
- If it's too difficult or confusing, write a simple version that tests things out like random numbers.
- If you get the code working for one die, then you can get the code working for two dice, etc.
- Make sure the general flow of your program works and test more complex
- Don't code right away.
-
-
Step by step
Solved in 2 steps