Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Extending the game
0:00 Remember I told you it would be a good idea to move the rules into its own data structure and because it was
0:06 a dictionary it's almost identical to JSON so why not that? The motivation was well then we could change it
0:11 or extend it or work with it from the outside so let's go ahead and just do that huh that'll be fun.
0:16 So over here we have the seven way rock paper scissors with rock water air paper sponge scissors and fire.
0:23 Though if we go down here you can actually find an easier way the way to interpret this diagram is air beats rock
0:29 because air is pointing at rock, but rock beats fire 'cause fire being pointed at by rock. Now I find that a little bit hard to follow, so let's
0:38 go down here and you can here even a like more colorful version rock pounds out fire and crushes scissors
0:45 and sponge, so we can use this actually this little bit here to help us figure out what we got to do for our file. So let's go over here to this one.
0:56 And first of all let's go and duplicate this real quick. And make that empty that empty that empty that empty.
1:04 We're going to have fire, we're going to need we have three we're going to have three more, four more. Sponge, air, and water.
1:17 Okay now let's see one two three four five six seven. So that's all of them and let's just do a couple real
1:24 quick together and then I'll just pause the video and I'll just do the rest myself. Okay so for rock, rock pounds out fire so that means
1:31 it defeats fire. Now be super careful here look what I typed how many things is in that list on line three?
1:41 Looks like two but in fact it's one 'cause there's a single string that just happens to have a comma in it so be real
1:47 careful that you put quotes there you go now there's two things so rock pounds out fires, crushes scissors and it beats sponge.
1:57 And it is defeated by something that beats let's go look through this let's see paper covers rock so yeah it's defeated by paper what else?
2:07 Air erodes rock so air is going to beat it. And water also erodes rock okay let's see that will have rock and let's have the sponge.
2:18 So sponge soaks paper, it uses air pockets apparently and it absorbs water of course it's going to be water
2:27 but what defeats sponge we have rock, fire burns it and scissors cut through it. Okay so let's just kind of run the game with this.
2:37 Now to verify that I'm only changing this file and nothing else is happening let's go and run this in a different way.
2:42 Let's run it over here separately so I'm going to say Python 3 and then just the file and look at that we loaded up
2:49 all those and now what are we going to play, we know that rock and air are pretty capable so let's try some air
2:56 this might not work because not really sure what's going to happen, they rolled scissors got to wait til the computer
3:03 throws something that we know about. Alright I guess to get anything meaningful to come out
3:07 I'm going to have to finish out this thing 'cause there's just too many that don't work with air.
3:11 But nonetheless, we're off to a good start you can see that it loaded those up. I'll go finish mapping this out and then we'll come back to the video.
3:19 Well here we have it the seven way rock paper scissor rules and going back it turns out it's easier to use this diagram
3:25 here actually to figure out what to type here just look okay this one points to those three, so put them in defeat.
3:31 But here they are we have it all written out and I think it's good I've checked it it seems okay but I haven't
3:38 exhaustively tested every case, we could do that. But let's just play around and see what happens here.
3:43 If you want to play it you can see it's delivered all the roles and let's put it over like this so you can see what's happening.
3:50 I'm going to play some sponge, they played scissors, scissors cuts sponge see what are we scissors they're defeating
3:59 sponge then the computer takes the round. As per usual on play six, the computer played I rolled
4:06 air the computer rolled paper and let's see somehow air eats paper, air is defeated by paper yeah so they beat me
4:12 with paper I'm going to play some fire. The computer also played fire. Do it again, they played paper I'm burnt up that paper
4:19 I just keep going, I played fire they did scissors I melted the scissors all right here we go I'm going to try to take it down with some sponge.
4:26 I did sponge they did fire ah man they burned it up. As per usual I lost rock paper scissors, even the seven way one look how incredibly cool this is.
4:35 All we did was take this same basic idea and extend the rules over here we literally didn't touch the source
4:40 code to make it understand that there are now more things to throw or that there are these rules between them.
4:47 So hopefully this tells you or shows you how powerful these data structures are and this general way
4:53 of programming once you get the right model in place. If you want to go and add 25 way rock paper scissors honestly it wouldn't be that hard.