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