1 00:00:00,025 --> 00:00:07,639 [SOUND] You've learned a ton about object oriented programming in Python. 2 00:00:07,639 --> 00:00:09,010 You've learned to make your own classes. 3 00:00:09,010 --> 00:00:09,885 Either from the ground up. 4 00:00:09,885 --> 00:00:12,410 Or by building on top of Python's built in classes. 5 00:00:12,410 --> 00:00:14,347 You've also taken advantage of magic methods. 6 00:00:14,347 --> 00:00:14,960 Class methods. 7 00:00:14,960 --> 00:00:15,596 And properties. 8 00:00:15,596 --> 00:00:17,422 To make your classes smarter and cleaner. 9 00:00:17,422 --> 00:00:19,150 You're doing amazing. 10 00:00:19,150 --> 00:00:21,980 We haven't built anything larger and more complex together, though. 11 00:00:21,980 --> 00:00:22,535 You know me. 12 00:00:22,535 --> 00:00:24,830 I like to build games with you as a learning tool. 13 00:00:24,830 --> 00:00:26,295 When I was first planning this course out. 14 00:00:26,295 --> 00:00:28,470 I wanted to build a terminal version of Yahtzee. 15 00:00:28,470 --> 00:00:30,870 With die rolling, and re-rolling, hand scoring. 16 00:00:30,870 --> 00:00:32,390 And even human and bot players. 17 00:00:34,270 --> 00:00:36,220 Yeah, that wasn't as simple as I thought it would be. 18 00:00:36,220 --> 00:00:37,399 When I did complete it. 19 00:00:37,399 --> 00:00:39,940 It was just way too much to teach in a course. 20 00:00:39,940 --> 00:00:40,975 So let's scale it back. 21 00:00:40,975 --> 00:00:43,072 And build something that's less for one specific game. 22 00:00:43,072 --> 00:00:45,330 And maybe a bit more universally useful. 23 00:00:45,330 --> 00:00:46,329 Over the next few videos. 24 00:00:46,329 --> 00:00:47,288 We'll build a small utility. 25 00:00:47,288 --> 00:00:50,770 To roll a specific number of dice with a specific number of sides. 26 00:00:50,770 --> 00:00:52,500 You could roll two 20-sided dice or 27 00:00:52,500 --> 00:00:55,730 five six-sided dice or whatever your game requires. 28 00:00:55,730 --> 00:00:57,693 While this isn't quite as exciting as a full game. 29 00:00:57,693 --> 00:01:00,365 It'll give us a lot more practice with building smart classes. 30 00:01:00,365 --> 00:01:02,855 And it'll be a utility we can use for building other games. 31 00:01:02,855 --> 00:01:04,800 As we continue working with Python. 32 00:01:04,800 --> 00:01:05,920 Get a drink or a snack. 33 00:01:05,920 --> 00:01:07,130 Take a nap if you need it. 34 00:01:07,130 --> 00:01:07,720 And then let's start. 35 00:01:07,720 --> 00:01:10,120 By defining exactly what our code dice should do. 36 00:01:11,480 --> 00:01:15,068 All right, we want a die that has a certain number of sides. 37 00:01:15,068 --> 00:01:18,820 And then a random value between 1 and the number of sides. 38 00:01:18,820 --> 00:01:20,130 And we could base this on int. 39 00:01:20,130 --> 00:01:22,910 But that actually raises a fair number of issues. 40 00:01:22,910 --> 00:01:26,410 It's easier and arguably cleaner to just make our own class. 41 00:01:26,410 --> 00:01:28,074 We can control the creation of the class. 42 00:01:28,074 --> 00:01:32,430 And we can make it a little easier to subclass for other die types. 43 00:01:32,430 --> 00:01:34,981 Now I've created a directory named yatzy. 44 00:01:34,981 --> 00:01:37,050 And I've created a file named dice.py. 45 00:01:37,050 --> 00:01:38,252 You wanna go ahead and do those. 46 00:01:38,252 --> 00:01:39,450 If they're not in your workspace. 47 00:01:40,620 --> 00:01:43,576 So then let's import random. 48 00:01:43,576 --> 00:01:45,790 And lets create our die class. 49 00:01:45,790 --> 00:01:49,561 So find our init [INAUDIBLE] self. 50 00:01:49,561 --> 00:01:51,570 I think we want two arguments. 51 00:01:51,570 --> 00:01:53,573 I think we want a sides. 52 00:01:53,573 --> 00:01:55,772 And we'll default that to two. 53 00:01:55,772 --> 00:01:59,127 And let's let them set a value. 54 00:01:59,127 --> 00:02:02,696 Just in case they want the die to be four or whatever. 55 00:02:02,696 --> 00:02:03,950 For testing purposes. 56 00:02:05,850 --> 00:02:08,910 So, okay, so we take a number of sides and we take a value. 57 00:02:08,910 --> 00:02:14,561 And we want to make sure that sides >= 2. 58 00:02:14,561 --> 00:02:17,860 Otherwise we should raise ValueError(. 59 00:02:17,860 --> 00:02:20,140 That says, must have at least 2 sides. 60 00:02:20,140 --> 00:02:24,820 If you can show me a one sided die, I would love to see it. 61 00:02:24,820 --> 00:02:28,450 And let's make sure that sides is an int. 62 00:02:28,450 --> 00:02:33,850 So if not is instance(sides, int): We're going to raise 63 00:02:33,850 --> 00:02:40,030 another value error that says sides must be a whole number. 64 00:02:40,030 --> 00:02:43,736 Now, arguably, that might ought to be a type error instead of a value error,. 65 00:02:43,736 --> 00:02:47,340 But I figured they gave me the wrong value, so it's a value error. 66 00:02:48,570 --> 00:02:52,407 So then let's say that self.value = value. 67 00:02:52,407 --> 00:02:59,510 That was passed in, or random.randint (1, sides. 68 00:02:59,510 --> 00:03:01,030 So something between 1 and sides. 69 00:03:01,030 --> 00:03:02,640 We can't have a number below 1. 70 00:03:02,640 --> 00:03:06,290 And it can't be above the number of sides that there are. 71 00:03:06,290 --> 00:03:10,170 Now, we're not taking into account if you had a strange die that had 72 00:03:10,170 --> 00:03:11,760 only even numbers or something like that. 73 00:03:12,820 --> 00:03:14,782 And we might want to have a check. 74 00:03:14,782 --> 00:03:16,857 To make sure that value is also an int. 75 00:03:16,857 --> 00:03:18,670 But I'll leave that up to you. 76 00:03:18,670 --> 00:03:23,560 Okay, we have enough here to try out our really simple die. 77 00:03:23,560 --> 00:03:25,793 So let's come down here. 78 00:03:25,793 --> 00:03:29,277 And Python from dice import. 79 00:03:29,277 --> 00:03:32,361 I need to exit. 80 00:03:32,361 --> 00:03:33,385 Let me get into Yatzy. 81 00:03:34,715 --> 00:03:39,215 Python from dice import die. 82 00:03:40,495 --> 00:03:46,764 And then let's say that d is equal to die, d.value. 83 00:03:46,764 --> 00:03:48,119 There's 2. 84 00:03:48,119 --> 00:03:51,432 And let's say d6 is equal to die. 85 00:03:51,432 --> 00:03:54,680 And sides is equals to 6 and the 6 sides. 86 00:03:54,680 --> 00:03:59,030 And then we do d6.value we get 2 there as well. 87 00:03:59,030 --> 00:04:01,690 All right, so it rolled a random number for us twice. 88 00:04:01,690 --> 00:04:03,090 So cool. 89 00:04:03,090 --> 00:04:09,460 Now, let's go back and let's make a die class that extends our die. 90 00:04:09,460 --> 00:04:11,652 But is always a six sided die. 91 00:04:11,652 --> 00:04:12,435 Right? Because that's 92 00:04:12,435 --> 00:04:13,102 a pretty common need, right? 93 00:04:13,102 --> 00:04:16,299 There are lots of games out there that need six sided dice. 94 00:04:16,299 --> 00:04:20,457 The convention is to call a die D. 95 00:04:20,457 --> 00:04:22,230 And then the number of sides that it has. 96 00:04:22,230 --> 00:04:24,504 At least in role playing circles. 97 00:04:24,504 --> 00:04:26,199 So we're gonna call this a d6. 98 00:04:26,199 --> 00:04:28,842 Ad this is going to extend die. 99 00:04:28,842 --> 00:04:30,240 But we're gonna override init. 100 00:04:31,730 --> 00:04:33,730 We're still gonna take value. 101 00:04:33,730 --> 00:04:34,780 We're not gonna take sides. 102 00:04:34,780 --> 00:04:36,810 Because we don't care how many sides they want, right? 103 00:04:36,810 --> 00:04:38,230 The sides on this is always gonna be six. 104 00:04:38,230 --> 00:04:42,090 So we're gonna call super and then the init method. 105 00:04:42,090 --> 00:04:45,830 We're gonna say size equals six, and value equals value. 106 00:04:45,830 --> 00:04:48,080 We'll just pass value on through. 107 00:04:48,080 --> 00:04:50,470 And that's it for our class. 108 00:04:50,470 --> 00:04:53,008 Since we set up our Die class to take a number of sides. 109 00:04:53,008 --> 00:04:55,160 We can just hardcode that in our D6 class. 110 00:04:56,210 --> 00:04:57,463 So, let's test this one out. 111 00:05:03,130 --> 00:05:07,476 So we're gonna import dice and then we'll say, d6 = dice.D6(). 112 00:05:08,750 --> 00:05:12,040 And then if we do d6.value we get a 3. 113 00:05:12,040 --> 00:05:12,890 Nice. 114 00:05:12,890 --> 00:05:13,811 Now we could go further. 115 00:05:13,811 --> 00:05:16,170 And turn this into a 12 sided or a 20 sided. 116 00:05:16,170 --> 00:05:21,990 Or whatever number of sides the dice in our game require. 117 00:05:21,990 --> 00:05:23,590 So feel free to do that if you want to.