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