poker odds
- From: davidstummer@xxxxxxxxx
- Date: 25 Sep 2005 15:01:17 -0700
Hi, i found some source code online for a holdem poker odds calculator.
i had a look at the code, and tried to copy and compile it. It
compiles, but doesn't give the answer i was expecting! bascially it's
for a java appletts which you can see here:
http://www.jbridge.net/jimmy/holdem_sim.htm
As you can see, a % probability shows in the top left of the applet.
What i need is the same code, so that when i supply hard-coded values
for the number of players, the cards etc, it will print to the console
the % probabilty. I have literally tried to work out the code for two
days without any luck. I was wondering if someone who knows abit more
about java could take a peak.
here is the code
poker.java: contains what i am after basically (the evaluate function i
think is the most important)
package cardgame;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
// Referenced classes of package cardgame:
// Card
public abstract class Poker
{
public static final int HIGH_CARD = 0;
public static final int PAIR = 1;
public static final int TWO_PAIRS = 2;
public static final int THREE_OF_A_KIND = 3;
public static final int STRAIGHT = 4;
public static final int FLUSH = 5;
public static final int FULL_HOUSE = 6;
public static final int FOUR_OF_A_KIND = 7;
public static final int STRAIGHT_FLUSH = 8;
public static final int WIN = 0;
public static final int TIE = 1;
public static final int LOSE = 2;
protected boolean cardTable[][];
protected int suitSum[];
protected int rankSum[];
protected int event[];
protected int totalEvent;
protected int rankList[];
protected int cardNum;
protected int maxSimulation;
protected int reportInterval;
protected boolean stopSimulation;
private Set cardSet;
private static Logger logger;
private static final int effectiveValues[] = {
5, 4, 3, 3, 1, 5, 2, 2, 1
};
private static String handType[] = {
"High Card", "One Pair", "Two Pairs", "Three of A Kind",
"Straight", "Flush", "Full House", "Four of A Kind", "Straight Flush"
};
public static class HandValue implements Comparable
{
public String toString()
{
StringBuffer buf = new StringBuffer(Poker.handType[type]);
for(int i = 0; i < Poker.effectiveValues[type]; i++)
buf.append(' ').append(Card.rank2text(values[i]));
return buf.toString();
}
public int compareTo(Object obj)
{
HandValue hv = (HandValue)obj;
if(type != hv.type)
return type - hv.type;
for(int i = 0; i < Poker.effectiveValues[type]; i++)
if(values[i] != hv.values[i])
return values[i] - hv.values[i];
return 0;
}
protected int type;
protected int values[];
public HandValue()
{
values = new int[5];
}
}
public Poker(int numCard, Card deck[])
{
cardTable = new boolean[4][13];
suitSum = new int[4];
rankSum = new int[13];
event = new int[3];
maxSimulation = 0x7fffffff;
reportInterval = 0x186a0;
cardSet = new HashSet();
rankList = new int[cardNum = numCard];
for(int i = 0; i < deck.length; i++)
cardSet.add(deck[i]);
}
public abstract void startSimulation(boolean flag);
protected abstract void report();
protected abstract void finished();
public void stopSimulation()
{
stopSimulation = true;
}
public void maxSimulation(int ms)
{
maxSimulation = ms;
}
public int maxSimulation()
{
return maxSimulation;
}
public void reportInterval(int ri)
{
reportInterval = ri;
}
public int reportInterval()
{
return reportInterval;
}
public int[] getEvent()
{
return event;
}
public void clearEvent()
{
event[0] = event[1] = event[2] = totalEvent = 0;
}
public void getEventProbability(double prob[])
{
prob[0] = (double)event[0] / (double)totalEvent;
prob[1] = (double)event[1] / (double)totalEvent;
prob[2] = 1.0D - prob[0] - prob[1];
}
protected void setCards(Card cards[])
{
for(int i = 0; i < cards.length; i++)
setCard(cards[i]);
}
protected void setCard(Card card)
{
cardTable[card.suit()][card.rank()] = true;
suitSum[card.suit()]++;
rankSum[card.rank()]++;
}
protected void removeCards(Card cards[])
{
for(int i = 0; i < cards.length; i++)
removeCard(cards[i]);
}
protected void removeCard(Card card)
{
cardTable[card.suit()][card.rank()] = false;
suitSum[card.suit()]--;
rankSum[card.rank()]--;
}
protected void holdCards(Card cards[])
{
for(int i = 0; i < cards.length; i++)
holdCard(cards[i]);
}
protected void holdCard(Card card)
{
if(cardSet.remove(card))
logger.fine("Card hold: " + card);
else
logger.warning("Card not in deck: " + card);
}
protected void unHoldCards(Card cards[])
{
for(int i = 0; i < cards.length; i++)
unHoldCard(cards[i]);
}
protected void unHoldCard(Card card)
{
if(cardSet.add(card))
logger.fine("Card unhold: " + card);
else
logger.warning("Card already in deck: " + card);
}
public Card[] getRemainingCards()
{
Card remain[] = new Card[cardSet.size()];
getRemainingCards(remain);
return remain;
}
public void getRemainingCards(Card remain[])
{
cardSet.toArray(remain);
}
protected int evaluate(HandValue hand)
{
int flushSuit = -1;
for(int i = 0; i < 4; i++)
{
if(suitSum[i] < 5)
continue;
flushSuit = i;
break;
}
int straight = 0;
int pair = 0;
int three = 0;
int four = 0;
int rankCount = 0;
for(int i = 12; i >= 0; i--)
{
switch(rankSum[i])
{
case 4: // '\004'
four++;
straight++;
rankList[rankCount++] = i;
break;
case 3: // '\003'
three++;
straight++;
rankList[rankCount++] = i;
break;
case 2: // '\002'
pair++;
straight++;
rankList[rankCount++] = i;
break;
case 1: // '\001'
straight++;
rankList[rankCount++] = i;
break;
default:
if(straight < 5)
straight = 0;
break;
}
if(i == 0 && straight == 4 && rankSum[12] > 0)
straight++;
}
if(straight >= 5 && flushSuit >= 0)
{
int straightflush = 0;
int highest = rankList[0];
int lowest = rankList[rankCount - 1];
for(int i = highest; i >= lowest && straightflush < 5; i--)
{
if(cardTable[flushSuit][i])
{
if(++straightflush == 1)
highest = i;
} else
{
straightflush = 0;
}
if(i == 0 && straightflush == 4 &&
cardTable[flushSuit][12])
straightflush++;
}
if(straightflush >= 5)
{
hand.type = 8;
hand.values[0] = highest;
return hand.type;
}
}
if(four > 0)
{
hand.type = 7;
for(int i = 0; i < rankCount; i++)
{
if(rankSum[rankList[i]] == 4)
{
hand.values[0] = rankList[i];
if(i == 0)
hand.values[1] = rankList[1];
break;
}
if(i == 0)
hand.values[1] = rankList[i];
}
} else
if(three > 1 || three > 0 && pair > 0)
{
hand.type = 6;
boolean threeDone = false;
boolean pairDone = false;
for(int i = 0; i < rankCount; i++)
{
if(rankSum[rankList[i]] == 3)
{
if(threeDone)
{
hand.values[1] = rankList[i];
break;
}
hand.values[0] = rankList[i];
if(pairDone)
break;
continue;
}
if(rankSum[rankList[i]] != 2 || pairDone)
continue;
hand.values[1] = rankList[i];
if(threeDone)
break;
pairDone = true;
}
} else
if(flushSuit >= 0)
{
hand.type = 5;
int copied = 0;
for(int rankId = 0; copied < 5; rankId++)
if(cardTable[flushSuit][rankList[rankId]])
hand.values[copied++] = rankList[rankId];
} else
if(straight >= 5)
{
hand.type = 4;
hand.values[0] = rankList[rankCount - 3];
for(int i = rankCount - 4; i >= 0; i--)
{
if(rankList[i] - hand.values[0] != 1)
break;
hand.values[0] = rankList[i];
}
} else
if(three > 0)
{
hand.type = 3;
int highCard = 0;
boolean threeFilled = false;
for(int i = 0; i < rankCount; i++)
{
if(rankSum[rankList[i]] == 3)
{
hand.values[0] = rankList[i];
if(highCard == 2)
break;
threeFilled = true;
continue;
}
if(highCard >= 2)
continue;
hand.values[++highCard] = rankList[i];
if(highCard == 2 && threeFilled)
break;
}
} else
if(pair > 1)
{
hand.type = 2;
int pairFilled = 0;
boolean lastFilled = false;
for(int i = 0; i < rankCount; i++)
{
if(rankSum[rankList[i]] == 2)
{
if(pairFilled < 2)
{
hand.values[pairFilled++] = rankList[i];
if(pairFilled == 2 && lastFilled)
break;
continue;
}
hand.values[2] = rankList[i];
break;
}
if(lastFilled)
continue;
hand.values[2] = rankList[i];
if(pairFilled == 2)
break;
lastFilled = true;
}
} else
if(pair > 0)
{
hand.type = 1;
int highcard = 0;
boolean pairFound = false;
for(int i = 0; i < rankCount; i++)
{
if(rankSum[rankList[i]] == 2)
{
hand.values[0] = rankList[i];
if(highcard == 3)
break;
pairFound = true;
continue;
}
if(highcard >= 3)
continue;
hand.values[++highcard] = rankList[i];
if(highcard == 3 && pairFound)
break;
}
} else
{
hand.type = 0;
for(int i = 0; i < 5; i++)
hand.values[i] = rankList[i];
}
//System.out.println("ht: " + hand.type);
return hand.type;
}
public int evaluateHand(Card hole[], HandValue hand)
{
setCards(hole);
int val = evaluate(hand);
removeCards(hole);
return val;
}
protected int registerEvent(HandValue my, HandValue op)
{
//System.out.println(event);
int comp = my.compareTo(op);
int e = 1;
if(comp < 0)
e = 2;
else
if(comp > 0)
e = 0;
event[e]++;
totalEvent++;
return e;
}
protected void registerEvent(int e)
{
event[e]++;
totalEvent++;
}
static
{
logger = Logger.getLogger(cardgame.Poker.class.getName());
}
}
oneholdem.java i think contains the functions which call the gui:
// Decompiled by DJ v3.8.8.85 Copyright 2005 Atanas Neshkov Date:
22/09/2005 15:07:13
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check
often for new version!
// Decompiler options: packimports(3)
// Source File Name: OneHoldEm.java
package view;
import cardgame.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.border.TitledBorder;
// Referenced classes of package view:
// BoardListener, Simculator, CardHolder, CardBoard,
// SpringUtilities, CardView
public class OneHoldEm extends JPanel
implements BoardListener, Simculator
{
private static final long serialVersionUID = 0xb3551d00c69d3aa0L;
private CardBoard board;
private CardHolder holder[];
private JTextField winScore;
private JTextField tieScore;
private JTextField losScore;
private JTextField potSize;
private JTextField unitSize;
private JTextField betSize;
private JButton resetButton;
private JButton simulButton;
private JSpinner pNumSpinner;
private JComboBox sNumBox;
private JProgressBar progressBar;
private JButton stopButton;
private CardLayout ctrlLayout;
private JPanel ctrlPanel;
private int inHolder;
private long startTime;
private ExpectedWin expectedWin;
private double pot;
private Card deck[];
private HoldEm holdEm;
private String progressView;
private String controlView;
private NumberFormat proForm;
private NumberFormat betForm;
private ActionListener cardReturner;
public OneHoldEm(Color bkground, Font defFont)
throws Exception
{
holder = new CardHolder[7];
winScore = new JTextField(8);
tieScore = new JTextField(8);
losScore = new JTextField(8);
potSize = new JTextField("0.00");
unitSize = new JTextField("0.05");
betSize = new JTextField();
resetButton = new JButton("Reset");
simulButton = new JButton("Simulate");
pNumSpinner = new JSpinner(new SpinnerNumberModel(2, 2, 10,
1));
sNumBox = new JComboBox(new String[] {
"Unlimited", "100,000", "1,000,000", "10,000,000"
});
progressBar = new JProgressBar();
stopButton = new JButton("Stop");
ctrlLayout = new CardLayout();
ctrlPanel = new JPanel(ctrlLayout);
expectedWin = new ExpectedWin(0.050000000000000003D);
deck = Card.createDeck();
holdEm = new HoldEm(deck) {
protected void report()
{
System.out.println(": " + event[0] + "\t" + event[1] +
"\t" + event[2] + "\t" + totalEvent);
double winProb = (double)event[0] / (double)totalEvent;
double tieProb = (double)event[1] / (double)totalEvent;
System.out.println("prob: " + winProb);
winScore.setText(proForm.format(winProb));
tieScore.setText(proForm.format(tieProb));
losScore.setText(proForm.format((float)event[2] /
(float)totalEvent));
int playerNum =
((Integer)pNumSpinner.getValue()).intValue();
expectedWin.setTable(pot, winProb, tieProb, playerNum);
double breakEven = expectedWin.breakEven();
System.out.println("Breakeven size: " + breakEven);
if(breakEven < 0.0D)
betSize.setText("Raise");
else
if(breakEven < expectedWin.unitBet())
betSize.setText("Fold");
else
betSize.setText("less than " +
betForm.format(breakEven));
if(maxSimulation > 0)
progressBar.setValue(totalEvent);
// else
// progressBar.setString(totalEvent);
}
protected void finished()
{
System.out.println("yo");
report();
long endTime = System.currentTimeMillis();
System.out.println("endTime - startTime");
ctrlLayout.show(ctrlPanel, controlView);
}
};
progressView = "progress";
controlView = "control";
proForm = NumberFormat.getPercentInstance();
betForm = NumberFormat.getInstance();
cardReturner = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
returnCard((CardHolder)e.getSource());
}
};
board = new CardBoard(deck, true, bkground);
board.flipMode(-1);
board.addBoardListener(this);
for(int i = 0; i < holder.length; i++)
{
holder[i] = new CardHolder();
holder[i].addActionListener(cardReturner);
}
((DecimalFormat)proForm).applyPattern("0.00%");
((DecimalFormat)betForm).applyPattern("0.00");
if(defFont != null)
{
resetButton.setFont(defFont);
simulButton.setFont(defFont);
stopButton.setFont(defFont);
}
buildGUI();
}
private void buildGUI()
{
setBorder(new TitledBorder("Hold'Em Odds Simulator"));
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < holder.length; i++)
returnCard(holder[i]);
winScore.setText("ert");
tieScore.setText("");
losScore.setText("");
potSize.setText("0.00");
pot = 0.0D;
}
});
simulButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
simulate();
}
});
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
holdEm.stopSimulation();
}
});
unitSize.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent focusevent)
{
}
public void focusLost(FocusEvent arg0)
{
try
{
double newUnit =
Double.parseDouble(unitSize.getText());
if(newUnit > 0.0D)
{
expectedWin.unitBet(newUnit);
System.out.println("New bet unit: " + newUnit);
return;
}
}
catch(NumberFormatException numberformatexception) { }
unitSize.setText((new
StringBuffer(String.valueOf(expectedWin.unitBet()))).toString());
}
});
potSize.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent focusevent)
{
}
public void focusLost(FocusEvent arg0)
{
try
{
double newPot =
Double.parseDouble(potSize.getText());
if(newPot >= 0.0D)
{
pot = newPot;
System.out.println("New pot size: " + newPot);
return;
}
}
catch(NumberFormatException numberformatexception) { }
potSize.setText((new
StringBuffer(String.valueOf(pot))).toString());
}
});
JPanel holePanel = new JPanel(new FlowLayout(0, 1, 1));
holePanel.setBorder(new TitledBorder("Hole"));
holePanel.add(holder[0]);
holePanel.add(holder[1]);
JPanel commPanel = new JPanel(new FlowLayout(0, 1, 1));
commPanel.setBorder(new TitledBorder("Community"));
commPanel.add(holder[2]);
commPanel.add(holder[3]);
commPanel.add(holder[4]);
commPanel.add(holder[5]);
commPanel.add(holder[6]);
Box butBar = Box.createHorizontalBox();
butBar.add(Box.createHorizontalGlue());
butBar.add(resetButton);
butBar.add(Box.createHorizontalGlue());
butBar.add(simulButton);
butBar.add(Box.createHorizontalGlue());
JPanel optBox = new JPanel(new SpringLayout());
optBox.add(new JLabel("# of simulation", 0));
optBox.add(new JLabel("players", 0));
optBox.add(sNumBox);
optBox.add(pNumSpinner);
SpringUtilities.makeCompactGrid(optBox, 2, 2, 0, 0, 0, 0);
JPanel ctrlBox = new JPanel(new BorderLayout());
ctrlBox.add(optBox, "North");
ctrlBox.add(butBar, "Center");
Box proPanel = Box.createVerticalBox();
proPanel.add(Box.createGlue());
proPanel.add(progressBar);
proPanel.add(Box.createVerticalStrut(3));
proPanel.add(stopButton);
stopButton.setAlignmentX(0.5F);
proPanel.add(Box.createVerticalStrut(3));
ctrlPanel.add(ctrlBox, controlView);
ctrlPanel.add(proPanel, progressView);
JPanel scorePanel = new JPanel(new SpringLayout());
scorePanel.add(new JLabel("Win: "));
scorePanel.add(winScore);
scorePanel.add(new JLabel("Tie: "));
scorePanel.add(tieScore);
scorePanel.add(new JLabel("Lose: "));
scorePanel.add(losScore);
scorePanel.add(new JLabel("Pot: "));
scorePanel.add(potSize);
scorePanel.add(new JLabel("Unit: "));
scorePanel.add(unitSize);
scorePanel.add(new JLabel("Bet? "));
scorePanel.add(betSize);
SpringUtilities.makeCompactGrid(scorePanel, 2, 6, 1, 1, 1, 1);
JPanel botPanel = new JPanel(new BorderLayout());
botPanel.add(holePanel, "West");
botPanel.add(commPanel, "East");
botPanel.add(ctrlPanel, "Center");
setLayout(new BorderLayout());
add(scorePanel, "North");
add(board, "Center");
add(botPanel, "South");
}
public void stop()
{
holdEm.stopSimulation();
}
protected void simulate()
{
if(!checkHolder())
return;
holdEm.clearTable();
holdEm.clearEvent();
holdEm.setPlayerNum(((Integer)pNumSpinner.getValue()).intValue());
int max = -1;
switch(sNumBox.getSelectedIndex())
{
case 1: // '\001'
max = 0x186a0;
break;
case 2: // '\002'
max = 0xf4240;
break;
case 3: // '\003'
max = 0x989680;
break;
default:
max = -1;
break;
}
holdEm.maxSimulation(max);
if(max > 0)
{
progressBar.setMaximum(max);
progressBar.setIndeterminate(false);
progressBar.setStringPainted(false);
} else
{
progressBar.setIndeterminate(true);
progressBar.setStringPainted(true);
}
holdEm.setHole(holder[0].getCard(), holder[1].getCard());
if(inHolder > 2)
holdEm.setFlop(holder[2].getCard(), holder[3].getCard(),
holder[4].getCard());
if(inHolder > 5)
holdEm.setTurn(holder[5].getCard());
if(inHolder > 6)
holdEm.setRiver(holder[6].getCard());
ctrlLayout.show(ctrlPanel, progressView);
startTime = System.currentTimeMillis();
holdEm.startSimulation(false);
}
private boolean checkHolder()
{
if(holder[0].isEmpty() || holder[1].isEmpty())
{
JOptionPane.showMessageDialog(this, "Need 2 hole cards");
return false;
}
if(inHolder > 2 && inHolder < 7)
switch(inHolder)
{
case 5: // '\005'
if(!holder[5].isEmpty() || !holder[6].isEmpty())
{
JOptionPane.showMessageDialog(this, "Need 3 flop
cards");
return false;
}
break;
case 6: // '\006'
if(!holder[6].isEmpty())
{
JOptionPane.showMessageDialog(this, "Need turn
card");
return false;
}
break;
default:
JOptionPane.showMessageDialog(this, "Need 3 flop
cards");
return false;
}
return true;
}
public void cardFlipped(CardBoard board, Card card, boolean faceUp)
{
if(!faceUp)
{
if(inHolder >= holder.length)
{
System.out.println("Holder full");
board.flip(card, 1);
return;
}
for(int i = 0; i < holder.length; i++)
{
if(!holder[i].isEmpty())
continue;
try
{
holder[i].setCard(card);
}
catch(Exception e)
{
e.printStackTrace();
}
inHolder++;
System.out.println(card + " put in holder " + i);
break;
}
}
}
private void returnCard(CardHolder holder)
{
if(!holder.isEmpty())
{
Card c = holder.removeCard();
inHolder--;
board.flip(c, 1);
System.out.println(c + " removed from holder");
}
}
public static void main(String args[])
throws Exception
{
File cimg[] = {
new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"),
new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"),
new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"),
new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"),
new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"), new File("cards.png"), new
File("cards.png"), new File("cards.png"),
new File("cards.png"), new File("cards.png")
};
CardView.loadIcons(cimg, null);
Color wizYellow = new Color(206, 231, 245);
UIManager.put("Panel.background", wizYellow);
Font defFont = new Font("Arial", 0, 12);
UIManager.put("TitledBorder.font", defFont);
UIManager.put("Label.font", defFont);
UIManager.put("ComboBox.font", defFont);
JFrame frame = new JFrame("One hole table test");
frame.setDefaultCloseOperation(3);
OneHoldEm ohe = new OneHoldEm(null, defFont);
frame.getContentPane().add(ohe, "Center");
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.show();
}
}
holdem extends poker , and cntains a startsimulatoin method which i
think is really important:
// Decompiled by DJ v3.8.8.85 Copyright 2005 Atanas Neshkov Date:
22/09/2005 15:07:12
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check
often for new version!
// Decompiler options: packimports(3)
// Source File Name: HoldEm.java
package cardgame;
// Referenced classes of package cardgame:
// Poker, Card, Dealer
public class HoldEm extends Poker
{
public static final int FLOP = 3;
public static final int TURN = 4;
public static final int RIVER = 5;
public static final int MAX_PLAYERS = 10;
private Card holes[][];
private Card flop[];
private Card turn;
private Card river;
private int playerNum;
private int needCommunity;
private Poker.HandValue myValue;
private Poker.HandValue opValue;
private Dealer dealer;
public HoldEm(Card deck[])
{
super(7, deck);
holes = new Card[10][2];
flop = new Card[3];
playerNum = 2;
needCommunity = 5;
myValue = new Poker.HandValue();
opValue = new Poker.HandValue();
dealer = new Dealer();
}
public void setPlayerNum(int pn)
{
playerNum = pn;
}
public void setHole(Card c1, Card c2)
{
holes[0][0] = c1;
holes[0][1] = c2;
holdCards(holes[0]);
}
public void setHole(Card h[])
{
setHole(h[0], h[1]);
}
public void setFlop(Card c1, Card c2, Card c3)
{
flop[0] = c1;
flop[1] = c2;
flop[2] = c3;
setCards(flop);
holdCards(flop);
needCommunity -= 3;
}
public void setFlop(Card f[])
{
setFlop(f[0], f[1], f[2]);
}
public void setTurn(Card c)
{
setCard(turn = c);
holdCard(c);
needCommunity--;
}
public void setRiver(Card c)
{
setCard(river = c);
holdCard(c);
needCommunity--;
}
public void clearTable()
{
if(flop[0] != null)
{
removeCards(flop);
unHoldCards(flop);
}
if(turn != null)
{
removeCard(turn);
unHoldCard(turn);
}
if(river != null)
{
removeCard(river);
unHoldCard(river);
}
if(holes[0][0] != null)
unHoldCards(holes[0]);
holes[0][0] = holes[0][1] = turn = river = null;
flop[0] = flop[1] = flop[2] = null;
needCommunity = 5;
}
public void startSimulation(boolean block)
{
stopSimulation = false;
dealer.resetShoe(getRemainingCards(), false);
(new Thread() {
public void run()
{
int simCount;
int reportCount;
int cardsPerRound;
clearEvent();
simCount = maxSimulation;
reportCount = reportInterval;
cardsPerRound = (playerNum - 1 << 1) + needCommunity;
_L2:
label0:
{
if(dealer.getRemainingSize() < cardsPerRound)
dealer.resetShoe(true);
fillCommunity(needCommunity);
evaluateHand(holes[0], myValue);
int result = 0;
for(int i = 1; i < playerNum; i++)
{
dealer.deal(holes[i]);
if(result != 2)
{
evaluateHand(holes[i], opValue);
int compare = myValue.compareTo(opValue);
if(compare == 0)
result = 1;
else
if(compare < 0)
result = 2;
}
}
registerEvent(result);
if(--reportCount <= 0)
{
report();
reportCount = reportInterval;
}
clearCommunity(needCommunity);
synchronized(HoldEm.this)
{
if(simCount > 0 && --simCount == 0)
stopSimulation = true;
if(!stopSimulation)
break label0;
}
//break; /* Loop/switch isn't completed */
}
holdem2;
JVM INSTR monitorexit ;
if(true) goto _L2; else goto _L1
_L1:
synchronized(HoldEm.this)
{
notifyAll();
}
finished();
return;
}
private void clearCommunity(int needMoreCards)
{
if(needMoreCards > 0)
{
removeCard(river);
river = null;
}
if(needMoreCards > 1)
{
removeCard(turn);
turn = null;
}
if(needMoreCards > 2)
{
removeCards(flop);
flop[0] = flop[1] = flop[2] = null;
}
}
private void fillCommunity(int needMoreCards)
{
if(needMoreCards > 0)
{
river = dealer.deal();
setCard(river);
}
if(needMoreCards > 1)
{
turn = dealer.deal();
setCard(turn);
}
if(needMoreCards > 2)
{
dealer.deal(flop);
setCards(flop);
}
}
// {
// super();
// }
}).start();
if(block)
synchronized(this)
{
try
{
wait();
}
catch(InterruptedException interruptedexception) { }
}
}
protected void report()
{
}
protected void finished()
{
}
}
there are some more classes, but i don't think they are necessary in
working it out. Cheers.
re:
here are some addiotonal files which may be needed
package cardgame;
public class Card
implements Comparable
{
private int suit;
private int rank;
private int index;
public Card(int s, int r)
{
suit = s;
rank = r;
index = s * 13 + r;
}
public int suit()
{
return suit;
}
public String suitString()
{
return suitString[suit];
}
public int rank()
{
return rank;
}
public String rankString()
{
return rankString[rank];
}
public int index()
{
return index;
}
public boolean isPair(Card c)
{
return rank == c.rank;
}
public boolean isSuited(Card c)
{
return suit == c.suit;
}
public String toString()
{
return suitString[suit] + rankString[rank];
}
public static Card createCard(String name)
{
if(name != null && name.length() == 2)
return new Card(getSuit(name.charAt(0)),
getRank(name.charAt(1)));
else
return null;
}
public static Card[] createDeck()
{
Card deck[] = new Card[52];
int i = 0;
int cardCount = 0;
for(; i < 4; i++)
{
for(int j = 0; j < 13; j++)
deck[cardCount++] = new Card(i, j);
}
return deck;
}
public static boolean isPair(Card c1, Card c2)
{
return c1.rank == c2.rank;
}
public static boolean isSuited(Card c1, Card c2)
{
return c1.suit == c2.suit;
}
public static String suit2text(int suit)
{
return suitString[suit];
}
public static String rank2text(int rank)
{
return rankString[rank];
}
public static int cardIndex(String name)
{
if(name != null && name.length() == 2)
return getSuit(name.charAt(0)) * 13 +
getRank(name.charAt(1));
else
return -1;
}
private static int getSuit(char sc)
{
switch(sc)
{
case 83: // 'S'
case 115: // 's'
return 0;
case 67: // 'C'
case 99: // 'c'
return 1;
case 68: // 'D'
case 100: // 'd'
return 2;
case 72: // 'H'
case 104: // 'h'
return 3;
}
return -1;
}
private static int getRank(char rc)
{
switch(rc)
{
case 50: // '2'
return 0;
case 51: // '3'
return 1;
case 52: // '4'
return 2;
case 53: // '5'
return 3;
case 54: // '6'
return 4;
case 55: // '7'
return 5;
case 56: // '8'
return 6;
case 57: // '9'
return 7;
case 84: // 'T'
case 116: // 't'
return 8;
case 74: // 'J'
case 106: // 'j'
return 9;
case 81: // 'Q'
case 113: // 'q'
return 10;
case 75: // 'K'
case 107: // 'k'
return 11;
case 65: // 'A'
case 97: // 'a'
return 12;
}
return -1;
}
public boolean equals(Object o)
{
if(o instanceof Card)
return index == ((Card)o).index;
else
return false;
}
public int hashCode()
{
return toString().hashCode();
}
public int compareTo(Object o)
{
return index - ((Card)o).index;
}
public static final int SPADE = 0;
public static final int CLUB = 1;
public static final int DIAMOND = 2;
public static final int HEART = 3;
public static final int TWO = 0;
public static final int THREE = 1;
public static final int FOUR = 2;
public static final int FIVE = 3;
public static final int SIX = 4;
public static final int SEVEN = 5;
public static final int EIGHT = 6;
public static final int NINE = 7;
public static final int TEN = 8;
public static final int JACK = 9;
public static final int QUEEN = 10;
public static final int KING = 11;
public static final int ACE = 12;
private static String suitString[] = {
"S", "C", "D", "H"
};
private static String rankString[] = {
"2", "3", "4", "5", "6", "7", "8", "9", "T", "J",
"Q", "K", "A"
};
private int suit;
private int rank;
private int index;
}
dealer:
package cardgame;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Random;
// Referenced classes of package cardgame:
// Card
public class Dealer
{
private Card shoe[];
private int dealt;
private Random random;
public Dealer()
{
random = new Random();
}
public int getShoeSize()
{
return shoe.length;
}
public int getRemainingSize()
{
return shoe.length - dealt;
}
public void resetShoe(Card deck[], boolean shuffle)
{
shoe = deck;
resetShoe(shuffle);
}
public void resetShoe(boolean shuffle)
{
dealt = 0;
if(shuffle)
shuffle();
}
private void shuffle()
{
if(shoe == null)
return;
for(int i = shoe.length - 1; i > 1; i--)
{
int swap = random.nextInt(i);
Card sc = shoe[swap];
shoe[swap] = shoe[i];
shoe[i] = sc;
}
}
public void sortShoe()
{
Arrays.sort(shoe);
}
public static void sortDeck(Card deck[])
{
Arrays.sort(deck);
}
public boolean deal(Card holder[])
{
if(shoe.length - holder.length < 0)
{
System.err.println("Not enough cards.");
return false;
}
for(int i = 0; i < holder.length;)
{
holder[i] = shoe[dealt];
i++;
dealt++;
}
return true;
}
public Card deal()
{
if(shoe.length == 0)
{
System.err.println("Not enough cards.");
return null;
} else
{
return shoe[dealt++];
}
}
}
package view;
import cardgame.Card;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.border.Border;
// Referenced classes of package view:
// CardView
public class CardHolder extends JButton
{
public CardHolder(boolean noborder, Color defbk)
{
if(noborder)
setBorder(emptyBorder);
if(defbk != null)
setBackground(defbk);
if(empty != null)
setMargin(empty);
}
public CardHolder()
{
this(false, null);
}
public CardHolder(Card c, boolean noborder, Color defbk)
throws Exception
{
this(noborder, defbk);
setCard(c);
}
public CardHolder(Card c)
throws Exception
{
this();
setCard(c);
}
public Card getCard()
{
return card;
}
public void setCard(Card c)
throws Exception
{
card = c;
setIcon(CardView.getIcon(c));
setMargin(noEdge);
if(empty == null)
{
Icon cardIcon = CardView.getIcon(c);
int h = cardIcon.getIconHeight() >> 1;
int w = cardIcon.getIconWidth() >> 1;
empty = new Insets(h, w, h, w);
}
}
public Card removeCard()
{
setIcon(null);
setMargin(empty);
Card c = card;
card = null;
return c;
}
public boolean isEmpty()
{
return card == null;
}
private static final long serialVersionUID = 0x813e3a79e1b09158L;
private static Insets empty;
private static Insets noEdge = new Insets(0, 0, 0, 0);
private static Border emptyBorder =
BorderFactory.createEmptyBorder();
private Card card;
}
cheers
.
- Follow-Ups:
- Re: poker odds
- From: Roedy Green
- Re: poker odds
- Prev by Date: Re: How do they search ?
- Next by Date: Re: Tomcat 5.5.9, correct directory for DB driver?
- Previous by thread: Entity EJBs vs Hibernate ?
- Next by thread: Re: poker odds
- Index(es):