In this programme we're going to pull together most of the concepts we've learned so far into a bigger programme.
00:08
As always, we'll create the boilerplate and, because this programme will make use of random numbers, we'll declare a random generator, in the global section so that it's available to all our subprograms without the need to pass it between them.
00:23
The object of the game is to achieve a total score of over 100 on a turn.
00:28
A player rolls two dice and adds together the face value of the dice, adding that to their score for the turn.
00:35
If a player rolls a one on either die, their turn is over, and they lose their score for the turn.
00:41
Otherwise they can choose to bank their total and end their turn or take another roll to potentially add the roll again to their total.
00:50
To make the game easier to write, we'll use problem decomposition to break the problem down and start with writing a couple of handy functions.
01:00
Static int, which will be an array, hence open and close brackets.
01:04
Roll.
01:04
This function will roll two six sided dice and return the result in an array.
01:09
Let's enumerate the array.
01:12
Int Dice has the numbers 0 and 0.
01:16
Note, we're not using quotes here because we want the numbers, not a string.
01:21
Dice 0, that's the first dice, because things tend to start at zero in computing, will become the random generator's next random number between 1 and 7 exclusive, which means 1 and 6 dice 1, which is actually the second dice, will become a random number between 1 and 6, then return the dice.
01:43
So we didn't really need to use an array here.
01:45
It only has two values, so we could have just used two variables.
01:49
But doing it this way allows us to return a single structure and not two variables, which is not possible in some languages.
01:58
Also notice how we enumerated the array with two zeros to start.
02:02
This just gave us two indexes or elements that we could change.
02:06
There are other ways to do this, such as stating the number of indexes for the array, but this approach enables an easy comparison to Python.
02:14
If you're interested in doing that.
02:17
The next subprogram, will calculate the points from the static int points from the dice.
02:24
If dice 0 is 1 and dice 1 is a 1, then they're both 1 and the player loses their turn.
02:32
We'll return a minus 1 to indicate that else.
02:37
If dice 0 is a 1 or dice 1 is a 1, then only one of the two dice is a 1.
02:43
We score 0 and return that.
02:46
This is almost the same outcome, but we'll use minus 1 and 0 in an extension to the game later in all other cases.
02:55
Else we return the sum of the two die.
02:58
That's dice zero plus dice one.
03:03
That's the helper functions written.
03:05
We can now write the actual static voidplaygame.
03:12
We'll need some variables to manage the game.
03:14
Score will be an array holding the scores for both players.
03:18
These will start at 0 and 0.
03:21
Player will hold whose turn it currently is.
03:24
Our two players are numbered 0 and 1.
03:27
The starting player will be player 0.
03:29
So player is assigned to be 0.
03:32
Total will hold the running total of the dice so far before the player chooses to bank which they could lose before it is banked.
03:40
So we can't store that in score.
03:44
This is a new player's turn, which is helpful to know so that we can provide the players with an indication of when a new player's turn has started.
03:52
We use a variable called newplayer to store this and assign it to be true.
03:59
Turn end will allow us to know if the player's turn has ended.
04:03
This is currently false.
04:05
We also need to store the player's choice of whether to bank or gamble.
04:09
So we'll say choice is currently an empty string.
04:14
The game continues until either player has reached more than 100 points.
04:18
So while score for player 0 is less than or equal to 100 and score for player 1 is less than or equal to 100, do the following code.
04:33
If it's a new player, we'll give them a handy prompt.
04:36
A blank line makes the output look nicer.
04:38
and some hyphens too.
04:40
Then we tell the player whose turn it is.
04:43
Note that we're using player one in this line.
04:46
That's because humans will think it's a bit odd.
04:48
If the computer outputs player zero, it's your turn.
04:52
We normally number players from one upwards.
04:54
In human terms, it's only the computer that starts an index at 0.
04:59
So if we add 1 to the player number, that will make more sense to the players.
05:04
Player one is actually player zero and player two is actually player one.
05:09
In the code, we will then output the score for the current player.
05:14
This is where we have a real advantage of storing both the player scores with one array instead of two variables.
05:21
It allows us to use the index that's the variable player to specify which player we're referring to.
05:28
The alternative would require two variables, one for each score and an if statement to output the correct one.
05:36
There's no need.
05:37
We can simplify the code a lot by using arrays.
05:40
When we have more than One of something to store, in this case more than one player and also more than one dice.
05:50
Assign the total score to be zero.
05:52
It's no longer a new player's turn and their turn has not ended.
05:56
So let's set, both of these to false.
05:59
That's everything we want to do if it's the start of a new player's turn.
06:03
So we close the curly bracket for the if statement.
06:06
Tell the player to press enter to roll the dice.
06:10
Collect the input, but don't store it in a variable.
06:13
We just want to know they pressed enter the array.
06:17
Dice becomes the result of a roll that we've already written a subprogram for, and the result becomes the return value from the points subprogram we've already written.
06:28
To remember, minus 1 meant the player rolled a double 1, 0 meant a single 1 and any other roll is the sum of the two dice.
06:37
We see again how the use of subprograms has made the programme easier to read and write.
06:44
We can tell the player what they rolled by outputting the data in the elements zero and one of the dice array, concatenating the string to a single sentence for the console writeline command.
06:57
So now we can programme the logic of the game depending on the result of the dice roll.
07:03
If the result is greater than zero, output what the player scored.
07:08
That's the result.
07:09
Then running total then becomes whatever it is.
07:13
Now that's total plus the result of the dice output the new total.
07:19
The player now gets to choose if they want to gamble and roll the dice again.
07:23
If they roll a zero or a minus one, they'll lose their turn and the points for that round.
07:29
But they can also keep going until they bank or win.
07:34
So the user needs to say yes or no to continuing to roll the dice while choice is not Y and choice is not N.
07:44
Oops, I should have really put the curly bracket on the next line to make it easier to read.
07:48
But once you become good at C, you'll find programmers often put it on the same line.
07:55
Next, ask the user for their choice and, store this in a variable choice.
08:00
If the player's choice is N, which will mean no, then we will add their total from this round to their overall score.
08:07
Scores are stored in an array, so score for the player is equal to the score for the player.
08:14
That is what it is now, plus their total for the round.
08:19
We could also have written this as, score player plus equals total, which would have been neater.
08:26
They're turn ends, so we can assign turnend to Be true if they select Y, which will mean yes to gamble and have another roll.
08:37
The while loop will take care of that, so we don't need to write any more code to handle it.
08:42
We've already coded what happens when a player takes a roll.
08:48
However, they might have scored a zero or minus one, so.
08:51
So back to finish the if statement.
08:54
Else output.
08:55
Oh no, that's a pig out.
08:57
And end the turn.
09:00
If the turn has ended, we'll tell the player so that they can keep track of what's going on in the game.
09:05
With a handy prompt.
09:07
Press Enter to hand the dice to the next player.
09:10
Wait for the user to press enter by not doing anything with the input console readline.
09:16
That's a bit more abstraction there, as we're not really passing any dice, just changing the player variable.
09:24
Then player becomes whoever the player is now.
09:27
Plus one and modulo two.
09:30
This is a neat programming trick.
09:33
It means player zero will become player one or player one will become player zero because we took the remainder from dividing by two.
09:43
Now don't worry if you're thinking I wouldn't have come up with that because you could have achieved this with an if statement instead.
09:50
For example, if player equals 1, then player equals 0, else player equals 1.
09:58
That's fine too.
09:59
As you become more experienced, you become more aware of these little shortcuts, which actually increase processing speed too.
10:06
In the same way that we now understand arrays, we can use them instead of multiple variables.
10:12
You can use multiple variables, but it just makes the code longer to write.
10:18
Finally, we set new player to be true because it's the next player's turn.
10:23
In the main programme section, all that's left to do is called the Play gamesub programme.
10:29
That's the game written.
10:30
At the moment, we're treating a score of zero to be the same as a score of minus one.