-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.sol
More file actions
71 lines (61 loc) · 2.85 KB
/
Game.sol
File metadata and controls
71 lines (61 loc) · 2.85 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
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract Game {
string private instructions = "This is a game, you are in a forest. Type 'right' to go deeper or 'left' to try to exit.";
uint64 private count;
uint64 private consecutiveRights;
string private update;
bool private gameActive;
// Initialize the game
constructor() {
resetGame();
}
function resetGame() internal {
count = 0; // Start with the player not deep in the forest
consecutiveRights = 0; // Reset consecutive rights
update = "You are at the entrance of the forest. Choose 'right' or 'left'.";
gameActive = true; // Game starts active
}
function instruction() public view returns (string memory) {
return instructions;
}
function game(string memory _direction) public {
require(gameActive, "Game is over. Start a new game.");
if (count == 0) {
if (keccak256(abi.encodePacked(_direction)) == keccak256(abi.encodePacked('right'))) {
update = "You are going deeper into the forest.";
count += 1; // Increase count as you go deeper
consecutiveRights += 1; // Count the consecutive rights
} else if (keccak256(abi.encodePacked(_direction)) == keccak256(abi.encodePacked('left'))) {
update = "You have exited the forest!";
gameActive = false; // End the game
}
} else if (count > 0) {
if (keccak256(abi.encodePacked(_direction)) == keccak256(abi.encodePacked('left'))) {
update = "Keep going left. You are getting closer to exiting.";
count -= 1; // Decrease count as you take the correct direction
consecutiveRights = 0; // Reset consecutive rights
} else if (keccak256(abi.encodePacked(_direction)) == keccak256(abi.encodePacked('right'))) {
update = "You are still deep in the forest!";
count += 1; // Going right takes you deeper
consecutiveRights += 1; // Count the consecutive rights
// Check for loss condition
if (consecutiveRights >= 3) {
update = "You have gone too deep into the forest and lost the game. Start over!";
gameActive = false; // End the game
}
}
// Check if you reached the exit
if (count == 0) {
update = "You are successfully out of the forest!";
gameActive = false; // End the game
}
}
}
function currentStatus() public view returns (string memory) {
return update;
}
function startNewGame() public {
resetGame(); // Restart the game
}
}