Skip to content

Instantly share code, notes, and snippets.

@ohadcn
Last active May 16, 2018 08:06
Show Gist options
  • Save ohadcn/7226fc4b521c1bafc428e70fa79fc6c5 to your computer and use it in GitHub Desktop.
Save ohadcn/7226fc4b521c1bafc428e70fa79fc6c5 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.23+commit.124ca40d.js&optimize=false&gist=
pragma solidity ^0.4.23;
// pragma experimental ABIEncoderV2;
library HandLib {
// enum HAND_TYPES { HIGH_CARD, ONE_PAIR, TWO_PAIRS, THREE, STRAIGHT, FLUSH, FULL_HOUSE, FOUR, STRAIGHT_FLUSH}
// mapping(HAND_TYPES => uint8) handRank { HIGH_CARD: 0, ONE_PAIR: 1, TWO_PAIRS: 2, THREE: 3, STRAIGHT: 4, FLUSH: 5, FULL_HOUSE: 5, FOUR: 6, STRAIGHT_FLUSH: 7}
function getCardRank(uint8 card) private pure returns (uint8 rank) {
require(card < 52);
rank = (card % 13);
}
function getCardSuit(uint8 card) pure private returns (uint8 suit) {
suit = card / 13;
}
function getHighCard(uint8[5] hand) pure private returns (uint8 high) {
high = getCardRank(hand[0]);
for (uint8 i = 1; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card > high) {
high = card;
}
}
}
function getHighCardExcept(uint8[5] hand, uint8 except) pure private returns (uint8 high) {
high = getCardRank(hand[0]);
for (uint8 i = 1; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card != except && (card > high || high == except )) {
high = card;
}
}
}
function verifyPair(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
bool found = false;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found){
res = true;
return;
}
found = true;
}
}
res = false;
}
function verifyThree(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
uint8 found = 0;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found == 2){
res = true;
return;
}
found++;
}
}
res = false;
}
function verifyFour(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
uint8 found = 0;
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
if(card == handRank) {
if(found == 3) {
res = true;
return;
}
found++;
}
}
res = false;
}
function verifyFlush(uint8[5] hand, uint8 /* handRank*/) pure private returns (bool res) {
uint8 suit = getCardSuit(hand[0]);
for (uint8 i = 1; i < 5; i++) {
require(getCardSuit(hand[i]) == suit);
}
res = true;
}
function verifyStright(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
bool[5] memory found;
for (uint8 j = 0; j < 5; j++) {
found[j] = false;
}
for (uint8 i = 0; i < 5; i++) {
uint8 card = getCardRank(hand[i]);
uint8 diff = handRank - card;
if(handRank == 3 && card == 12)
diff = 4;
require(4 >= diff && diff >= 0 );
require(!found[diff]);
found[diff] = true;
}
res = true;
}
function verifyStrightFlush(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
verifyStright(hand, handRank);
verifyFlush(hand, handRank);
res = true;
}
function verifyFullHouse(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
if(verifyThree(hand, handRank) &&
verifyPair(hand, getHighCardExcept(hand, handRank))) {
res = true;
} else
res = false;
}
function verifyTwoPairs(uint8[5] hand, uint8 handRank) pure private returns (bool res) {
verifyPair(hand, handRank);
uint8[2] memory cards;
for(uint8 j = 0; j < 2; j++) {
cards[j] = 15;
}
for(uint i = 0; i < 5; i++) {
uint8 rank = getCardRank(hand[i]);
if(rank == handRank)
continue;
if(rank == cards[0] || rank == cards[1]) {
res = true;
return;
}
cards[(cards[0] == 15) ? 0 : 1] = rank;
}
res = false;
}
function verifyHand(uint8[5] hand, uint8 handType, uint8 handRank) pure public returns (bool res) {
require(handRank < 13);
res = true;
if(handType == 0)
require(getHighCard(hand) == handRank);
else if(handType == 1)
require(verifyPair(hand, handRank));
else if(handType == 2)
require(verifyTwoPairs(hand, handRank));
else if(handType == 3)
require(verifyThree(hand, handRank));
else if(handType == 4)
require(verifyStright(hand, handRank));
else if(handType == 5)
require(verifyFlush(hand, handRank));
else if(handType == 6)
require(verifyFullHouse(hand, handRank));
else if(handType == 7)
require(verifyFour(hand, handRank));
else if(handType == 8)
require(verifyStrightFlush(hand, handRank));
else
res = false;
}
}
pragma solidity ^0.4.23;
import "./table.sol";
contract Hand {
// enum HandState {
// SHUFFLE_COMMIT: 0, GIVE_CARDS: 1, PRE_FLOP:2 , FLOP: 3, PRE_TURN: 4, TURN: 5, PRE_RIVER: 6, RIVER: 7, POST_RIVER: 8, SHOW: 9
// }
Table table;
uint8 state;
uint8 turn;
uint8 checks;
uint curVal;
mapping(address => bytes32) cardShuffle;
mapping(address => uint) pot;
mapping(address => uint8[2]) userCards;
mapping(address => uint8[2]) userCardsRank;
modifier onlyplayer(uint8 reqState, uint8 player) {
if (msg.sender == table.players[player] && state == reqState)
_;
}
function put(uint ammount, address player) private {
require(ammount < table.balances[player]);
prev = table.balances[player];
require(prev > ammount);
require(ammount >= curVal || (ammount - prev) == table.balances[player]);
table.balances[player] -= ammount - prev;
pot[address] = ammount;
}
constructor(Table _table, address _bigBlind, address _smallBlind, uint8 _startingPlayer) public {
table = _table;
put((table.blind / 2), _smallBlind);
put(table.blind, _bigBlind);
turn = _startingPlayer;
}
function commitShuffle(bytes32 shuffleHash) public {
require(msg.sender == table.players[turn]);
require(state == 0);
cardShuffle[msg.sender] = shuffleHash;
turn = (turn + 1) % table.playersOnline;
checks++;
if(checks == table.playersOnline) {
state++;
checks = 0;
}
}
function acceptCards() public onlyplayer(1, turn) {
turn = (turn + 1) % table.playersOnline;
checks++;
if(checks == table.playersOnline) {
state++;
checks = 0;
}
}
function nextPlayer() {
turn = (turn + 1) % table.playersOnline;
while(userCards[table.players[turn]][0] == 100) {
turn = (turn + 1) % table.playersOnline;
checks++;
}
if(checks == table.playersOnline) {
state++;
}
}
function bet(uint val) public { //can be used for call / raise / bet
require((state % 2) == 0);
require(state != 0);
require(msg.sender == table.players[turn]);
put(val, msg.sender);
checks = 0;
nextPlayer();
}
function fold() {
require((state % 2) == 0);
require(state != 0);
require(msg.sender == table.players[turn]);
userCards[msg.sender][0] = 100;
checks++;
nextPlayer();
}.
function submitCards(bytes32[5][] proof1, uint8 proof1Path, uint8 card1, bytes32[5][] proof2, uint8 proof2Path, uint8 card2) {
nmuj
}
}
import random
import hashlib
def merckleTree():
cards = [i for i in range(52)]
random.shuffle(cards)
cards4hash = [(random.randrange(1<<248)<<8) + card for card in cards]
cards4hash += [random.randrange(1<<256) for i in range(52,64)]
hashes = [hashlib.sha256(card.to_bytes(256, 'little')).digest() for card in cards4hash]
size = 64
while size > 1:
hashes = [hashlib.sha256(hashes[i] + hashes[i+1]).digest() for i in range(0, size, 2)]
size = int(size / 2)
return (cards, cards4hash, hashes[0])
if __name__ == '__main__':
cards, cards4hash, root = merckleTree()
print(cards, root.hex())
pragma solidity ^0.4.23;
import "./card.sol";
contract Poker {
mapping(address => CardLib.Card[2]) hands;
address house;
constructor() public {
house = msg.sender;
hands[house][0] = CardLib.num2card(15);
hands[house][1] = CardLib.num2card(25);
}
}
pragma solidity ^0.4.23;
contract Table {
uint blind;
uint minBuyIn;
address[] players;
uint8 playersOnline;
mapping(address => uint) balances;
mapping(address => uint) lockedBalances; //on hand
uint WithdrawRequestTimeout;
mapping(address => uint) balancesWithdrawRequests;
mapping(address => uint) balancesWithdrawTime;
event Deposit(address _from, uint value);
event WithdrawRequest(address _from, uint value);
event Withdraw(address _from, uint value);
uint handCount;
constructor(uint8 _maxPlayers, uint _blind, uint _minBuyIn, uint _WithdrawRequestTimeout) public payable {
require(_minBuyIn > 0);
require(_blind < _minBuyIn);
players.length = _maxPlayers;
balances.length = _maxPlayers;
minBuyIn = _minBuyIn;
blind = _blind;
players[0] = msg.sender;
balances[msg.sender] = msg.value;
WithdrawRequestTimeout = _WithdrawRequestTimeout;
playersOnline = 1;
}
function() payable {
if(balances[msg.sender] == 0){
// TODO
}
balances[msg.sender] += msg.value;
if (msg.value > 0)
Deposit(msg.sender, msg.value);
}
function sit() payable {
balances[msg.sender] += msg.value;
if (msg.value > 0)
Deposit(msg.sender, msg.value);
require(balances[msg.sender] > minBuyIn );
require(players[_id] == address(0));
players[playersOnline] = msg.sender;
playersOnline++;
}
}
pragma solidity ^0.4.23;
contract Test {
constructor() payable public {
}
function Withdraw() public {
selfdestruct(msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment