Learning EOSIO with Lena: Tic Tac Toe

Let’s take a quick look at the EOSIO Tutorial for the Tic Tac Toe Game Contract, which describes the whole process of contract building for the game “Tic-Tac-Toe” in great detail. First, it introduces the goal and the rules of the game. For this game, we will use a standard 3×3 tic-tac-toe board. Players are divided into two roles: host and challenger. And we will use 1 to denote movement by the host, 2 to denote movement by the challenger, and 0 to denote empty cell. The equivalent of representing the board with a one-dimensional array. There is an example in the tutorial that can be easily understood.

After understanding the rules of tic-tac-toe, let’s imagine that you are in the middle of one game. So what functions will you be needing? Without a doubt,  you will need functions that allow you to create a new game, place your pieces, check out the result, etc. Then we can summarize the basic actions of the game.  Actions include: create (create a new game), restart (restart an existing game, host or challenger is allowed to do this), close (close an existing game, only the host is allowed to do this), move(make a movement). These actions clarify the framework of the game and will help users to interact with this contract. The process of coding is the process of implementing these actions.

In the next step, we are getting started to build the contract on EOSIO. For a beginner without any experience of programming, it’s probably difficult to understand.

What is cleos? Where do I write this command? For these questions, I’ll answer briefly here.

To begin the building of tic-tac-toe game, you first need to set up the EOS development environment.

The EOSIO software supports the following operating systems:

Amazon Linux 2
Centos 7
Ubuntu 16.04
Ubuntu 18.04
MacOS 10.14 (Mojave) and higher

If you are a Windows user, unfortunately, they do not provide PowerShell ports and instructions at this time. While it’s not convenient to check it out by yourself, it’s still possible to gain some knowledge about EOSIO-based contract building with this tutorial.

For development tools, any tool that supports C++ will do. If you’re unsure what to use, there are some options for you to choose from. Potential Editors and IDEs: Sublime TextAtom EditorCLionEclipseVisual Studio Code

Once you have built the EOS development environment, you will be exposed to the following three applications.

Nodeos, short for “node eos“, is the core daemon of EOS, through which a node can be run. After launching Nodeos, it will produce blocks.
Cleos, short for “client eos“, is a command-line program. If you think of nodeos as the server side of the EOS system, then cleos is the client that accesses the EOS blockchain.
Keosd, which is launched on the back end. Its purpose is primarily to manage wallets and create private keys.

We can see the subcommands of Cleos by simply entering “cleos” in the terminal.

I will not go into detail here further. After understanding these basics, it becomes much clearer, once we look back at the Tic-Tac-Toe tutorial.

At the “Contract account” step, we need to push the game contract to an account called tic.tac.toe. Just execute the commands given in this step in the terminal. Once this step is completed, it’s time to start the building of game.

The programming language we will be using is C++. The tutorial clearly explains the two files we need to create and what they are for. The tutorial gives a very detailed explanation of both the tic.tac.toe.hpp file and the tic.tac.toe.cpp file. Code notes are given for almost every key section. The overall code framework was broken down into several parts for analysis.

Let’s take the “move” action handler as an example. Every note after “//” explains what the following part of the code does. And lines like “the next move should be made by”, “game doesn’t exist”,etc. can be changed. If you are a Chinese, it is totally cool to change these parts to Chinese. And these sentences will be displayed to remind users when they meet corresponding situations.

The tutorial also gives you the full code content of the two files after the detailed instruction.

This tutorial is very detailed, and following the tutorial should make it very easy to write these two C++ files.

After completing the above steps, we are next going to compile and deploy. There are just two command lines we need to run in the terminal.

Finally, it’s time to try out the tic-tac-toe game we wrote. We’ll also test these actions in the terminal. Such as creating games, moving, seeing the game results, etc. Examples are given in the tutorial.

The whole tutorial ends perfectly here and by following the steps laid out in the tutorial, we can not only learn how to build a small game but also gain some understanding of EOS commands. Whether you’re a developer or a beginner, you will always find something interesting to learn about EOSIO here. 

I encourage everyone to try it out by themselves! Click here to access the full Tic Tac Toe tutorial on the EOSIO Developer Portal.

Lena Wang is an EOS Nation intern that has been with us for almost 2 years now. In 2015, Lena began her university study in the faculty of computer science of Sichuan University. For now, she is studying for her master’s degree with a full scholarship at Sichuan University, located in Chengdu, China. Due to her major, she has a strong interest in blockchain and she’s honored to be a part of EOS Nation and make some contributions by writing and translating articles about EOSIO. 

1 thought on “Learning EOSIO with Lena: Tic Tac Toe”

Comments are closed.

Daniel Keyes

Chief Operating Officer (COO)
Responsibilities include: product management, operations, community
Location: Toronto, Canada

Prior to founding the first EOS community in Toronto and co-founding EOS Nation, Daniel spent a decade in the financial technology industry working several diverse roles. His extensive experience in customer service, sales, sales coaching, agent training, digital marketing, digital process management (lean green belt), and product management (certified scrum master, certified product owner) eventually lead him to consulting for a blockchain dev shop.

Daniel earned a Bachelor of Journalism from Ryerson University in 2009 and worked as a chase producer intern at Global TV.

Daniel lives by the principles of Truth, Love, and Freedom.