我正在练习如何在 EOS 中编写智能合约,并且我正在尝试构建我的井字游戏版本,以 EOS 开发者门户中的示例为例。
但是,由于某种原因,当我尝试生成 .wast 文件时,我收到错误消息“test.cpp:173:1: 错误:构造函数从基类 'contract' 继承的 'test' 被隐式删除”。
在这个问题出现之前,我被拒绝构建 abi 文件,因为我缺乏李嘉图合约。但是,EOS 开发者门户中的井字游戏示例也没有......
(所有代码都是一个 .cpp 文件)
#include <eosiolib/eosio.hpp>
#include <stdint.h>
using namespace eosio;
class test : public eosio::contract{
public:
using contract::contract;
test(account_name challenger_id, account_name host_id) :
contract(challenger_id), games_table(_self, _self) { }
bool board_is_full(uint32_t const board[9]) {
for (int a = 0; a <= 8; a++) {
if (board[a] != 0)
continue;
else
return false;
}
return true;
}
bool is_valid_move(uint8_t loc, uint32_t const board[9]) {
if (board[loc] == 0)
return true;
else
return false;
}
bool three_in_a_row(uint32_t const board[9], int num) {
for (int a = 0; a <= 2; a++) {
for (int b = 0; b <= 6; b += 3) {
if (board[a + b] == num) {
if (b == 6) {
return true;
}
continue;
}
break;
}
}
for (int a = 0; a <= 2; a+=2) {
if(a==0) {
for (int b = 0; b <= 8; b += 4) {
if (board[a + b] == num) {
if (b == 8) {
return true;
}
continue;
}
else {
break;
}
}
}
else{
for(int b = 0; b<=4; b++) {
if (board[a + b] == num) {
if (b == 4) {
return true;
}
continue;
}
else {
break;
}
}
}
}
return false;
}
int winner_scanner(uint32_t const board[9]) {
if (three_in_a_row(board, 1)) {
return 1;
}
if (three_in_a_row(board, 2)) {
return 2;
}
return 0;
}
///@abi action
void create(account_name challenger, account_name host) {
auto itr = games_table.find(challenger);
eosio_assert(static_cast<uint32_t>(itr != games_table.end()), "game already exists");
games_table.emplace(challenger, [&](auto& g) {
g.challenger = challenger;
g.host = host;
g.turn = host;
});
}
///@abi action
void place(account_name challenger, account_name turn, int loc){
auto itr = games_table.find(challenger);
require_auth(turn);
require_auth(itr->challenger);
eosio_assert(games_table.end() == itr, "game does not exist");
eosio_assert(itr->turn == turn, "not this player's turn");
eosio_assert(!board_is_full(itr->board), "board is full");
eosio_assert(is_valid_move(loc, itr->board), "invalid move");
games_table.modify(itr, challenger, [&](auto& g){
if(challenger == turn){
g.turn = g.host;
g.board[loc] = 2;
}
else{
g.turn = g.challenger;
g.board[loc] = 1;
}
});
int winner = winner_scanner(itr->board);
if (winner != 0) {
if (winner == 2) {
eosio_assert(true, "Challenger wins!");
}
if (winner == 1) {
eosio_assert(true, "Host wins!");
}
}
}
///@abi action
void restart(account_name challenger){
auto itr = games_table.find(challenger);
eosio_assert(itr!=games_table.end(), "game doesn't exist");
games_table.modify(itr, challenger, [&](auto& g){
for(int a= 0; a<=8; a++){
g.board[a] = 0;
}
});
}
///@abi action
void forfeit(account_name challenger, account_name forfeiter){
auto itr = games_table.find(challenger);
eosio_assert(itr==games_table.end(), "game does not exist");
games_table.erase(itr);
}
private:
///@abi table
struct game{
account_name challenger;
account_name host;
account_name turn;
uint32_t board[9];
account_name primary_key() const {return challenger;}
EOSLIB_SERIALIZE(game, (challenger)(host)(turn))
};
multi_index<N(games_table), game> games_table;
};
EOSIO_ABI(test, (create)(place)(restart)(forfeit))