Reputation: 5
Trying to code a rocks, paper, scissors game, but having trouble getting the contract code to compile:
Current error I am getting is: ParserError: Expected '(' but got identifier -->
I think I'm missing a { or ) somewhere, but I'm not sure where.
// 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);
}
}
}
Upvotes: 0
Views: 408
Reputation: 571
You wrongly enclosed the "evaluate" function. You need to move the curly brace in line 72 to line 61. Also, I think you have to make bob and alice addresses payable in the "pickWinner" function. Doing both things you'll have this:
// 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);
}
}
Upvotes: 1