solidity - 在 remix IDE 中编译我的代码时遇到问题
问题描述
尝试编写石头、纸、剪刀游戏,但无法编译合约代码:
我得到的当前错误是: ParserError: Expected '(' but got identifier -->
我想我在某处缺少 { 或 ) ,但我不确定在哪里。
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract Game {
uint8 constant ROCK = 0;
uint8 constant PAPER = 1;
uint8 constant SCISSORS = 2;
address[] public players;
// the public keyword will create a function with the same name as the mapping which will allow us to lookup the key outside the contract
// no data is ever hidden in a smart contract deployed on a public chain and using `private` will not hide data in any way.
mapping(address => uint8) public choices;
function enroll() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function play(uint8 choice) external {
// check that the move is valid
require(choice == ROCK || choice == PAPER || choice == SCISSORS);
// check that the player hasnt played the move already
require(choices[msg.sender] == 0);
// set the choice for the players address
choices[msg.sender] = choice;
}
function evaluate(address alice, address bob)
public
view
returns (address add)
{
// if the choices are the same, the game is a draw, therefore returning 0x0000000000000000000000000000000000000000 as the winner
if (choices[alice] == choices[bob]) {
return address(0);
}
// paper beats rock bob/alice
if (choices[alice] == ROCK && choices[bob] == PAPER) {
return bob;
// paper still beats rock (played in opposite alice/bob)
} else if (choices[bob] == ROCK && choices[alice] == PAPER) {
return alice;
} else if (choices[alice] == SCISSORS && choices[bob] == PAPER) {
return alice;
} else if (choices[bob] == SCISSORS && choices[alice] == PAPER) {
return bob;
} else if (choices[alice] == ROCK && choices[bob] == SCISSORS) {
return alice;
} else if (choices[bob] == ROCK && choices[alice] == SCISSORS) {
return bob;
}
function pickWinner(address bob, address alice) public payable {
if (evaluate(alice, bob) == bob) {
bob.transfer(address(this).balance);
}
if (evaluate(alice, bob) == alice) {
alice.transfer(address(this).balance);
}
players = new address[](0);
}
}
}
解决方案
您错误地包含了“评估”功能。您需要将第 72 行的大括号移动到第 61 行。另外,我认为您必须在“pickWinner”函数中设置 bob 和 alice 地址。做这两件事你会得到这个:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract Game {
uint8 constant ROCK = 0;
uint8 constant PAPER = 1;
uint8 constant SCISSORS = 2;
address[] public players;
// the public keyword will create a function with the same name as the mapping which will allow us to lookup the key outside the contract
// no data is ever hidden in a smart contract deployed on a public chain and using `private` will not hide data in any way.
mapping(address => uint8) public choices;
function enroll() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function play(uint8 choice) external {
// check that the move is valid
require(choice == ROCK || choice == PAPER || choice == SCISSORS);
// check that the player hasnt played the move already
require(choices[msg.sender] == 0);
// set the choice for the players address
choices[msg.sender] = choice;
}
function evaluate(address alice, address bob)
public
view
returns (address add)
{
// if the choices are the same, the game is a draw, therefore returning 0x0000000000000000000000000000000000000000 as the winner
if (choices[alice] == choices[bob]) {
return address(0);
}
// paper beats rock bob/alice
if (choices[alice] == ROCK && choices[bob] == PAPER) {
return bob;
// paper still beats rock (played in opposite alice/bob)
} else if (choices[bob] == ROCK && choices[alice] == PAPER) {
return alice;
} else if (choices[alice] == SCISSORS && choices[bob] == PAPER) {
return alice;
} else if (choices[bob] == SCISSORS && choices[alice] == PAPER) {
return bob;
} else if (choices[alice] == ROCK && choices[bob] == SCISSORS) {
return alice;
} else if (choices[bob] == ROCK && choices[alice] == SCISSORS) {
return bob;
}
}
function pickWinner(address payable bob, address payable alice) public payable {
if (evaluate(alice, bob) == bob) {
bob.transfer(address(this).balance);
}
if (evaluate(alice, bob) == alice) {
alice.transfer(address(this).balance);
}
players = new address[](0);
}
}
推荐阅读
- azure - 了解 Azure 发布管道
- python - Matplotlib:使用 ArtistAnimation 为 plot_surface 设置动画
- db2 - DB2 访问非唯一表中的特定行,以进行更新/删除操作
- codeigniter - 使用 codeigniter 取消链接功能未成功
- eclipse-plugin - Eclipse 中的 Sigasi
- postgresql - Postgres - BDR 复制 - auto_increment 主键的问题
- javascript - NodeJS请求从函数返回JSON
- java - 如果我注释一些代码,JButton 就会消失
- vue.js - 如何更改 nuxt.js 路由器以使用自定义路径?
- r - 通过 gganimate 动画条形图:view_follow 和 coord_flip 的冲突