Tinkercad-Powered Game Theory Summer School Hackathon

by arduinocelentano in Teachers > Coding

666 Views, 4 Favorites, 0 Comments

Tinkercad-Powered Game Theory Summer School Hackathon

logo.png

Summer camps may be fun, educating, entertaining, collaborative and so on. However, the way things are going this season, summer camps are mostly online and virtual. For me, as for an educator, it’s always been challenging. You could not use real hardware, tools and stuff, you could not see instantly how well your students deal with their projects, it’s more difficult to involve everyone into an activity.
In this instructable I’m sharing my virtual summer camp concept in form of an interactive online hackathon. It involves some Arduino wiring and programming, some approaches from mathematical game theory, some soft skills development, and even some features of social experiment which helps you to know your students better. I’ll use Tinkercad as a learning tool in a slightly unexpected way, so stay tuned!

This virtual summer camp is for you if:

  • you are teaching programming, electronics, STEM disciplines and/or use Arduino and Tinkercad;
  • you are teaching mathematical modeling and looking for tools to simulate interactively some game theory concepts;
  • you are teaching soft skills and looking for an interactive exercise;
  • you are not an educator, but you are interested in some of the aforesaid topics. Particularly, I’ll show you how to build an I²C network with several Arduino UNO boards and make them communicate.

Supplies

The Hackathon is completely virtual. So you and your students should have Internet access, compatible browsers and Tinkercad accounts.

The Game

rules.png

Imagine you are in front of a gambling machine which is designed for two players. I could call it “two-armed bandit”, but it would made things confusing, since such term with different meaning exists in probability theory. Your opponent is at the opposite side of the machine, so you are not able to see her or his turns. You have two options. You may put your coin in the machine and pull the bandit’s arm to make a turn. Or you may pull your opponent’s leg instead, that is to say, make turn without inserting a coin.

  1. If you cooperate (put in coins), you both receive two coins back.
  2. If one of the players cheats, he or she gains three coins prize, and another player loses one coin.
  3. If both players cheat, they receive nothing.

Explain your students that you want them to create a bot to play this game till the end of the summer camp and there will be tournament between them.

It’s obvious that in long-term perspective mutual trust is a win-win strategy, but temptation to victimize your opponent is strong enough and promises more benefits in short-term perspective. Would your students build a thriving high trust virtual society? Or would they create a population of suspicious liars without two pennies to rub together? Let’s find out.

[Spoiler]

Wiring

wiring01.png
wiring02.png
wiring03.png
wiring04.png

I used Tinkercad to create game engine mainly because I teach Arduino programming and my students are familiar with it. You could use collaborative mode of Tinkercad while explaining game mechanics, but later your students should have their own copies of Tinkercad project to avoid revealing their bots strategies. Each player is represented by an Arduino UNO board, so their code is isolated. One more board is needed to implement game server. Let’s call it Game Master. There are several ways of communication between Arduino boards, but I used I²C. The wiring is pretty much easy and it allows you to connect up to 128 devices.

You should connect all the boards in the following manner:

  • A4-A4-A4-...-A4
  • A5-A5-A5-...-A5
  • GND-GND-GND-...-GND

It means that A4 pins of all the boards should be wired together and so on. Quite simple, innit?

Game Server

protocol.png
wiring05.png
wiring06.png
wiring07.png

Click on the board you are going to use as a game server, push Code button, and upload the attached code.

I’ve made some comments so that you could figure out how it works. I implemented a simple communication protocol with 10 one-byte commands:

  1. CMD_START_GAME – game server asks player to join a duel.
  2. CMD_START_ROUND – game server asks player to make a turn in current round.
  3. CMD_TRUST – player wants to cooperate (response to CMD_START_ROUND).
  4. CMD_CHEAT – player wants to cheat (response to CMD_START_ROUND).
  5. CMD_MINUS_COIN – player receives -1 coin (response to CMD_TRUST if other player cheats).
  6. CMD_0COIN – player receives 0 coins (response to CMD_CHEAT if other player cheats).
  7. CMD_2COIN – player receives 2 coins (response to CMD_TRUST if other player cooperates).
  8. CMD_3COIN – player receives 3 coins (response to CMD_CHEAT if other player cooperates).
  9. CMD_END_GAME – server informs player about end of current game.
  10. CMD_READY – player informs server that they are ready to join a duel (response to CMD_START_GAME).

