Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Making the game extensible
0:00 Remember how we had this dictionary that we were creating for choosing which roll was played and then what beats it
0:07 and what is beaten by it, and so on? Like here if we play rock we know that rock defeats scissors but is defeated by or loses to paper?
0:15 Well, look at the top here. If we change this where we store this from a source file into a regular file, just a data file
0:22 like here we called it rules.json we can load this at startup and it's super easy for Python to understand this JSON format.
0:30 The first thing that we're going to do is we're going to use a JSON file that looks very much like the stuff that we actually had built into Python.
0:38 We're going to use this to allow the rules of our game to be extended or controlled from the outside without even touching the program
0:46 or the source code or anything like that. Now, that might seem like overkill like, Well we already have it in the game file.
0:52 Why not just leave it there? because, there's more variations to Rock, Paper, Scissors and some of them are totally awesome. This place at umop.com
1:00 they've got a couple variations that I think are really fun. So here you can see at the top we have rock at the bottom we have paper
1:06 and on the left we have scissors but now we're adding three no four more things. Fire, water, air, and sponge!
1:12 And you can click that link in the bottom to learn more about it. So what you're going to see is that we could actually create this version
1:18 of Rock, Paper, Scissors without touching our software by just changing that rules file. And that's going to be really cool
1:25 because that means that people that get our game they could actually change the rules just by editing a simple text file
1:30 and not even knowing programming. Sure, we could code this into there, right? We could actually put this one and make it a choice but you know what?
1:37 There's also 25 way Rock, Paper, Scissors and I think there's even like 100 way Rock, Paper, Scissors. There's some really, really insane stuff
1:44 so this one is really fun. You've got the snake, you've got the ax you've got the devil, you've got the wolf the cockroach, the alien, so.
1:52 This one's really, really fun. And you can check it out at this URL here as well. We're not going to do this because it would be a lot of work
1:59 but I do want to make a quick comment about the data structure stuff that we did. Remember our if statements we had three if statements.
2:05 If it's rock, then if the other player played rock do something if they played paper, do something and so on. So we had nine nested if statements.
2:13 Because there were three times three things and ways to play the game so that it ended up with nine but it seemed kind of bad but not terribly bad.
2:21 If we do that with Rock, Paper, Scissors for 25 way. RPS-25 we're going to end up with 625 if statements in a entirely non-maintainable way.
2:32 I just want to point that out because this thing here would not be very hard to create an add to our system that we're going to create here
2:40 either by putting it in a source code or this better version with a file we're going to use. So I think that really drives home the power
2:46 of those data structures to do a much more general solution than to just hard coat it. Okay, so our first challenge is to move
2:53 that data structure out of our program and into a file so that we can then extend it and add some of these game features to it.