The assignment is to create a small game in which the player (i.e., the user) starts with 100 SEK (Swedish crowns) and may bet on a series of coin tosses.
The player starts by betting a number of SEK within his budget and then chooses to bet on either heads or tails for the next toss. After the player has placed his bet, the program will simulate a coin toss and present the result for the player. If the toss was in line with the player's bet, the player wins as much money as was betted, but if the toss resulted in the opposite, the player instead loses his bet.
The player may quit the game at any time by betting 0 SEK. If the game is quit, the final amount of SEK that the player had should be written to the screen. The game is also over if the player runs out of money.
Below is an example run of the program for you to get an idea of how it may look. The program does not have to look exactly like in the example.
Example run (the player's input is in bold):
You have 100 SEK. How much do you want to bet? 50
What do you want to bet on? 1 for heads, 0 for tails: 0
Tossing coin… it is heads!
Sorry you lost 50 SEK.
You have 50 SEK. How much do you want to bet? 25
What do you want to bet on? 1 for heads, 0 for tails: 1
Tossing coin… it is heads!
Congratulations you won 25 SEK.
You have 75 SEK. How much do you want to bet? 75
What do you want to bet on? 1 for heads, 0 for tails: 1
Tossing coin… it is tails!
Sorry you lost 75 kr.
Game over! You have no more money!
Rules and Implementation
You will make the implementation of this program by using a set of functions. We will start by writing the functions. We will make three functions: one that takes a bet from the player, one that makes the player guess heads or tails and finally one function that tosses the coin. It is important that you finish one step at the time, i.e., don't move on the step 2 before you are sure step 1 works as it should.
You may not use global variables, static variables or the goto-statement in this assignment. If you don't know what those things are you dont need to worry.
Step 1:
Write a function getPlayerBet() which takes the player's amount of money as argument. The function should ask the player how much he wants to bet. The player should not be able to bet more money than he has and also not a negative amount. If the player bets an incorrect amount of money, the player should be asked again until a correct amount is given. The function should then return the bet.
Start by designing how the function head should look like, and then implement the actual function.
Test that the function works properly by making a few calls to getPlayerBet() from main(). You don't need to make a full program, you can just call the function with any amount of money and just test that it rejects erroneous bets and returns correct ones.
Step 2:
Write a function getPlayerGuess(). This function should not take any arguments. It should ask the player to bet on either heads or tails. You can let the number 1 represent heads and let 0 represent tails. If the player enters 0 or 1, that number should be returned. Otherwise, the program should ask the player for a new number until 0 or 1 is entered.
Test your function by calling it from main() just like in step 1.
Step 3:
Write a function tossCoin(). This function takes no arguments. It should print the message "tossing coin" on the screen and choose heads or tails randomly (just like in exercise 5.31 you did earlier!). The function should return 1 if the result is heads and 0 if the result is tails.
Test it like the other functions.
Step 4:
Finally write the main() program that makes the program work as in the example. You must call all three functions outlined in step 1,2, and 3 in your solution. It is a good idea to draw a flowchart or write pseudocode so that you know when the functions should be called and what you will do with arguments and return values.
Note that you should not change number of arguments or anything else in the functions from steps 1-3 to make it work.