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
  • [00:00 - 00:08] 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.

    [00:09 - 00:26] 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.

    [00:27 - 00:46] 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.

    [00:47 - 01:02] 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.

    [01:03 - 01:31] 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.

    [01:32 - 01:39] So I see what it's complaining about. It is saying that okay, well, I missed the equal.

    [01:40 - 01:45] So that's okay. So this is the benefit of auto-combiol.

    [01:46 - 01:53] 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.

    [01:54 - 02:05] 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.

    [02:06 - 02:15] 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.

    [02:16 - 02:37] 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.

    [02:38 - 02:45] 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.

    [02:46 - 02:57] 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.

    [02:58 - 03:04] So we can create a repo for that. So everyone can learn from it.

    [03:05 - 03:19] 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.

    [03:20 - 03:30] So this is the simple thing. Now the other thing we want to check is that the player hasn't already made a choice.

    [03:31 - 03:38] So they're not like after they've seen someone else's choice. They're not reverting to anything.

    [03:39 - 03:57] 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.

    [03:58 - 04:20] 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.

    [04:21 - 04:32] 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.

    [04:33 - 04:48] 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.

    [04:49 - 05:04] 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.

    [05:05 - 05:18] 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.

    [05:19 - 05:29] 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.

    [05:30 - 05:43] 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.

    [05:44 - 05:51] And if you can't, then I'll go through the solution with you. Okay.

    [05:52 - 06:02] 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.

    [06:03 - 06:10] So for this, this is actually very simple. What we can do is we can do choices of Alice.

    [06:11 - 06:31] 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.

    [06:32 - 06:39] 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.

    [06:40 - 06:43] And it's perfectly fine. But if it's not a draw, then we have to check.

    [06:44 - 06:48] And for this, I'm not going to use any smart ways. I'm just going to write if statements.

    [06:49 - 06:57] 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.

    [06:58 - 07:08] So we are just going to be writing simple if statements to check who won. So we are going to say if choices, Alice.

    [07:09 - 07:18] Okay. So if choices, Alice equals rock.

    [07:19 - 07:31] And so again, we're going to do an and and we're going to do choices. Bob equals people.

    [07:32 - 07:41] Then we know that Bob has won because they put beat rock. So they're just going to say return Bob.

    [07:42 - 07:53] 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.

    [07:54 - 08:04] 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.

    [08:05 - 08:11] So again, you don't have to watch me do that. You can try to write it yourself and see how you go.

    [08:12 - 08:26] 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.

    [08:27 - 08:41] [ Pause ]