-
Notifications
You must be signed in to change notification settings - Fork 1
/
web3 game.sol
66 lines (52 loc) · 2.55 KB
/
web3 game.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract RockPaperScissors {
// Enum to represent the possible moves in the game
enum Move {None, Rock, Paper, Scissors}
// Mapping to store the address of each player and their chosen move
mapping(address => Move) public playerMoves;
// Mapping to store the balance of each player
mapping(address => uint256) public playerBalances;
// Event to be emitted when a game is played
event GamePlayed(address indexed player, Move playerMove, address indexed opponent, Move opponentMove, string result);
// Function to play a move in the game
function playMove(Move move) external {
require(move == Move.Rock || move == Move.Paper || move == Move.Scissors, "Invalid move");
// Ensure the player has not played before
require(playerMoves[msg.sender] == Move.None, "You have already played");
// Set the player's move
playerMoves[msg.sender] = move;
}
// Function to determine the winner and distribute rewards
function determineWinner(address opponent) external {
require(playerMoves[msg.sender] != Move.None, "You haven't played yet");
require(playerMoves[opponent] != Move.None, "Your opponent hasn't played yet");
// Get the moves of the players
Move playerMove = playerMoves[msg.sender];
Move opponentMove = playerMoves[opponent];
// Determine the winner and update balances
if ((playerMove == Move.Rock && opponentMove == Move.Scissors) ||
(playerMove == Move.Paper && opponentMove == Move.Rock) ||
(playerMove == Move.Scissors && opponentMove == Move.Paper)) {
// Player wins
playerBalances[msg.sender] += 1;
playerBalances[opponent] -= 1;
emit GamePlayed(msg.sender, playerMove, opponent, opponentMove, "You win!");
} else if (playerMove == opponentMove) {
// It's a draw
emit GamePlayed(msg.sender, playerMove, opponent, opponentMove, "It's a draw!");
} else {
// Player loses
playerBalances[msg.sender] -= 1;
playerBalances[opponent] += 1;
emit GamePlayed(msg.sender, playerMove, opponent, opponentMove, "You lose!");
}
// Reset the moves for both players
playerMoves[msg.sender] = Move.None;
playerMoves[opponent] = Move.None;
}
// Function to check the balance of the player
function getBalance() external view returns (uint256) {
return playerBalances[msg.sender];
}
}