Building An Ethereum Simulation Game – Part 1 – Inception
Tags: ethereum, blockchain, blockchaindeveloper, crypto
Published: 2020-03-28 22:37:18

---
title: Building An Ethereum Simulation Game – Part 1 – Inception
published: true
date: 2020-03-28 22:07:02 UTC
tags: Ethereum,blockchain,blockchain developer,crypto
canonical_url: https://blog.coincodecap.com/an-ethereum-simulation-game-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=an-ethereum-simulation-game-part-1
---

I recently wrote an article on ERC721 tokens. To demonstrate their use, I used the example of a Pokemon-like game. It got me thinking of some of the games I couldn’t get enough of as a kid.

The hours I spent on my Gameboy Colour trying to _catch ’em all_ must number in the hundreds if not thousands. As I grew older, my taste moved towards simulation games like The Sims, SimCity, and sports-oriented sims. One game, in particular, stands out more than others: [Football Manager](http://footballmanager.com).

#### Sports Simulations

> “Take over a football team and make it completely your own. From tactics and training, to transfers and trophies.” — [footbalmanager.com](https://www.footballmanager.com/)

It’s an enticing prospect. You can finally compare yourself to the pros by running your team the way you think it should be run.

* * *

Backing up a little… Before I found out about Football Manager, there was a similar online game that took up a lot of my after-school time.

[Sundayleague.com](http://sundayleague.com?utm_source=coincodecap) is of the same premise, except instead of simulating against a computer, you manage your team against other people. You play with fake generated players with funny names like _Ben Dover_. The pyramid system of leagues spans tens of levels with promotion and relegation. There are online forums for tactics, training, buying players, etc. All of these aspects contribute to the success of your team.

It’s time-based, so only one game is played per day at a scheduled time. Training your team is also limited and time-based.

In-game money can be won, but it can also be bought with real money if you want to progress your team faster.

You can bid for and buy players, increasing the quality of your team.

Whilst I was reminiscing about all of this, it struck me that these in-game football players could be considered ERC721 compliant if the game was running on the [Blockchain](https://blog.coincodecap.com/tag/blockchain/). How easy could it be to recreate a game like this on the [Ethereum Blockchain](https://blog.coincodecap.com/tag/ethereum/)?

> **ERC721 is [Non-Fungible Token Standard](https://en.wikipedia.org/wiki/Non-fungible_token)**

## Breaking It Down

Let’s assume, in our Ethereum football game, each player is an ERC721 token. In each squad, there are 20 players.

Each player has several attributes dictating their ability: fitness, speed, strength, creativity, work rate, aggression, passing, shooting, tackling.

Each player has an age, which increases depending on some metric (actual time, in-game time or matches played perhaps).

Training the players increases their stats depending on age, current stats, etc.

Managers can pick the formation, tactics and starting lineup for future matches. Matches take into account each player in each team, the starting eleven and the bench.

Each one of these decisions has some bearing on the result of each match, with some element of chance thrown in also.

* * *

At this point, the scale of our Ethereum football game is getting a little out of hand. We’re starting to rack up a lot of computing time for a lot of calculations, meaning lots and lots of gas.

Maybe team sports aren’t a great starting point. Let’s try something simpler, like a solo sport. At least then we only have one ERC721 token to deal with when performing calculations for training, and two for competing. How about Tennis?

* * *

## Tennis Manager

_I’m not a Tennis expert, but I’ve watched a few Wimbledon tournaments so I can make an educated guess to the kinds of attributes tennis players would consider important._

Each tennis player is an ERC721 token and has basic attributes:

- Personal: name, age, height
- Physical: technique, power, agility, stamina, condition

Let’s assume that we can train our players to increase their stats, and we can set up matches to compete against other players.

### Training

Increase specific physical attributes by _training_, whether that be agility, power, stamina or technique. It also has the side effect of decreasing the condition of the player, which can be increased by _resting_ the player.

All of these operations should cost something. Let’s call it XP. 

XP is gained by playing matches against other players.

_ **Note:** In its current state, XP is another attribute in the Tennis Player ERC721 token. However, there could be scope to convert this into an ERC20. Explore this later on._

### Matches

Matches can only be played against players which have registered to be available to play matches. They can only play if their condition is higher than a certain value, otherwise, they’re considered not match fit.

Tactics can be set before each match which influences the payer’s stats temporarily for that game.

For example, The _Attack_ tactic increases the power attribute but reduces stamina. The _Patient_ tactic increases technique but reduces power.

The mechanism for calculating match-winners is yet to be decided. (Initial thoughts: weightings on attributes and tactics, or numeric rules based on those same things.)

Both players gain XP from playing matches, but the winner gains more. Both player’s conditions decrease as a result of playing matches, which can be recuperated by _rest_.

## The First Smart Contracts

### ERC721 Player Contract

Given that the game is built on Ethereum and based around ERC721, I’m starting with the TennisPlayerBase ERC721 token.

The below [smart contract](https://blog.coincodecap.com/tag/smart-contact/) shows the token at this stage.

{% gist https://gist.github.com/alexroan/06b5a8d7c1be6df1699619e0f85eb3ac %}


[view raw](https://gist.github.com/alexroan/06b5a8d7c1be6df1699619e0f85eb3ac/raw/6efa6b54537b48b20f1275b7e6974df4b5a90954/TennisPlayerBase.sol)[TennisPlayerBase.sol](https://gist.github.com/alexroan/06b5a8d7c1be6df1699619e0f85eb3ac#file-tennisplayerbase-sol) hosted with ❤ by [GitHub](https://github.com)

  

_I’ve added in a boolean value in the Player struct called_ `isBot` _to distinguish between players managed by real people and bot players._

#### Training Smart Contract

Training the players requires a bit of logic, but not too much. The below smart contract shows us **TrainableTennisPlayer.sol** , a child of TennisPlayerBase.sol.

{% gist https://gist.github.com/alexroan/bd65a479c079d0d8f651e822b3e8de1a %}


[view raw](https://gist.github.com/alexroan/bd65a479c079d0d8f651e822b3e8de1a/raw/dfdd4167be7bc562d27f95970349b99c58a6415a/TrainableTennisPlayer.sol)[TrainableTennisPlayer.sol](https://gist.github.com/alexroan/bd65a479c079d0d8f651e822b3e8de1a#file-trainabletennisplayer-sol) hosted with ❤ by [GitHub](https://github.com)

  

The `train()` function on line 28 ensures that the player has enough XP and condition, subtracts the costs and adds the attribute gain.

The `rest()` function on line 53 ensures that the player has enough XP, then increases the condition attribute.

There are some private functions below these which help cast `uint8` variables to `uint256` so the contract can utilize OpenZeppelin’s SafeMath library.

I’ve left a todo statement near the state variables as a reminder that it might be a good idea to increase the difficulty of leveling up as players level up

* * *

### Next Steps

This project is in active development. At the time of writing, the current state of the code is as described in this article.

I’ll write some tests for the current contracts next, which probably isn’t worthy of another article. Once tested adequately, I’ll likely move on to adding the ability to play matches against other players., which will be the subject of part 2.

I’ve added the code repo to Github so it’s public and open to pull requests. If you’re interested in helping out with this Ethereum Game, You can contribute! [Here is the repo](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Falexroan%2Ftennis-manager).

**Also Read:**

- **[What are Dapps?](https://blog.coincodecap.com/what-are-dapps-an-ultimate-guide/)**
- **[The Ultimate Guide to DeFi](https://blog.coincodecap.com/the-ultimate-guide-to-defi-decentralized-finance/)**
- **[How Ethereum Mining Work](https://blog.coincodecap.com/how-ethereum-mining-works-an-overview/)**
- **[Ethereum Smart Contracts and the Oracle Problem](https://blog.coincodecap.com/ethereum-smart-contracts-and-the-oracle-problem/)**
- **[A Beginner’s Guide to DeFi (Decentralized Finance)](https://blog.coincodecap.com/a-beginner-guide-to-defi-decentralized-finance/)**
- [**Making Sense of Ethereum Transaction History With Etherscan**](https://blog.coincodecap.com/ethereum-transaction-history-with-etherscan/)


The post [Building An Ethereum Simulation Game – Part 1 – Inception](https://blog.coincodecap.com/an-ethereum-simulation-game-part-1/) appeared first on [CoinCodeCap Blog](https://blog.coincodecap.com).