1 // Open Hold em is a java-based, server and client environment for hosting
2 // your own texas hold poker tournaments, and regular tables. It is full
3 // customizable and deployable in a number of different java-based hosting
4 // environments.
5 // Copyright (C) 2005 Chris A. Mattmann <chris@baron.pagemewhen.com>
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // $Id: PokerEngine.java,v 1.4 2005/08/11 17:53:10 mattmann Exp $
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 // are we a royal flush, str8 flush, or just a regular flush?
78 if (h.sameSuit()) {
79 List handCards = h.getFCards();
80 // get the suit of one of the first card
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 // we're just a regular old straight
92 return Hand.STRAIGHT;
93 }
94
95 }
96
97 // are we a 4 of a kind?
98 if (h.isFourOfAKind()) {
99 return Hand.FOUR_KIND;
100 }
101
102 // if we get don't here and we haven't figured out the hand yet, then
103 // there's something wrong, and throw a hand exception
104 throw new HandException("PokerEngine:Can't figure out hand! Hand = "
105 + h);
106 }
107
108 }