Skip to content

Instantly share code, notes, and snippets.

@ohadcn
Created May 15, 2018 21:13
Show Gist options
  • Save ohadcn/84d6b13f10f8955ab0d0456999355fe8 to your computer and use it in GitHub Desktop.
Save ohadcn/84d6b13f10f8955ab0d0456999355fe8 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 "./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;
mapping(address => uint) balances;
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);
constructor(uint8 _maxPlayers, uint _blind, uint _minBuyIn, uint _WithdrawRequestTimeout) public payable {
players.length = _maxPlayers;
balances.length = _maxPlayers;
minBuyIn = _minBuyIn;
blind = _blind;
players[0] = msg.sender;
balances[msg.sender] = msg.value;
WithdrawRequestTimeout = _WithdrawRequestTimeout;
}
function() payable {
if(balances[msg.sender] == 0){
}
balances[msg.sender] += msg.value;
if (msg.value > 0)
Deposit(msg.sender, msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment