Writing our first smart contract

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 let's start by creating a new file. I am going to name it game, but you can name it anything you want. The name of the file and the name of the contract do not have to match up. The only thing that matches is that you have a contract name and that's all. So the first thing that we have to do is define the solidarity version that we 're going to be writing the code in. So as I've said before, we are going to be using 060 and that's fine. So we are now going to create a contract called gain and this is the contract where we are going to play the rock paper scissors game. So before I start this, let's just write out a few requirements and we can just create that over here. So basically what we want to be able to achieve with this game is that only in fallacy Bob can play and in other than Bob can play rock paper scissors. And they should be able to display with their addresses and once they both have played the move, any one of them or anyone can call an evaluate function which will then decide who was the winner. So at this point, the first version of the game, we don't care about the actual privacy. So the only thing that we actually want in this game is that the Alison Bob should be able to play the game and that's all that matters at this point. So if you want to have a go, in my case, I am going to have two functions. So one function is going to be a play. So play function is where a player can define what they are playing. So for this case, just to keep it simple, this game is only for two players, Alice and Bob and in play, they just defined what move they are playing. So rock paper or scissors. And we're going to have another function which is going to be called evaluate. And in evaluate, you essentially can say, given the two players who played this , so they are identified by their Ethereum address, evaluate who won. So it will return the address of the player who won. So if you want to have a go at it, you can pause the video now and have a go at it. Okay. So let's start by just writing the functional interfaces just so we can see what is coming on. So the first function we want is play. So play is a function and it's going to take in a choice. So in our case, I'm going to make the choice human just to keep it simple. And since we know the number of moves are very low, there's only three moves. Rock paper or scissors, we can make it a unit H to save some storage. And we're just going to call it choice. And this function is going to be external because we are not going to call it internally. And you can write the function. Okay. So this is going to be a play function. Now we're also going to have an evaluate function. So in the evaluate function, we are going to take into addresses. So the address of Alice and the address of Bob. And again, this function is also going to be external. And this function is also since it's only doing like things that are not changing the state. It's only evaluating. We can make it real, which means that it's not changing the state of the contract. And after that, we can just make that so it's going to return an address. So it's going to return an address, which is going to be something. So again, now that you have the interfaces of both of the functions, have a go at implementing this contract and see where you get to. So pause the video here and try to solve it. Okay. So just to give you a few more hints right before we dive into the solution in the next video, you have seen that I am using you in eight here. This is basically how I have chosen to store the choice and in the next iterations, we will change that to something else. But just to get it started, this is very simple to do because numbers are very easy to compare. And you can think of like what kind of storage you can use. Anything is fine. For now, we just want to be able to play that game. And also you can see that I am mapping a lesson verb as the addresses. And as an additional hint, you can use something called message to sender, which will tell you who called this contract. And you can use that in the play function. So in here, you can use message to sender to see what move they're playing. Of course, you can do validation and you can do a few other things. But have a go at it. And if you can't do it in the next video, I will solve it with you line by line . [ Silence ]