Open Side Menu Go to the Top

11-02-2007 , 01:03 PM
Has anyone got any java code related to poker?
I'm trying to model a game of holdem and know I'm not the first to do this in java.
Top of my agenda is to find a class that will work out the winning hand.

Anyone know where I should look for this?
Looking for some java code Quote
Looking for some java code
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Looking for some java code
11-02-2007 , 03:06 PM
Not sure how it rates speed wise. I think there is some code on sourceforge, too, but this is something I wrote from scratch a few days ago. (I ave some other code from a few years ago, but I decided to start from scratch for my current analysis efforts.)

<font class="small">Code:</font><hr /><pre> public class PokerHand
{

public static final short STRFLUSH = 8;
public static final short QUADS = 7;
public static final short FULLHOUSE = 6;
public static final short FLUSH = 5;
public static final short STRAIGHT = 4;
public static final short TRIPS = 3;
public static final short TWOPAIR = 2;
public static final short PAIR = 1;
public static final short HIGHCARD = 0;

private static final short[] MASKS = {0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
//for high card/flush hands. PH 2 3 4 5 6 7 8 9 T J Q K A

private static final short SHIFT = 13;
private static final short TWO_SHIFT = 169;
private static final short THREE_SHIFT = 2197;

private short handType;
private short handRank;
private short[] ranks = new short[14];
private boolean[] clubs = new boolean[14];
private boolean[] diamonds = new boolean[14];
private boolean[] hearts = new boolean[14];
private boolean[] spades = new boolean[14];
private boolean ranked = false;

private short numClubs = 0;
private short numDiamonds = 0;
private short numHearts = 0;
private short numSpades = 0;

/** Creates a new instance of PokerHand */
public PokerHand(int[] cards)
{
addCards(cards);
}

public void addCard(int card)
{
ranked = false;
int rank;
int suit;
rank = card % 13;
suit = card / 13;
ranks[rank]++;
ranks[13] = ranks[0];
switch(suit)
{
case 0:
clubs[rank] = true;
numClubs++;
clubs[13] = clubs[0];
break;
case 1:
diamonds[rank] = true;
numDiamonds++;
diamonds[13] = diamonds[0];
break;
case 2:
hearts[rank] = true;
numHearts++;
hearts[13] = hearts[0];
break;
case 3:
spades[rank] = true;
numSpades++;
spades[13] = spades[0];
break;
default:
System.out.println("Error in PokerHand.addCard: illegal suit");
System.exit(1);
}
}

public void addCards(int[] cards)
{
ranked = false;
int rank;
int suit;
for (int i = 0; i &lt; cards.length; i++)
{
rank = cards[i] % 13;
suit = cards[i] / 13;
ranks[rank]++;
switch(suit)
{
case 0:
clubs[rank] = true;
numClubs++;
break;
case 1:
diamonds[rank] = true;
numDiamonds++;
break;
case 2:
hearts[rank] = true;
numHearts++;
break;
case 3:
spades[rank] = true;
numSpades++;
break;
default:
System.out.println("Error in PokerHand.addCards: illegal suit");
System.exit(1);
}
}
clubs[13] = clubs[0];
diamonds[13] = diamonds[0];
hearts[13] = hearts[0];
spades[13] = spades[0];
ranks[13] = ranks[0];
}

private String rank(int i)
{
switch (i)
{
case 13:
case 0: return "A";
case 12: return "K";
case 11: return "Q";
case 10: return "J";
case 9: return "T";
case 8: return "9";
case 7: return "8";
case 6: return "7";
case 5: return "6";
case 4: return "5";
case 3: return "4";
case 2: return "3";
case 1: return "2";
}
return "Illegal Card Rank " + i;
}

public String printHand()
{
String ret = "";
for (int i = 13; i &gt; 0; i --)
{
if (clubs[i]) ret += rank(i) + "c ";
if (diamonds[i]) ret += rank(i) + "d ";
if (hearts[i]) ret += rank(i) + "h ";
if (spades[i]) ret += rank(i) + "s ";
}
return ret;
}

public String printDetailHand()
{
String ret = printHand() + "\tHand Type: ";
switch (handType)
{
case 8: ret += "Str Flush \t"; break;
case 7: ret += "Quads \t"; break;
case 6: ret += "Full House\t"; break;
case 5: ret += "Flush \t"; break;
case 4: ret += "Straight \t"; break;
case 3: ret += "Trips \t"; break;
case 2: ret += "Two Pair \t"; break;
case 1: ret += "One Pair \t"; break;
case 0: ret += "High Card \t"; break;
}
ret += "Rank: " + handRank;
return ret;
}

public void rank()
{
if (ranked) return;
ranked = true;
boolean flushPos = false;
if (numClubs &gt; 4 || numDiamonds &gt; 4 || numHearts &gt; 4 || numSpades &gt; 4) flushPos = true;
int count = 0;

//check for straight flush
if (flushPos)
{
if (numClubs &gt; 4)
{
for (short i = 13; i &gt;= 0; i--)
{
if(clubs[i]) count++;
else count = 0;
if (count == 5)
{
handType = STRFLUSH;
handRank = i;
return;
}
}
}
if (numDiamonds &gt; 4)
{
for (short i = 13; i &gt;= 0; i--)
{
if(diamonds[i]) count++;
else count = 0;
if (count == 5)
{
handType = STRFLUSH;
handRank = i;
return;
}
}
}
if (numHearts &gt; 4)
{
for (short i = 13; i &gt;= 0; i--)
{
if(hearts[i]) count++;
else count = 0;
if (count == 5)
{
handType = STRFLUSH;
handRank = i;
return;
}
}
}
if (numSpades &gt; 4)
{
for (short i = 13; i &gt;= 0; i--)
{
if(spades[i]) count++;
else count = 0;
if (count == 5)
{
handType = STRFLUSH;
handRank = i;
return;
}
}
}
}

//check for quads
short bestQuads = 0;
short bestKicker = 0;
for (short i = 13; i &gt; 0; i--)
{
if (ranks[i] == 4 &amp;&amp; bestQuads == 0) bestQuads = i;
else if (ranks[i] &gt; 0 &amp;&amp; bestKicker == 0) bestKicker = i;
}
if (bestQuads &gt; 0)
{
handType = QUADS;
handRank = (short) ((bestQuads * SHIFT) + bestKicker);
return;
}

//check for full house
short bestTrips = 0;
short pair = 0;
for (short i = 13; i &gt; 0; i--)
{
if (ranks[i] == 3 &amp;&amp; bestTrips == 0) bestTrips = i;
else if ( ranks[i] &gt; 1 &amp;&amp; pair == 0) pair = i;
}
if (bestTrips &gt; 0 &amp;&amp; pair &gt; 0)
{
handType = FULLHOUSE;
handRank = (short) ((bestTrips * SHIFT) + pair);
return;
}

//check for flush
if (flushPos)
{
count = 0;
handRank = 0;
handType = FLUSH;
if (numClubs &gt; 4)
{
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 5; i--)
{
if (clubs[i])
{
count++;
handRank += MASKS[i];
}
}
return;
}
if (numDiamonds &gt; 4)
{
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 5; i--)
{
if (diamonds[i])
{
count++;
handRank += MASKS[i];
}
}
return;
}
if (numHearts &gt; 4)
{
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 5; i--)
{
if (hearts[i])
{
count++;
handRank += MASKS[i];
}
}
return;
}
if (numSpades &gt; 4)
{
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 5; i--)
{
if (spades[i])
{
count++;
handRank += MASKS[i];
}
}
return;
}
}
//check for straight
for (short i = 13; i &gt;= 0; i--)
{
if (ranks[i] &gt; 0) count++;
else count = 0;
if (count == 5)
{
handRank = i;
handType = STRAIGHT;
return;
}
}

