#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the game (client-side)
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
You saw us write the game, interaction of the game loop previously. We've consumed a bunch of API's with Uplink already.
0:08
So let me just drop in some code here that is going to be basically, the same thing but using the service, just for the sake of time.
0:19
So what do we got? We're going to print out the top scores, and we're going to call top_scores and then we'll loop
0:23
over them, and remember everything that comes back is a dictionary so we've got to get the values and then this is a nested dictionary,
0:30
so we've got to get the name from the player that we got, and so on. So we're going to print out the top scores,
0:36
we're going to create a new game that's going to return that dictionary which we're going to get the game id.
0:40
We could do some work to make this much easier to consume, but just keep it focused on more the server side. Kind of just roll with what we got.
0:47
Give us all the rolls, get us the player list. Suppose we want to be the player Charles. And then we're just going to say while the game is not over,
0:55
we're just going to go through and play a round, pass the game id, the user being Charles, and the roll which we're randomly
1:01
choosing from the various options we got from the server. Right up there. Okay, so we're just going to go run and do that, and when
1:09
it's over we're going to get the game status, and print the outcome which the game status has the winner. Oh, okay, so let's run this.
1:17
By the way, it doesn't ask us what we want to do. It just randomly chooses rolls for Charles, and makes him play those.
1:23
But it could just as well, this could be an input or some other type of UI. Oh, Charles is not found. Yeah, I guess we got to create Charles.
1:35
Let's go ahead and just create him this way, it's probably the easiest way. All right, try again. Boom, look at that.
1:49
Top scores, Michael scored 1, Charles scored 0, so Charles threw rock, computer threw scissors, that resulted in a win for the player which is Charles.
1:58
We got a tree and a sponge, win. Lightning does not beat the devil, water does not beat humans,
2:04
but finally the end, humans beat scissors and that takes it. The game is over and Charles is the winner. Let's run it again.
2:12
At the top you see now Charles and Michael have 1. One score each in the top score, in the leaderboard there.
2:18
Come down here, game is over, the computer won. Let's try again. Leaderboard should now be 1, 1, 1. How come Charles winner, is the winner.
2:27
So if we run it this time, Charles now has the global high score. Pretty amazing. It might feel like we're kind of playing the same game,
2:36
but this is totally different, right? We could put this on a phone app, we could distribute this around the world.
2:41
And if we were hosting that website somewhere like Heroku, or Digital Ocean, or Linode, or something like that.
2:47
Put it out there, and this would be shared for the world. Pretty cool. Hopefully this has inspired you to see what you could build.
2:56
I know it's not the super simplest demo, but it's pretty realistic, and it does cover a lot of interesting use cases, and it really opens another world
3:05
for you guys to build cool applications that are data driven remotely.