-
Notifications
You must be signed in to change notification settings - Fork 0
/
jokenpo.sol
76 lines (55 loc) · 2.43 KB
/
jokenpo.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
67
68
69
70
71
72
73
74
75
76
/**
*Submitted for verification at Etherscan.io on 2024-03-09
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract JoKenPo {
// declaração de enumerador, para disponibilizar as opções possiveis do jogo
enum Options { NONE, ROCK, PAPER, SCISSORS }
// declaração do estado que utiliza o enumerador
// private é utilizado para o jogador 2, não ver a opção do jogador 1
// como padrao, é inicilializado com NOME ( jogador 1 ainda não jogou )
Options private choice1 = Options.NONE;
// guardar o endereço do jogador 1
address private player1;
// mensagens do jogo ( conseguir ver a situação do andamento do jogo )
string public result = "";
function update(string memory newResult) private {
result = newResult;
// limpar os estados
player1 = address(0);
choice1 = Options.NONE;
}
function play(Options newChoice) public {
// a jogada não pode ser NONE ( tem que ser uma opcao )
require(newChoice != Options.NONE, "Invalid choice") ;
// verificar se o player1 insistir em jogar novamente
require(player1 != msg.sender, "Wait the another player.");
// primeira jogada do player1
if (choice1 == Options.NONE) {
// guardar o endereço do player1
player1 = msg.sender;
// guarda a opção enviada pelo player1
choice1 = newChoice;
// atualizar andamento do jogo
result = "Player 1 choose his/her option. Waiting player 2";
}
// verificar se player 1 vence
else if (choice1 == Options.ROCK && newChoice == Options.SCISSORS)
update("Rock breaks scissors. Player 1 won.");
else if (choice1 == Options.PAPER && newChoice == Options.ROCK)
update("Paper wraps rock. Player 1 won.");
else if (choice1 == Options.SCISSORS && newChoice == Options.PAPER)
update("Scissors cuts paper. Player 1 won.");
// verificar se player 2 vence
else if (choice1 == Options.SCISSORS && newChoice == Options.ROCK)
update( "Rock breaks scissors. Player 2 won.");
else if (choice1 == Options.ROCK && newChoice == Options.PAPER)
update("Paper wraps rock. Player 2 won.");
else if (choice1 == Options.PAPER && newChoice == Options.SCISSORS)
update("Scissors cuts paper. Player 2 won.");
// Para empate
else
update("Draw game.");
}
}