EOS三连棋游戏

本文由【区块链研习社】优质内容计划支持,更多关于区块链的深度好文,请点击《区块链研习社》

前言

好吧,我承认本文有点“标题党”,本文其实是相通过玩三连棋,学习EOS的智能合约。

一、准备

参考上一篇文章《EOS智能合约部署》,这里我创建了一个tic.tac.toe账户。

二、部署

1、编译智能合约代码

由于编译EOS代码的时候没有编译tic_tac_toe,需要手动编译。

小tips:EOS自带智能合约源代码在~/eos/contracts目录下面,编译EOS代码时候生成的文件在~/eos/build/contracts目录下面。

$ eosc -o tic_tac_toe.wast tic_tac_toe.cpp

2、部署智能合约

$ eosc set contract tic.tac.toe tic_tac_toe.wast tic_tac_toe.abi

三、开始游戏

从abi文件可以看到,这个智能合约支持四种action(操作),分别是create、move、restart和close。

"actions": [{
      "action_name": "**create**",
      "type": "create"
    },{
      "action_name": "**restart**",
      "type": "restart"
    },{
      "action_name": "**close**",
      "type": "close"
    },{
      "action_name": "**move**",
      "type": "move"
    }
  ]

游戏开始,GO!

首先,我们先来创建棋局,tic.tac.toe是东道主,currency是挑战者(currency账户由上一篇文章《EOS智能合约部署》创建),创建棋局需要东道主的授权(–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

从abi文件可以看出,create操作所需要的数据,即challenger和host。

{
      "name": "create",
      "base": "",
      "fields": {
        "challenger": "account_name",
        "host": "account_name"
      }
}

现在开始下棋,东道主先下(比如东道主先在1行0列落子)。

$  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

从abi文件可以看出,move操作所需要的数据,即challenger、host、by和movement。

其中,by表示当前下棋人,movement表示落子的位置。

{
      "name": "move",
      "base": "",
      "fields": {
        "challenger": "account_name",
        "host": "account_name",
        "by": "account_name",
        "movement": "movement"
      }
  }

movement的结构中有row和column两个成员。

{
      "name": "movement",
      "base": "",
      "fields": {
        "row": "uint32",
        "column": "uint32"
      }
}

我们如何知道落子已经成功了呢?简单,查看一下棋局即可:

$ 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
}

从上面可以看到,在1行0列已经有子了。

tips:从ABI文件可以看到,table_name是games,所以get table的最后一个参数是games

"tables": {
"table_name": "**games**",
        "type": "game",
        "index_type": "i64",
        "key_names" : ["challenger"],
        "key_types" : ["account_name"]
 }

而game的类型如下,这与get table出来的数据结构一致

{
      "name": "game",
      "base": "",
      "fields": {
        "challenger": "account_name",
        "host": "account_name",
        "turn": "account_name",
        "winner": "account_name",
        "board": "uint8[]"
      }
}

好了,现在轮到挑战者落子了,原理和上面一样,这里就不多说了。

$ 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

从下面可以看到,在0行1列已经有子了。

$ 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
}

为了演示的简单,我们设定东道主从上往下下,挑战者从左往右下,那最终结果就是东道主胜利。

当东道主下第3子后,我们查看一下当前的棋局,发现胜负已定,winner是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
}

到这里,game over了,如果还想再来一盘,任何一方都已发起,再来一盘可以使用restart操作。

注意:如果使用restart操作,并不能改变挑战者和东道主的角色。

$ eosc push message tic.tac.toe restart '{"challenger":"currency", "host":"tic.tac.toe", "by":"currency"}' --scope currency,tic.tac.toe --permission currency@active

从abi文件可以看出,restart操作所需要的数据,即challenger、host和by。

{
      "name": "restart",
      "base": "",
      "fields": {
        "challenger": "account_name",
        "host": "account_name",
        "by": "account_name"
      }
}

如果玩完了,那就关掉棋局。

关掉棋局可以使用close操作。

$ eosc push message tic.tac.toe close '{"challenger":"currency", "host":"tic.tac.toe"}' --scope currency,tic.tac.toe --permission tic.tac.toe@active

从abi文件可以看出,close操作所需要的数据和create一样,即challenger和host。

{
      "name": "close",
      "base": "",
      "fields": {
        "challenger": "account_name",
        "host": "account_name"
      }
}

好了,游戏到此结束,想要知道上面规则还要阅读源代码,本人更多从ABI文件出发,和智能合约互动。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值