Implementing Rock Paper Scissors

Implement a simple rock paper scissors contract with no privacy

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the newline's Introduction to Privacy on Ethereum course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to newline's Introduction to Privacy on Ethereum, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course newline's Introduction to Privacy on Ethereum

Okay, so first of all what we need to do is to be able to know what moves are. So we need to represent them in one way or another. So the thing that I'm going to go with is you're in eight for now. So let's just say we're going to say that rock is zero and we're going to replicate that and say another unit eight and rock. So all paper comes next so we can do paper equals one and we can do scissors. Okay, let's just keep it all capitals because it's going to be consistent. So okay, so now we have defined all papers is is we can now know what moves actually playable. So what we can do now is we can write a require function that choice must be equal to one of these things. So choice either equals rock or it equals paper or it equals as you guessed it. So now we've basically said that choice must be equal to one of the three things. So I see what it's complaining about. It is saying that okay, well, I missed the equal. So that's okay. So this is the benefit of auto-combiol. It's combining always. So as soon as you make a mistake, it can tell you that you're made a mistake and you can fix it immediately. So now that we have already checked that, you know, the player move is valid. We can now also check that they haven't already played a move. So what we want to prevent is that they haven't played a move. Well, but to do that, we need to actually know and have a storage mechanism for it. So the first storage we have in SolDITY is a mapping. So we're going to be using a mapping and what we're going to be doing is we're going to do a mapping between an address and a choice and the choice in our case is a year and eight. Of course, you can represent it in many different ways and there's a lot of valid ways to do it. This is just one of them at Conrad. Of course, you can use structs, you can use something else and feel free to do that. There is no obligation to use this and you can also send me all of your solutions. So we can create a repo for that. So everyone can learn from it. And of course, I'm just going to make it public and what this does is that it will create a function for us so we can access, given a key, we can access the value. And for this case, we are just going to be calling it choices. So this is the simple thing. Now the other thing we want to check is that the player hasn't already made a choice. So they're not like after they've seen someone else's choice. They're not reverting to anything. So we don't want people to do that. Therefore, we are going to say, okay, choices and the choices of the message sender, which is what I was talking about before, should be equal to address zero. Now the reason for address zero is because address zero is the default of, oh, sorry, my bad, it shouldn't be address zero, it should be just zero because zero is the default form anyone. So you can do that. And this basically means that they haven't already played a move. For us to keep it a little bit more simpler, what we can do is we can now change this to match that. And to basically say that it's 1, 2, and 3, because we know that zero is where they haven't played a move. And technically we shouldn't be comparing a non-played move to a played move. So just to make sure of that, we are now allowing this. And because we already, we are just comparing variables, it's completely fine to just say give variables and not have to worry about the other statements. So I mean, of course, we can represent the zero as an unplayed move as well, or a player hasn't played. So once we have that, we can now do choices and now we can just set what they are playing. So we've done all the verification that we have to. And now we can say, okay, well, they're playing a while it moves, we know that we know that they haven't played a move before, so we can just do this. And this is all that there is to it for the play function. So now if you want to have a go at the evaluate function and see if you can get it to work, go for it and pause the video here. And if you can't, then I'll go through the solution with you. Okay. So we can now do the evaluate function. And the first thing that we can do in the evaluate function is basically see that if they have played the same move or not. So for this, this is actually very simple. What we can do is we can do choices of Alice. So if the choices of Alice, Alice equals the choices of Bob, then we know that 's actually a draw. So if it's a draw, then we are just going to say, then address zero. So we're going to say the address zero is if it's a draw. If it's a draw, we don't have to do any computation at all. And it's perfectly fine. But if it's not a draw, then we have to check. And for this, I'm not going to use any smart ways. I'm just going to write if statements. You can do things better, but this is just to keep it simple. And just, you know, because that's not the aim of this. So we are just going to be writing simple if statements to check who won. So we are going to say if choices, Alice. Okay. So if choices, Alice equals rock. And so again, we're going to do an and and we're going to do choices. Bob equals people. Then we know that Bob has won because they put beat rock. So they're just going to say return Bob. All these are just the addresses that we are getting and we're just comparing. So given this, given this, this is going to be whatever choice they made. And if their choice is rock, if Alice played rock and Bob played paper, then Bob won. And then we can do a lot of alfives. So again, you don't have to watch me do that. You can try to write it yourself and see how you go. And only if you get stuff, you can come and see the actual result. So I'm going to finish up writing all of these and we'll see you in the next lesson and talk about the solution a bit more. [ Pause ]