All the competition is a round-robin tournament. It means that each player plays one duel game against all other players. Game Master selects two players and asks them to join a duel. Each game consists of 10 rounds, but you could easily change this number. It is passed in the last argument of playGame function. In each round Game Masters asks players to make their turns, calculates their prizes and sends feedback. Please check out the attached diagram for more details.

Each player has a unique I²C address which is represented by integer number. It’s your responsibility to distribute IDs among your students. You should change I2C_1ST_PLAYER_ADDRESS and I2C_LAST_PLAYER_ADDRESS to have a proper players number. Please make sure you use all the IDs from this interval. I mean, you could not assign players IDs 1,2,4,5 and skip 3. The game engine assumes that players have consequent IDs.

Downloads

Game Client

wiring08.png
wiring09.png
wiring10.png
wiring11.png
wiring12.png

Click on the board you are going to use as a player, push Code button, and upload the attached code. Repeat this procedure for each client. Make sure you use unique ID for each client and those IDs are the same as Game Master expects. To achieve this, you should manually change I2C_SLAVE_ADDRESS in each client.

The client code is asynchronous with two event handlers (requestEvents and receiveEvents). It is necessary to avoid data corruption. Game Master tells players when they are allowed to send messages. Variables command and response store received command and response to send. The protocol is described in the previous step.

It’s your choice whether discuss or not discuss all the communication details with your students. You know your audience better. The simplest approach is to ask your students to re-implement the only function in client code, namely playRound. So all they need is to call setResponse function with one of two possible arguments. Pretty easy. They could also analyze a previous turn reward by checking command variable. It may help in implementing their strategy. Some simple strategies to start with:

//Random strategy: I will cheat or cooperate randomly
void playRound()
{
  setResponse( random(CMD_TRUST, CMD_CHEAT+1) );
}
//Naïve strategy: I will always cooperate
void playRound()
{
  setResponse(CMD_TRUST);
}
//Deceptive strategy: I will always cheat
void playRound()
{
  setResponse(CMD_CHEAT);
}
//Copycat strategy: First time I will trust. Afterwards I’ll copy the last turn of my opponent
void playRound()
{
  static byte previousResult = CMD_2COIN;
  if ((previousResult == CMD_2COIN)||(previousResult == CMD_3COIN))
      setResponse(CMD_TRUST);
  else
      setResponse(CMD_CHEAT);
  while(!command);//Waiting for round result from server
  previousResult = command;//Saving previous round result
}

Downloads

Playing

wiring13.png
wiring14.png

When you uploaded sketches to all the boards, you should be ready to start simulation. Just push Run button and open Serial monitor of the Game Master. If you assembled everything correctly, you’ll see duels results and finally total tournament scores.

Discussion

Now you are ready for self-reflection session with your students. It’s up to you what would you like to discuss. If you are interested in mathematical aspects, you could introduce prisoner's dilemma theory, which describes this sort of games in game theory. Maybe it would be interesting to change the rules and run similar instances of each bot in the tournament to collect more statistics. This interactive demo may be helpful for you. Alternatively, you may go into ethical and psychological issues and discuss cooperation and social trust problems. Chapters 11 and 12 of “Predictably Irrational” by Dan Ariely may be a good starting point in preparing such a discussion. Finally, you could also carry out your own research to know your students better. For example, do tournament results depend on relations in the group, would they be the same in a group of random stranger students? What happens if you propose your students the ability to exchange coins they won for some real minor items? Do results differ in groups of students from different countries? Who knows, maybe your findings would be interesting, surprising or could help in better understanding the world and society around us. Your thoughts are always welcomed.