EOS Triple Chess Game __ Block chain

Source: Internet
Author: User

This article is supported by the "block chain Workshop" Quality content program, and more on the depth of the block chain, please click on "Block Chain workshop". Preface

Well, I admit this article is a bit of a "title party", this article is actually connected to play three games, learning EOS intelligent contract. First, prepare

For reference to the previous article, "EOS Smart Contract deployment," I created a Tic.tac.toe account here. Second, deployment 1, the compilation of intelligent contract code

Manual compilation is required because the Tic_tac_toe is not compiled when the EOS code is compiled.

Small Tips:eos with intelligent contract source code under the ~/eos/contracts directory, the file generated at the time the EOS code is compiled is under the ~/eos/build/contracts directory.

$ eosc-o Tic_tac_toe.wast tic_tac_toe.cpp
2, the deployment of intelligent contracts
$ eosc Set contract Tic.tac.toe Tic_tac_toe.wast Tic_tac_toe.abi
Third, start the game

As you can see from the ABI file, this smart contract supports four kinds of action, namely create, move, restart, and close.

"Actions": [{
      "action_name": "**create**", "
      type": "Create"
    },{
      "action_name": "**restart**",
      ' type ': ' Restart '
    },{
      ' action_name ': ' **close** ',
      ' type ': ' Close '
    },{
      ' action_name ': ' **move** ",
      " type ":" Move "
    }
  ]

Game started, go.

First, we'll start with the chess game, Tic.tac.toe is the host, currency is the Challenger (currency account created by the EOS Smart Contract deployment), creating the chess game requires the host's authorization (–permission tic.tac.toe@ Active

$ EOSC Push message Tic.tac.toe create ' {Challenger ': ' Currency ', ' Host ': ' Tic.tac.toe '} '--scope Currency,tic.tac.toe-- Permission Tic.tac.toe@active

As you can see from the ABI file, the data that the create operation requires is challenger and host.

{
      "name": "Create",
      "base": ","
      fields ": {"
        challenger ":" Account_name ",
        " host ":" Account _name "
      }
}

Now start playing chess, the host first (for example, host in 1 rows of 0 columns drop).

$  eosc Push message Tic.tac.toe move ' {Challenger ': ' Currency ', ' Host ': ' Tic.tac.toe ', ' by ': ' Tic.tac.toe ', ' Movement ": {" row ": 1," column ": 0}} '--scope Currency,tic.tac.toe--permission tic.tac.toe@active

As you can see from the ABI file, the data that the move operation requires is challenger, host, by, and movement.

Where by means the current chess player, movement represents the position of the drop.

{
      "name": "Move", "
      base": "",
      "fields": {"
        challenger": "Account_name",
        "host": "Account_name",
        "by": "Account_name",
        "movement": "Movement"
      }
  }

The movement structure has two members in row and column.

{
      "name": "Movement", "
      base": ",
      " fields ": {
        " row ":" UInt32 ",
        " column ":" UInt32 "
      }
}

How do we know that drop has succeeded? Simple, check the chess game can be:

$ eosc Get table Tic.tac.toe Tic.tac.toe games
{"
  rows": [{"
      Challenger": "Currency", "
      Host": " Tic.tac.toe ",
      " turn ":" Currency ",
      " winner ":" None ",
      " board ": [
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0
      ]
    }
  ,
  "more": True
}

As you can see from the above, there are already children in 1 rows and 0 columns.

Tips: As you can see from the ABI file, table_name is games, so the last argument to get table is games

"Tables": {
"table_name": "**games**",
        "type": "Game",
        "Index_type": "i64",
        "key_names": [" Challenger "],
        " key_types ": [" account_name "]
 }

The game type is the following, which is consistent with the data structure of Get table

{
      "name": "Game", "
      base": "",
      "fields": {"
        challenger": "Account_name",
        "host": "Account_name",
        "Turn": "Account_name", "
        Winner": "Account_name",
        "board": "uint8[]"
      }

OK, now it's the Challenger drop, and the principle is the same as above, here is not much to say.

$ eosc Push message Tic.tac.toe move ' {Challenger ': ' Currency ', ' Host ': ' Tic.tac.toe ', ' by ': ' Currency ', ' movement ': {' row ": 0," column ": 1}} '--scope Currency,tic.tac.toe--permission currency@active

As you can see from below, there are already children in 0 rows and 1 columns.

$ eosc Get table Tic.tac.toe Tic.tac.toe games
{"
  rows": [{"
      Challenger": "Currency", "
      Host": " Tic.tac.toe ",
      " Turn ":" Tic.tac.toe ",
      " winner ":" None ",
      " board ": [
        0,
        2,
        0,
        1,
        0,
        0,
        0,
        0,
        0
      ]
    }
  ,
  "more": True
}

To demonstrate the simplicity, we set the host from the top down, the challenger from left to right, and the final result is the host victory.

When the host 3rd son, we look at the current chess game, found that the outcome has been set, winner is Tic.tac.toe.

$ eosc Get table Tic.tac.toe Tic.tac.toe games
{"
  rows": [{"
      Challenger": "Currency", "
      Host": " Tic.tac.toe ",
      " turn ":" Currency ",
      " **winner** ":" Tic.tac.toe ",
      " board ": [
        0,
        2,
        2,
        1 ,
        1,
        1,
        0,
        0,
        0
      ]
    }
  ,
  "more": True
}

Here, game over, if you want to come again, either side has been launched, and then a disk can use restart operation.

Note: If you use the restart operation, you cannot change the role of Challenger and host.

$ EOSC Push message Tic.tac.toe restart ' {Challenger ': ' Currency ', ' Host ': ' Tic.tac.toe ', ' by ': ' Currency '} '--scope Currency,tic.tac.toe--permission currency@active

As you can see from the ABI file, the data required for the restart operation is challenger, host, and by.

{
      "name": "Restart", "
      base": "",
      "fields": {
        "challenger": "Account_name",
        "host": "Account_ Name ',
        ' by ': ' Account_name '
      }
}

If it's over, turn off the chess game.

Turn off the chess. You can use the close action.

$ eosc Push message Tic.tac.toe close ' {Challenger ': ' Currency ', ' Host ': ' Tic.tac.toe '} '--scope Currency,tic.tac.toe-- Permission Tic.tac.toe@active

As you can see from the ABI file, the close operation requires the same data as the create, that is, Challenger and host.

{
      "name": "Close",
      "base": ",
      " fields ": {"
        challenger ":" Account_name ",
        " host ":" Account_ Name '
      }
}

Well, the game is over, want to know the above rules to read the source code, I more from the ABI file, and intelligent contract interaction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.