Skip to content

Instantly share code, notes, and snippets.

@gHashTag
Created July 21, 2023 07:25
Show Gist options
  • Save gHashTag/9102a59975aee2da065741b543a03282 to your computer and use it in GitHub Desktop.
Save gHashTag/9102a59975aee2da065741b543a03282 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: MIT
pragma solidity ^0.8.0;
contract LeelaGame {
uint8 constant MAX_ROLL = 6;
mapping(address => uint8[]) public playerRolls;
event DiceRolled(address indexed roller, uint8 rolled);
function rollDice() external {
uint8 rollResult = generateRandomNumber();
playerRolls[msg.sender].push(rollResult);
emit DiceRolled(msg.sender, rollResult);
}
function generateRandomNumber() private view returns (uint8) {
uint256 randomNum = uint256(keccak256(abi.encodePacked(block.timestamp, blockhash(block.number - 1), msg.sender)));
uint8 roll = uint8((randomNum % MAX_ROLL) + 1);
return roll;
}
function getRollHistory(address player) public view returns (uint8[] memory) {
return playerRolls[player];
}
function getRollAtIndex(address player, uint index) public view returns (uint256) {
require(index < playerRolls[player].length, "Index out of bounds");
return uint256(playerRolls[player][index]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment