Skip to content

Instantly share code, notes, and snippets.

@pom421
Created February 9, 2023 01:05
Show Gist options
  • Save pom421/5c8c00f98de95887aa1d57dc7d8b0195 to your computer and use it in GitHub Desktop.
Save pom421/5c8c00f98de95887aa1d57dc7d8b0195 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.18;
/**
* Guess a word with a clue.
*/
contract Guess {
address owner;
string mysteryText;
string clue;
// Mapping of people who have already try a guess.
mapping(address => bool) voters;
address winner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "You are not the owner of the contract.");
_;
}
modifier canGuess() {
require(voters[msg.sender] == false, "You have already try a guess.");
_;
}
modifier readyToPlay() {
require(
keccak256(abi.encodePacked(mysteryText)) !=
keccak256(abi.encodePacked("")),
"Mystery text is not set yet."
);
_;
}
modifier emptyGame() {
require(
keccak256(abi.encodePacked(mysteryText)) ==
keccak256(abi.encodePacked("")),
"Mystery text already set."
);
_;
}
function setWordAndClue(string memory _text, string memory _clue)
public
onlyOwner
emptyGame
{
if (
keccak256(abi.encodePacked(_text)) ==
keccak256(abi.encodePacked("")) ||
keccak256(abi.encodePacked(_clue)) ==
keccak256(abi.encodePacked(""))
) {
revert("The guess or clue are not compliant");
} else {
mysteryText = _text;
clue = _clue;
}
}
function makeAGuess(string memory _guess)
public
readyToPlay
canGuess
returns (bool)
{
voters[msg.sender] = true;
bool isFound = keccak256(abi.encodePacked(_guess)) ==
keccak256(abi.encodePacked(mysteryText));
if (isFound) winner = msg.sender;
return isFound;
}
function getClue() public view readyToPlay returns (string memory) {
return clue;
}
function getWinner() public view returns (bool, address) {
if (winner == address(0)) return (false, winner);
else return (true, winner);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment