在混音IDE中编译代码时遇到问题



试图编写岩石、纸张、剪刀游戏的代码,但在编译合同代码时遇到困难:

我得到的当前错误是:ParserError:应为"(",但得到了标识符-->

我想我在某个地方错过了一个(或(,但我不确定在哪里。

// 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行。此外,我认为你必须让bob和alice的地址以";pickWinner";作用同时做这两件事,你会得到这个:

// 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);
}





}

最新更新