//check for trips
bestTrips = 0;
for (short i = 13; i &gt; 0; i--)
{
if (ranks[i] == 3)
{
bestTrips = i;
break;
}
}
if (bestTrips &gt; 0)
{
count = 0;
handType = TRIPS;
handRank = (short) (bestTrips * TWO_SHIFT);
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 2; i--)
{
if (ranks[i] &gt; 0 &amp;&amp; i != bestTrips)
{
count++;
if (count == 1) handRank += (short) (i * SHIFT);
else handRank += i;
}
}
return;
}

//check for two pair
short topPair = 0;
short botPair = 0;
short kicker = 0;
for (short i = 13; i &gt; 0; i--)
{
if (ranks[i] == 2 &amp;&amp; topPair == 0) topPair = i;
else if (ranks[i] == 2 &amp;&amp; botPair == 0) botPair = i;
else if (ranks[i] &gt; 0 &amp;&amp; kicker == 0) kicker = i;
}
if (botPair &gt; 0)
{
handType = TWOPAIR;
handRank = (short) ((topPair*TWO_SHIFT) + (botPair*SHIFT) + kicker);
return;
}

//check for one pair
if (topPair &gt; 0)
{
handType = PAIR;
handRank = (short) (topPair * THREE_SHIFT);
count = 0;
for (short i = 13; i &gt; 0; i --)
{
if (i == topPair) continue;
if (ranks[i] == 1)
{
count ++;
if (count == 1) handRank += (short) (i * TWO_SHIFT);
else if (count == 2) handRank += (short) (i * SHIFT);
else
{
handRank += i;
return;
}
}
}
}

