1 00:00:00,210 --> 00:00:03,810 Before we start, did you try solving the workbook yourself? 2 00:00:03,840 --> 00:00:09,600 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,900 --> 00:00:13,600 Welcome to Workbook 5.11. 4 00:00:13,600 --> 00:00:18,340 Task one is to print a bunch of messages, so we'll start with that. 5 00:00:21,470 --> 00:00:22,430 System. 6 00:00:22,430 --> 00:00:23,630 Dot dot. 7 00:00:23,630 --> 00:00:25,790 Print line printing the following. 8 00:00:29,440 --> 00:00:31,870 And then there are four more things to print. 9 00:00:31,870 --> 00:00:36,100 So I'm just going to copy these inside of comments. 10 00:00:39,570 --> 00:00:42,310 Create for print line statements. 11 00:00:42,330 --> 00:00:44,340 Great to are the rules. 12 00:00:47,990 --> 00:00:50,660 If you rule a six, the game stops. 13 00:00:53,810 --> 00:00:56,930 And if they rule A for nothing happens. 14 00:01:00,880 --> 00:01:03,790 Otherwise you get one point. 15 00:01:07,930 --> 00:01:08,770 Splendid. 16 00:01:08,770 --> 00:01:13,690 And last thing to print, you must collect at least three points to win. 17 00:01:16,100 --> 00:01:20,510 And so this last part implies waiting for the user to submit something. 18 00:01:20,510 --> 00:01:25,070 So here we can just say scan next line, and that should suffice. 19 00:01:35,580 --> 00:01:36,040 All right. 20 00:01:36,060 --> 00:01:39,370 Task three is to make a wild loop that runs forever. 21 00:01:39,390 --> 00:01:44,010 Place your last scanned next line as the first line in the loop. 22 00:01:44,010 --> 00:01:45,840 And in the second line print. 23 00:01:45,840 --> 00:01:46,710 Hey, for now. 24 00:01:46,710 --> 00:01:47,910 Simple enough. 25 00:01:47,910 --> 00:01:52,470 So if you want to define an infinite wild loop, you just say, Well, true. 26 00:01:53,850 --> 00:01:57,240 A wild loop always runs so long as its condition is true. 27 00:01:57,240 --> 00:01:59,990 But if you put true, then it's going to run forever. 28 00:02:00,000 --> 00:02:04,970 So our instruction was to put this as the first line and then to print. 29 00:02:04,980 --> 00:02:05,700 Hey. 30 00:02:08,090 --> 00:02:11,420 Now we get errors here because this is now a dud code. 31 00:02:11,420 --> 00:02:17,840 It's unreachable code because the while loop runs forever without ever being stopped, which means this 32 00:02:17,840 --> 00:02:19,070 will never get reached. 33 00:02:19,070 --> 00:02:20,850 So let's comment it out for now. 34 00:02:20,870 --> 00:02:23,180 Place score of zero on top. 35 00:02:27,100 --> 00:02:27,480 Okay. 36 00:02:28,150 --> 00:02:30,610 In the interest of time, I'll press the run button. 37 00:02:32,020 --> 00:02:33,580 Keep pressing next. 38 00:02:34,120 --> 00:02:34,840 All right. 39 00:02:35,170 --> 00:02:37,500 Now it says remove the hate print. 40 00:02:37,510 --> 00:02:39,130 Will do just that. 41 00:02:39,730 --> 00:02:44,660 Define the roll dice function based on the following comment. 42 00:02:44,680 --> 00:02:49,120 We did something extremely similar to that in workbook 5.10. 43 00:02:50,360 --> 00:02:52,340 So you will say public static. 44 00:02:52,340 --> 00:02:54,170 It's going to return an integer. 45 00:02:56,480 --> 00:02:59,330 The function name will be called a roll dice. 46 00:03:00,430 --> 00:03:01,050 All right. 47 00:03:01,060 --> 00:03:08,890 First, your remember that math at random returns a decimal in the range of zero to less than one so 48 00:03:08,890 --> 00:03:14,180 it can return any decimal between 0 to 2.99999. 49 00:03:14,200 --> 00:03:18,250 And if you multiply it, whatever, decimal gets returned by six. 50 00:03:18,370 --> 00:03:23,560 If, let's say you got the minimum value, zero times six is still zero. 51 00:03:23,560 --> 00:03:31,060 But if math random were to return the maximum possible value of 0.99999, then that max value would 52 00:03:31,060 --> 00:03:34,920 become less than 65.999. 53 00:03:34,930 --> 00:03:43,840 So double random number can be any decimal within this range and whatever number we get, we're going 54 00:03:43,840 --> 00:03:45,670 to have to add one to it. 55 00:03:45,670 --> 00:03:50,170 Which means if this happened to be the minimum number now, it would be one. 56 00:03:50,200 --> 00:03:54,400 If it happened to be the maximum number that this piece of code can return. 57 00:03:54,430 --> 00:03:58,320 Now it's less than 76.999. 58 00:03:58,540 --> 00:04:04,300 And now if you were to typecast whatever random number ended up being at line number 28. 59 00:04:08,040 --> 00:04:10,200 One would still be one. 60 00:04:11,510 --> 00:04:18,680 But if this happened to be the maximum possible value of 6.999, it would just become six. 61 00:04:18,680 --> 00:04:24,980 Which means at this point we're returning a random number between one and six, which is pretty much 62 00:04:24,980 --> 00:04:25,860 a dice roll. 63 00:04:25,880 --> 00:04:26,540 All right. 64 00:04:26,540 --> 00:04:29,510 That would have been your hardest task, in my opinion. 65 00:04:29,510 --> 00:04:31,940 But we've done this countless times before. 66 00:04:32,360 --> 00:04:38,960 So task number five is during each run, call a roll dice and store the value in dice or roll. 67 00:04:39,380 --> 00:04:42,350 So here we've got a variable dice roll. 68 00:04:45,830 --> 00:04:48,140 We'll set that equal to roll dice. 69 00:04:48,260 --> 00:04:49,190 Simple enough. 70 00:04:49,190 --> 00:04:50,720 Did we have to print it? 71 00:04:52,200 --> 00:04:53,540 Uh, we sure did. 72 00:04:53,550 --> 00:04:54,470 We have to print. 73 00:04:54,480 --> 00:04:56,310 You rolled a dice roll. 74 00:05:05,290 --> 00:05:09,610 All right, In the interest of time, I'll just use the run button. 75 00:05:16,090 --> 00:05:17,710 And the loop runs forever. 76 00:05:17,710 --> 00:05:20,440 Every single time it runs, it prompts me to press. 77 00:05:20,440 --> 00:05:21,040 Enter. 78 00:05:21,040 --> 00:05:24,850 When I press enter, we're rolling a dice and then we're printing it. 79 00:05:24,880 --> 00:05:27,010 Nothing too crazy. 80 00:05:27,910 --> 00:05:29,740 Moving on to task number six. 81 00:05:29,740 --> 00:05:33,650 During each run, if they roll a six print end of game and stop it. 82 00:05:33,670 --> 00:05:36,470 So we would have to break the loop, quote unquote. 83 00:05:36,490 --> 00:05:40,060 If they were live four, then they would just continue with the loop. 84 00:05:40,090 --> 00:05:46,120 Otherwise, if they're all anything else, update the points variable by one and get them to keep rolling. 85 00:05:46,240 --> 00:05:47,620 So if. 86 00:05:48,280 --> 00:05:51,640 The dice roll is equal to six. 87 00:05:51,820 --> 00:05:53,620 We have to print. 88 00:05:54,350 --> 00:05:56,660 And of a game. 89 00:05:57,200 --> 00:05:58,700 And break the loop. 90 00:06:01,100 --> 00:06:04,910 Otherwise if the dice roll is equal to four. 91 00:06:06,000 --> 00:06:07,860 Then nothing happens. 92 00:06:10,470 --> 00:06:11,480 Zero points. 93 00:06:11,490 --> 00:06:12,690 Keep rolling. 94 00:06:16,320 --> 00:06:17,010 Else. 95 00:06:17,010 --> 00:06:23,580 If they roll anything that isn't six or four, then we can update the points variable by one. 96 00:06:24,790 --> 00:06:27,970 There is no points variable, but there is a score variable. 97 00:06:27,970 --> 00:06:29,200 So we'll say score. 98 00:06:30,690 --> 00:06:31,830 Plus plus. 99 00:06:33,720 --> 00:06:35,370 And then we're going to print. 100 00:06:36,400 --> 00:06:37,420 One point. 101 00:06:37,420 --> 00:06:38,470 Keep rolling. 102 00:06:47,480 --> 00:06:47,900 All right. 103 00:06:47,900 --> 00:06:54,410 And then after the game ends, So after it reaches this if statement and the while loop breaks, we 104 00:06:54,410 --> 00:06:59,900 need to check if their score ended up being greater than or equal to three, in which case they win. 105 00:06:59,900 --> 00:07:01,880 Otherwise they lose. 106 00:07:02,480 --> 00:07:04,160 So here we say if. 107 00:07:05,360 --> 00:07:09,590 Their score ended up being greater than or equal to three. 108 00:07:10,190 --> 00:07:12,800 Then we'll just say you win. 109 00:07:14,680 --> 00:07:17,110 Otherwise you lose. 110 00:07:22,210 --> 00:07:23,140 And that's all. 111 00:07:23,140 --> 00:07:26,270 We created a dice rolling game using wild loops. 112 00:07:26,290 --> 00:07:28,930 Before I forget, let me close scanner. 113 00:07:30,770 --> 00:07:31,340 And. 114 00:07:31,340 --> 00:07:37,580 All right, I'm not going to put a breakpoint here because for some reason, if you put next line right 115 00:07:37,580 --> 00:07:41,600 after your while loop, it makes it really hard to visualize the runtime. 116 00:07:41,600 --> 00:07:49,850 So I'm just going to put a breakpoint over here and one at the beginning of the if elseif else chain 117 00:07:49,850 --> 00:07:50,960 and we should be good. 118 00:07:57,500 --> 00:08:01,940 So now we're probably at line 17, which means we have to enter something. 119 00:08:02,450 --> 00:08:06,410 Dice roll is going to equal whatever roll dice returns. 120 00:08:07,550 --> 00:08:12,410 In this case, we've got a dice roll of one that does not equal six. 121 00:08:12,410 --> 00:08:14,210 That does not equal four. 122 00:08:15,680 --> 00:08:20,570 So our score increases by one because we rolled a good dice. 123 00:08:21,930 --> 00:08:23,250 And we start again. 124 00:08:28,470 --> 00:08:29,900 This time I roll a four. 125 00:08:29,910 --> 00:08:33,740 So fortunately for me, four means the game doesn't end. 126 00:08:33,750 --> 00:08:37,169 I'm still good, but I don't get a point on this turn. 127 00:08:38,820 --> 00:08:39,720 All right. 128 00:08:42,400 --> 00:08:48,830 This time I wrote a three that is just perfect, which means I get a point and I get to keep rolling. 129 00:08:48,850 --> 00:08:50,040 I think I'm going to win. 130 00:08:50,050 --> 00:08:51,400 Let's find out. 131 00:08:54,390 --> 00:08:55,260 I roll it, too. 132 00:08:55,290 --> 00:08:56,940 That is amazing. 133 00:08:58,580 --> 00:09:02,780 And get another point, which means I will eventually win. 134 00:09:03,770 --> 00:09:05,770 Next line, press submit. 135 00:09:05,780 --> 00:09:06,950 Roll the dice. 136 00:09:07,040 --> 00:09:07,860 Five. 137 00:09:07,880 --> 00:09:09,410 It is my lucky day. 138 00:09:09,410 --> 00:09:11,150 I have got four points. 139 00:09:13,220 --> 00:09:14,420 Press submit. 140 00:09:14,630 --> 00:09:16,990 This time I roll of four no points. 141 00:09:17,000 --> 00:09:22,100 I'm going to fast forward the video until I eventually roll a six because you're probably getting really 142 00:09:22,100 --> 00:09:23,360 bored of me right now. 143 00:09:24,080 --> 00:09:24,710 Never mind. 144 00:09:24,710 --> 00:09:31,130 I guess I won't have to fast forward because I did just roll with six, which means end of Game Break 145 00:09:31,130 --> 00:09:33,710 is going to break the entire while loop. 146 00:09:35,550 --> 00:09:41,490 And now that the loop has been broken, I ended up with a score of four, which is greater than or equal 147 00:09:41,490 --> 00:09:44,010 to three, which means I win.