Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created August 3, 2018 15:25
Show Gist options
  • Save ruprict/a68204d1d1f73522e4e542803a6683e7 to your computer and use it in GitHub Desktop.
Save ruprict/a68204d1d1f73522e4e542803a6683e7 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.4.24+commit.e67f0147.js&optimize=true&gist=
pragma solidity 0.4.24;
contract Casino {
address public owner;
uint256 public minimumBet;
uint256 public totalBet;
uint256 public numberOfBets;
uint256 public maxAmountOfBets = 100;
address[] public players;
struct Player {
uint256 amountBet;
uint256 numberSelected;
}
mapping(address => Player) public playerInfo;
constructor(uint256 _minimumBet) public {
owner = msg.sender;
if(_minimumBet != 0) minimumBet = _minimumBet;
}
function kill() public {
if(msg.sender == owner) selfdestruct(owner);
}
function bet(uint256 numberSelected) public payable {
require(!checkPlayerExists(msg.sender));
require(numberSelected >= 1 && numberSelected <= 10);
require(msg.value >= minimumBet);
playerInfo[msg.sender].amountBet = msg.value;
playerInfo[msg.sender].numberSelected = numberSelected;
numberOfBets++;
players.push(msg.sender);
totalBet += msg.value;
if(numberOfBets >= maxAmountOfBets) generateNumberWinner();
}
function checkPlayerExists(address player) public constant returns(bool){
for(uint256 i = 0; i < players.length; i++){
if(players[i] == player) return true;
}
return false;
}
function generateNumberWinner() public {
uint256 numberGenerated = block.number % 10 + 1; //not secure
distributePrizes(numberGenerated);
}
function distributePrizes(uint256 numberWinner) public {
address[100] memory winners;
uint256 count = 0;
for (uint256 i = 0 ; i < players.length; i++) {
address playerAddress = players[i];
if(playerInfo[playerAddress].numberSelected == numberWinner) {
winners[count] = playerAddress;
count++;
}
delete playerInfo[playerAddress]; // Delete all the players
}
players.length = 0;
uint256 winnerEtherAmount = totalBet / winners.length; //How much each winner gets
for(uint256 j = 0; j < count; j++) {
if(winners[j] != address(0)) // Check that the address in this fixed array is not empty
winners[j].transfer(winnerEtherAmount);
}
}
function() payable public {}
}
pragma solidity ^0.4.7;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () {
ballotToTest = new Ballot(2);
}
function checkWinningProposal () public {
ballotToTest.vote(1);
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal");
}
function checkWinninProposalWithReturnValue () public constant returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment