-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorBottleGame.sol
More file actions
60 lines (47 loc) · 1.64 KB
/
Copy pathColorBottleGame.sol
File metadata and controls
60 lines (47 loc) · 1.64 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ColorBottleGame {
uint256[5] public correctArrangement;
uint256[5] public playerAttempt;
uint256 public attemptsLeft = 5;
bool public gameOver = false;
event AttemptResult(uint256 correctPositions, uint256 attemptsLeft);
constructor() {
for (uint256 i = 0; i < 5; i++) {
correctArrangement[i] = (uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, i))) % 5) + 1;
}
}
function newAttempt(uint256[5] memory _attempt) public {
require(!gameOver, "Game is over. Start a new game.");
require(attemptsLeft > 0, "No attempts left. Start a new game.");
playerAttempt = _attempt;
uint256 correctPositions = 0;
for (uint256 i = 0; i < 5; i++) {
if (playerAttempt[i] == correctArrangement[i]) {
correctPositions++;
}
}
emit AttemptResult(correctPositions, attemptsLeft);
attemptsLeft--;
if (correctPositions == 5) {
gameOver = true;
}
if (attemptsLeft == 0 && !gameOver) {
shuffle();
}
}
function shuffle() private {
for (uint256 i = 0; i < 5; i++) {
correctArrangement[i] = (uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, i))) % 5) + 1;
}
}
function startNewGame() public {
// Reset the game state
attemptsLeft = 5;
gameOver = false;
shuffle();
}
function getCorrectArrangement() public view returns (uint256[5] memory) {
return correctArrangement;
}
}