I am trying to write a game on smart contract which is a game in which the player has to type in 5 guesses, and based on how close they were to the actual number, they would receive ether as prize.
GuessTheNumber:
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;
contract Oracle {
address owner;
uint public variableRand;
constructor () {
owner = msg.sender;
}
function setRandomVariable (uint _varRand) external {
require (msg.sender == owner);
variableRand = _varRand;
}
}
contract Play {
// Saves the value of what you have won in the session
address owner;
uint public currentPrize = 0;
int[] public currentGuess;
uint[] public randNum;
event Log(uint gas);
function showPoolBalance() public view returns(uint) {
return address(this).balance;
}
function randMod() public view returns(uint) {
// grab information from the blockchain randomly to generate random numbers - we need something dynamically changing
// abi.encodePacked concatonates arguments nicely
return uint(keccak256(abi.encodePacked(oracle.variableRand, block.timestamp, block.difficulty, msg.sender))) % 1000;
}
function inputGuess(int _guess) public {
// Adds the guesses to the array
currentGuess.push(_guess);
}
function clearGuesses() public {
for (uint i = 0; i < currentGuess.length; i++) {
//removes all of the guesses in the array
currentGuess.pop();
}
}
function abs(int x) private pure returns (int) {
//returns the abolute value of the input
return x >= 0 ? x : -x;
}
function game() private returns (int, uint) {
for (uint i = 0; i <= 5; i++) {
randNum[i] = randMod();
int absv = abs(int(randNum[i]) - currentGuess[i]);
if (absv < 6 && absv > 0) {
currentPrize += 2;
}
else if (absv == 0) {
currentPrize += 4;
}
else if (absv < 16 && absv > 5) {
currentPrize += 1;
}
return (absv , randNum[i]);
}
}
function play() public payable returns (int, uint) {
require(msg.value == (10 ether), "You must pay with 10 ethers!");
game();
}
function chargePool() public payable {
require (msg.value >= 10 ether);
}
function withdrawPrize() public payable {
payable(msg.sender).transfer(currentPrize);
}
fallback () external payable {
emit Log(gasleft());
}
Oracle oracle;
constructor (address oracleAddress) {
oracle = Oracle(oracleAddress);
owner = msg.sender;
}
}
The Oracle contract deploys correctly. and the Play contract would deploy as well. The guessed numbers would be pushed in the array correctly but when I wanna call the play function with 10 ether the following error comes up :
[vm]from: 0x5B3...eddC4to: Play.play() 0xf8e...9fBe8value: 10000000000000000000 weidata: 0x93e...84cd9logs: 0hash: 0xfd3...dcad2 transact to Play.play errored: VM error: revert.
revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
Any help would be appreciated.