//it's a high card
handType = HIGHCARD;
handRank = 0;
count = 0;
for (short i = 13; i &gt; 0 &amp;&amp; count &lt; 5; i--)
{
if (ranks[i] == 1)
{
count++;
handRank += MASKS[i];
}
}
}

public int compare(PokerHand ph)
{
//if we are higher than ph, return 1, tied return 0, losing return -1
if (!ranked) rank();
if (handType &gt; ph.getHandType()) return 1;
else if (ph.getHandType() &gt; handType) return -1;
else if (handRank &gt; ph.getHandRank()) return 1;
else if (ph.getHandRank() &gt; handRank) return -1;
else return 0;
}

public short getHandType()
{
rank();
return handType;
}

public short getHandRank()
{
rank();
return handRank;
}
} </pre><hr />
Looking for some java code Quote
11-02-2007 , 03:09 PM
I don't implement comparator at this point, because I am not decided how I want to implement equals and I am not sure if I even have a need to, but I do have the compare function written for determining if a hand beats another.
Looking for some java code Quote
11-03-2007 , 05:33 PM
Thanks a lot for that. I found this code which I'm working with. It seems to work very well.
http://spaz.ca/poker/doc/
http://spaz.ca/poker/UofAHandEval.zip
Looking for some java code Quote
11-04-2007 , 02:14 PM
Thanks Indiana. Do you know if there is any code relating to the game play out there?
Looking for some java code Quote
11-04-2007 , 02:18 PM
What do you mean by game play?

If this is what I think of, you can search for older versions of Poki (which is java) in the canadian yahoo poker research usergroup.

I have this downloded and can save you some hassle, PM me as i don't want get benned for marketing and other issues
Looking for some java code Quote
11-04-2007 , 02:22 PM
I mean dealing the cards to the players, accepting bets, awarding pots etc. I'll send you a pm in a few mins.
Looking for some java code Quote
11-04-2007 , 02:27 PM
Quote:
I mean dealing the cards to the players, accepting bets, awarding pots etc. I'll send you a pm in a few mins.
Agh OK -- for this there is no problem. You can take Alberta's software that they use to run their contest. I don't know if it's open source and so on, but the source code is available (since last year it supports NL headsup but there is no ring support yet, it's considered for this year competition).

Start here (there is direct download, but look also at their forums)
http://www.cs.ualberta.ca/~pokert/
Looking for some java code Quote
Looking for some java code
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Looking for some java code

      
m