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 [email protected])
$ eosc push message tic.tac.toe create '{"challenger" :"currency", "host":"tic.tac.toe"}' --scope currency,tic.tac.toe --permission [email protected]
從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 [email protected]
從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 [email protected]
從下面可以看到,在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 [email protected]
從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 [email protected]
從abi檔案可以看出,close操作所需要的資料和create一樣,即challenger和host。
{
"name": "close",
"base": "",
"fields": {
"challenger": "account_name",
"host": "account_name"
}
}
好了,遊戲到此結束,想要知道上面規則還要閱讀原始碼,本人更多從ABI檔案出發,和智慧合約互動。