Defining the Game Mechanics

Course Introduction
Overview
Difficulty
Intermediate
Duration
2h 36m
Students
13
Ratings
5/5
starstarstarstarstar
Description

In this course, we begin building a game in Solidity, and more specifically, we begin to look at defining the mechanics and the components of the game.

Transcript

Alright, this is the game that we are developing in this section, and we're going to have a very very simple game, it's a round-based game, it's a multiplayer round based game and we're going to do  a three in a row or tic-tac-toe game. And if you don't know what is tic-tac-toe, then let me quickly explain it to you. I think for almost everyone is pretty clear with this tic-tac-toe. So, the idea is that you start with an empty board, like this one. Player one sets his stone on the board, let's call them player X. And he's maybe playing the left top corner; And then player two, let's call him player O, is setting stone and maybe he's setting the stone here. And then player one again, setting the X here. Player two is setting his O here, and player X or player one is setting his X here, or stone here. And as soon as somebody has three in a row; horizontal, vertical or diagonal then he won the game. So in our case, player one won this game. But that's not all what features will our game have. Since it's running on the blockchain, we can do a lot more than just having this simple board game. We can have pay for play functionality, where maybe one game costs for each player 0.1 Ether, 1 Ether or 0.1 Ether. So that,  I  leave it up to you and maybe the winner takes it all. And if you feel extremely adventurous, then maybe you can even put something like a commission for the platform in there. But essentially, for our game that we are going to develop, it's going to be 0.1 Ether for each game. So, player one has to pay 0.1 Ether; player two has to pay 0.1 Ether; then they can play against each other and the winner takes it all. And if there's a draw, then 0.1 Ether go back to the players, which very much reminds me of this old arcade machines where you have to put in some coins here. And I got this picture from an arcade machine which you can buy on Amazon. The link is below. What else do we have? If you've ever played these arcade machines, then at the beginning, you have this high scores list. And usually underneath this high score list, there is  a insert X coins to play the game. And they keep showing. So, we will have a very similar functionality. We will have a high score list and we will show their addresses and how many times they won before we start the game, and then there's a button underneath that you can start the game. Our app, or what's called our DApp;  or Decentralized Application, will be completely decentralized. So, our database will run on the blockchain. All the data will be  saved on the blockchain. So, that will definitely be decentralized. And at the end, you will have the option to also decentralize the actual HTML JavaScript part if you want to put it on IPFS. And that's something that we're going to tackle in one of the next sections. In these sections, we are really talking about the smart contracts. So our app will be, at the end; they will have an HTML5 front-end. So, there will be one URL where you can reach the game. It's like an app, like Facebook is an app. And you will see, you will start by seeing this high score list. You can create a game and then wait for the opponent, and your opponent can join  a game. Then you play the game and then there is a payout. I've also put some wire frames together, so to make it a little bit more clear how I envisioned that or how this looks like. So, when you first go to this website, then you would see something like, a title; that's a Three In a Row game. And there's the high score and there's  a high score list. And underneath is a button, and the button will create a new game. And once you created the game, then you will see that in the next screen, and underneath the button there's an input field where you can put in an address and you can join the game. So, obviously you have two players. The one creates the game and then the other one joins the game. So, as soon as you create a game, you wait for another player because you can't play alone. We're not programming any bots here. We are really playing multiplayer with two different people. And you have some instructions here, how another player can join this game. And underneath, you have the actual game address which the player has to put into this input field. The second layer,  there is input field to actually start playing. Alright. And then you play. Each and every one player, one after the other, gets his turn until somebody wins. And once you win, you get the message that you won, congratulations or you lost. Then there's just the lost message, and underneath you get instructions; how to get your money out from this smart contract. And that will be very interesting where we're going to talk about push versus pull methods for withdrawing money, okay? How is the game workflow? Essentially, there are these five steps which we are going to implement, that is, starting; you have to pay in order to create a new game. So, we are going to have a constructor which is payable and so on. We're going to talk about events which we haven't talked yet, but we're going to talk about these events in this section, and in the next section, we are going to deep dive into events where we are talking a little bit more in depth. What they are doing and how you can utilize them. And essentially in our case, an event is fired when the game is started and tells you which address is the game created. Then there is a joint functionality, player two can join the game. Obviously also payable, because you have to transfer 0.1 Ether in order to join this game. And then there's the gameplay; player one, player two, player one, player two and so on, until there is a winner or a loser and the winner takes it all. And have envisioned for, that free smart contracts which are working together, their one is a game manager which is managing starting and little bit of the high score and it's basically a global entry point to this game management system. Then there is a high score manager and that's entirely only managing high score entries because it's not so easy to have  a gas, less gas consuming way of having an ordered list in order to have this top 10 high score list. And then obviously, there is a three in a row, the actual game that you want to play. And in the next steps, we are defining the interfaces, we are going to implement the game functions. We are talking about events because we haven't covered them yet. And after that, after this section, once we have done our smart contracts, we are doing a deep dive into the Ethereum network into the EVM; the Ethereum virtual machine, and how you can utilize the Ethereum virtual machine. And now it's demo time. I'm going to show you how the smart contracts work if they are finished and then we're going to start from scratch and I'm going to guide you through each and every step and tell you exactly how you can achieve that. I'm here in remix now and I'm going to show you how this game or how this whole free smart contracts are envisioned to work together. And then after this lecture, we are going to start from scratch and I'll show you step by step, really one step at a time, how this game builds up. But for now it's just demo time. We're going to deploy the smart contracts and then we're going to play a game. And I'm in remix and let me show you the free files before we're actually starting. So I have the game manager, there is a high score manager and there is a three in a row game and the game manager is importing both the three in a row game and the high score manager. And as soon as the game manager is deployed, it will deploy its own high score manager in order to manage the high scores for the one games, and as soon as you win a game, you are going to add one win for your address or you can start a new game and that starts a new instance of the three in a row game. Okay, now I'm going to deploy the game manager and interesting enough, if we deploy it with three million gas, it fails because it's going to run out of gas. We have to give it more gas. So, it's quite a complex smart contract. And you can see later when you opening up the transaction, then you see that use like 3.6 some million gas in order to deploy the smart contract. But that's just a side note. Once the smart contract is deployed over here on the right side, you see the instance we actually have just free options here, and this becomes a little bit tricky if you're going to work with the HTML JavaScript part where we have to emit events and then listen to these events. Let me explain this to you. I want to start a new game, I have to transfer 0.1 ether obviously because it costs something and then I start game. And what happens next is that a new transaction is started against this start new game function and when the game is created, this event; GameCreated event is emitted and that one tells us the game is running under this address here. Okay, I'm going to copy this address now and I'm going to simulate two different players. I'm going to say the three in  a row game is already deployed at this address, which is true, because we just deployed it with the start new game functionality. And now our player one is the player that is this address here. So, we have our first player is our first address in our address list. And our player two is empty. So, we have to join this game in order to start playing. And because we want to play with two different people, I'm going to select the second address, is our second person, and I have to give it 0.1 Ether. And then we hit 'Join the Game'  and that transaction perfectly went through. And now our second player is the second player in our second address in our account list. Now, how do we know which player is the next player; which player has to wait and which player has to move, whose turn is it? Our transaction to join a game also triggered an event and next player event. And here we see that the player whose turn it is, is the 0xCA35 address. So, we have to go back, and we set a stone to 0,0 and then we get the game board. And now we have a board with three times freeze and array three times three. And that one tells us our stone was set to 00. So, from player one. So, our address 3,3 has an entry in 0,0. Now if we set  a stone, then another event is emitted with the next player which is 0X414. It's not really surprising because we just have two players and they play with each other. If we try to set the stone to the same field again, we get an exception which tells us we cannot set the stone again to the same field. We have to set it to a different field. We set the stone, we got the board and so on and so forth until we win. So, let me quickly run through this. We set the stone in the center, we set the stone at on the right side. Let's get the board and you can already see how this turns out. And now our player one will win because he's going to set the stone to the right bottom corner, and we have a winner which is this address. And this address in our case, automatically got the 0.1 Ether. And I'm going to tell you later, a little bit more about push and pull methods, how to withdraw Ether from a smart contract and why it's actually dangerous to push it directly out there. But that will be something for the next sections. Now I really want to just start over, start fresh with an empty, kind of empty smart contract. In the next lecture, we are going to talk about the very, very basic structure of our smart contract here. So stay tuned.

About the Author
Students
43
Courses
7

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.