Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 5.11.
Task one is to print a bunch of messages, so we'll start with that.
System.
Dot dot.
Print line printing the following.
And then there are four more things to print.
So I'm just going to copy these inside of comments.
Create for print line statements.
Great to are the rules.
If you rule a six, the game stops.
And if they rule A for nothing happens.
Otherwise you get one point.
Splendid.
And last thing to print, you must collect at least three points to win.
And so this last part implies waiting for the user to submit something.
So here we can just say scan next line, and that should suffice.
All right.
Task three is to make a wild loop that runs forever.
Place your last scanned next line as the first line in the loop.
And in the second line print.
Hey, for now.
Simple enough.
So if you want to define an infinite wild loop, you just say, Well, true.
A wild loop always runs so long as its condition is true.
But if you put true, then it's going to run forever.
So our instruction was to put this as the first line and then to print.
Hey.
Now we get errors here because this is now a dud code.
It's unreachable code because the while loop runs forever without ever being stopped, which means this
will never get reached.
So let's comment it out for now.
Place score of zero on top.
Okay.
In the interest of time, I'll press the run button.
Keep pressing next.
All right.
Now it says remove the hate print.
Will do just that.
Define the roll dice function based on the following comment.
We did something extremely similar to that in workbook 5.10.
So you will say public static.
It's going to return an integer.
The function name will be called a roll dice.
All right.
First, your remember that math at random returns a decimal in the range of zero to less than one so
it can return any decimal between 0 to 2.99999.
And if you multiply it, whatever, decimal gets returned by six.
If, let's say you got the minimum value, zero times six is still zero.
But if math random were to return the maximum possible value of 0.99999, then that max value would
become less than 65.999.
So double random number can be any decimal within this range and whatever number we get, we're going
to have to add one to it.
Which means if this happened to be the minimum number now, it would be one.
If it happened to be the maximum number that this piece of code can return.
Now it's less than 76.999.
And now if you were to typecast whatever random number ended up being at line number 28.
One would still be one.
But if this happened to be the maximum possible value of 6.999, it would just become six.
Which means at this point we're returning a random number between one and six, which is pretty much
a dice roll.
All right.
That would have been your hardest task, in my opinion.
But we've done this countless times before.
So task number five is during each run, call a roll dice and store the value in dice or roll.
So here we've got a variable dice roll.
We'll set that equal to roll dice.
Simple enough.
Did we have to print it?
Uh, we sure did.
We have to print.
You rolled a dice roll.
All right, In the interest of time, I'll just use the run button.
And the loop runs forever.
Every single time it runs, it prompts me to press.
Enter.
When I press enter, we're rolling a dice and then we're printing it.
Nothing too crazy.
Moving on to task number six.
During each run, if they roll a six print end of game and stop it.
So we would have to break the loop, quote unquote.
If they were live four, then they would just continue with the loop.
Otherwise, if they're all anything else, update the points variable by one and get them to keep rolling.
So if.
The dice roll is equal to six.
We have to print.
And of a game.
And break the loop.
Otherwise if the dice roll is equal to four.
Then nothing happens.
Zero points.
Keep rolling.
Else.
If they roll anything that isn't six or four, then we can update the points variable by one.
There is no points variable, but there is a score variable.
So we'll say score.
Plus plus.
And then we're going to print.
One point.
Keep rolling.
All right.
And then after the game ends, So after it reaches this if statement and the while loop breaks, we
need to check if their score ended up being greater than or equal to three, in which case they win.
Otherwise they lose.
So here we say if.
Their score ended up being greater than or equal to three.
Then we'll just say you win.
Otherwise you lose.
And that's all.
We created a dice rolling game using wild loops.
Before I forget, let me close scanner.
And.
All right, I'm not going to put a breakpoint here because for some reason, if you put next line right
after your while loop, it makes it really hard to visualize the runtime.
So I'm just going to put a breakpoint over here and one at the beginning of the if elseif else chain
and we should be good.
So now we're probably at line 17, which means we have to enter something.
Dice roll is going to equal whatever roll dice returns.
In this case, we've got a dice roll of one that does not equal six.
That does not equal four.
So our score increases by one because we rolled a good dice.
And we start again.
This time I roll a four.
So fortunately for me, four means the game doesn't end.
I'm still good, but I don't get a point on this turn.
All right.
This time I wrote a three that is just perfect, which means I get a point and I get to keep rolling.
I think I'm going to win.
Let's find out.
I roll it, too.
That is amazing.
And get another point, which means I will eventually win.
Next line, press submit.
Roll the dice.
Five.
It is my lucky day.
I have got four points.
Press submit.
This time I roll of four no points.
I'm going to fast forward the video until I eventually roll a six because you're probably getting really
bored of me right now.
Never mind.
I guess I won't have to fast forward because I did just roll with six, which means end of Game Break
is going to break the entire while loop.
And now that the loop has been broken, I ended up with a score of four, which is greater than or equal
to three, which means I win.