In this lesson we will walkthrough the solution and discuss the improvements/extensions that can be made to the implementation
The solution is shown below so only look if you have had a go at solving it yourself and make sure to watch the video first
.
..
...
....
.....
......
.......
........
.........
..........
1
pragma solidity 0.6.0;
2
3
contract Game {
4
bytes32 constant ROCK = "ROCK";
5
bytes32 constant PAPER = "PAPER";
6
bytes32 constant SCISSORS = "SCISSORS";
7
8
mapping(address => bytes32) public choices;
9
10
function play(bytes32 choice) external {
11
require(choices[msg.sender] == bytes32(0)); // make sure player hasnt played before
12
choices[msg.sender] = choice;
13
}
14
15
function evaluate(
16
address alice,
17
bytes32 aliceChoice,
18
bytes32 aliceRandomness,
19
address bob,
20
bytes32 bobChoice,
21
bytes32 bobRandomness
22
) external view returns (address) {
23
// make sure the commitment of the choices hold
24
require(
25
keccak256(abi.encodePacked(aliceChoice, aliceRandomness)) ==
26
choices[alice]
27
);
28
29
require(
30
keccak256(abi.encodePacked(bobChoice, bobRandomness)) ==
31
choices[bob]
32
);
33
34
if (aliceChoice == bobChoice) {
35
return address(0);
36
}
37
38
if (aliceChoice == ROCK && bobChoice == PAPER) {
39
return bob;
40
} else if (bobChoice == ROCK && aliceChoice == PAPER) {
41
return alice;
42
} else if (aliceChoice == SCISSORS && bobChoice == PAPER) {
43
return alice;
44
} else if (bobChoice == SCISSORS && aliceChoice == PAPER) {
45
return bob;
46
} else if (aliceChoice == ROCK && bobChoice == SCISSORS) {
47
return alice;
48
} else if (bobChoice == ROCK && aliceChoice == SCISSORS) {
49
return bob;
50
}
51
}
52
}