1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package com.openholdem.engine;
24
25 import com.openholdem.structs.Hand;
26 import com.openholdem.structs.Card;
27 import com.openholdem.structs.Deck;
28 import com.openholdem.structs.RuleSet;
29 import com.openholdem.util.RuleSetReader;
30 import com.openholdem.structs.exceptions.HandException;
31 import com.openholdem.structs.exceptions.PokerException;
32
33 import java.net.URL;
34
35 import java.util.List;
36
37 /***
38 * @author mattmann
39 * @version $Revision: 1.4 $
40 *
41 * <p>
42 * A Poker Engine is a rule engine that evalutes {@link Hand}s.
43 * </p>
44 *
45 */
46 public class PokerEngine {
47
48 private RuleSet ruleSet = null;
49
50 private Deck theDeck = null;
51
52 /***
53 * <p>
54 * Constructs a new poker engine with the rule set specified in
55 * <code>rs</code>.
56 * </p>
57 *
58 * @param rs
59 * a URL pointing to the rule set xml file.
60 */
61 public PokerEngine(URL rs) throws PokerException {
62 try {
63 ruleSet = new RuleSetReader(rs).readRuleSet();
64 } catch (PokerException e) {
65 e.printStackTrace();
66 throw e;
67 } catch (Exception e) {
68 e.printStackTrace();
69 throw new PokerException(e.getMessage());
70 }
71
72 theDeck = new Deck();
73 }
74
75 public String getHandString(Hand h) throws HandException {
76
77
78 if (h.sameSuit()) {
79 List handCards = h.getFCards();
80
81 String suit = ((Card[]) (handCards.toArray(new Card[] {})))[0]
82 .getSuit();
83
84 if (h.isStraight()
85 && handCards.contains(theDeck.getCard("K", suit))
86 && handCards.contains(theDeck.getCard("A", suit))) {
87 return Hand.ROYAL_FLUSH;
88 } else if (h.isStraight()) {
89 return Hand.STRAIGHT_FLUSH;
90 } else {
91
92 return Hand.STRAIGHT;
93 }
94
95 }
96
97
98 if (h.isFourOfAKind()) {
99 return Hand.FOUR_KIND;
100 }
101
102
103
104 throw new HandException("PokerEngine:Can't figure out hand! Hand = "
105 + h);
106 }
107
108 }