KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > business > DealerImpl


1 package poker.business;
2
3 import java.util.Vector JavaDoc;
4 import poker.spec.*;
5 /**
6  *
7  * EnhyDraw!, beta 4, 5/21/99
8  *
9  * Copyright 1999, Larry Wolcot & Daryl Tempesta
10  * ALL rights reserved. Not for commercial use
11  * without written permission from both authors.
12  *
13  * This class handles all of the cardGame rules,
14  * distribution of cards, hand evaluation, etc.
15  */

16 public class DealerImpl implements Dealer{
17
18     int[] e = { 0, 0, 0, 0, 0};
19     public Vector JavaDoc dealFiveCards(){
20     Vector JavaDoc thisHand = new Vector JavaDoc(5);
21     Vector JavaDoc usedCards = new Vector JavaDoc();
22     for (int i = 0; i <5; i++){
23         thisHand.addElement(new Integer JavaDoc(0));
24     }
25     for (int i = 0; i <5; i++){
26          thisHand = getUnusedCard(usedCards, thisHand, i);
27     }
28     return thisHand;
29     }
30
31    /**
32     * Insert an unused card at index, into theseCards
33     */

34     public Vector JavaDoc getUnusedCard(Vector JavaDoc usedCards, Vector JavaDoc theseCards,int
35 index){
36     boolean shouldLoop = true;
37     boolean isUsed = false;
38     Integer JavaDoc aRandomCard = null; Integer JavaDoc thisCard = null;
39
40     //Loop until we draw a uique, unused card
41
while (shouldLoop){
42       shouldLoop = false;
43       aRandomCard = new Integer JavaDoc(((int)(Math.random()*52)+1));
44       if (usedCards.contains(aRandomCard)) { shouldLoop = true;
45       } else { shouldLoop = false; }
46     }
47     usedCards.addElement(aRandomCard);
48     theseCards.setElementAt(aRandomCard, index);
49
50     return theseCards;
51     }
52
53     /**
54      * This method drops the selected cards and draws new ones
55      */

56     public Vector JavaDoc drawMoreCards(Vector JavaDoc haveCards, Vector JavaDoc dropCards){
57     Vector JavaDoc allCards = new Vector JavaDoc(0);
58
59     //initialize a new Vector with the contents of another.
60
//Yeah, there are better ways to do it. Feeling liniar?
61
for (int i = 0; i < haveCards.size(); i++){
62         allCards.addElement(haveCards.elementAt(i));
63     }
64     String JavaDoc flag;
65
66     //draw a new card for every one select to drop
67
for (int i = 0; i < 5; i++){
68         flag = (String JavaDoc)dropCards.elementAt(i);
69         if (flag.equals("DROP")){
70         haveCards = getUnusedCard(haveCards,haveCards, i);
71         }
72     }
73
74     return haveCards;
75     }
76     
77     /**
78      * This is one scary method. It checks the hand!
79      * Not bad work for a mathaphobic :)
80      */

81     public String JavaDoc checkHand(Vector JavaDoc cardsInHand){
82     // init all of my boolean flags
83
boolean flush = false;
84     boolean lowStraight = false;
85     boolean highStraight = false;
86     boolean straightFlush = false;
87     boolean two = false;
88     boolean three = false;
89     boolean twoPair = false;
90     boolean four = false;
91     boolean house = false;
92     Integer JavaDoc[] card = new Integer JavaDoc[5];
93     int[] c = { 0, 0, 0, 0, 0};
94         int[] d = { 0, 0, 0, 0, 0};
95
96     // set up cards and strip suits
97
for (int i = 0; i < 5; i++){
98         card[i] = (Integer JavaDoc)cardsInHand.elementAt(i);// root card
99
c[i] = card[i].intValue(); // get int value
100
d[i] = c[i];// copy to d[] array for scratch work
101
while (d[i] > 13) { d[i] -= 13; }//strip suits from d[]
102
}
103
104     // Sort from high to low, put results into e[]
105
for (int i = 0; i < 5; i++){
106         if (d[i] >= e[0]) {
107         shift(e, 0); e[0] = d[i];
108         }
109         if ((d[i] >= e[1]) & (d[i] < e[0])) {
110         shift(e, 1); e[1] = d[i];
111         }
112             if ((d[i] >= e[2]) & (d[i] < e[1])) {
113         shift(e, 2); e[2] = d[i];
114         }
115             if ((d[i] >= e[3]) & (d[i] < e[2])) {
116         shift(e, 3); e[3] = d[i];
117         }
118             if ((d[i] >= e[4]) & (d[i] < e[3])) {
119         shift(e, 4); e[4] = d[i];
120         }
121     }
122
123     //Check for 2 and 3 of a kind
124
//This is one of the scariest sorts I've ever created! ;)
125
int scratch1 = 0; int scratch2 = 0; int scratch3 = 0;
126     for (int i = 0; i <4; i++){
127         for (int x = i + 1; x < 5; x++){
128             if (e[x] == e[i]){
129             if ((two == true) & (e[x] != scratch1)){
130             if (e[x] != scratch3){
131                 twoPair = true;
132                 scratch2 = e[x];
133             }
134             } else {
135             if (e[x] != scratch3){
136                     two = true;
137                     scratch1 = e[x];
138             }
139             }
140             if (x < 4){
141                 for (int y = x + 1; y < 5; y++){
142                     if(e[y] == e [x]){
143                         three = true;
144                 scratch3 = e[y];
145                 if (e[y] == scratch1){
146                         two = false; scratch1 = 0;
147                 } else {
148                     if (e[y] == scratch2){
149                     twoPair = false; scratch2 = 0;
150                 }
151                 }
152                     }
153                 }
154             }
155         }
156         }
157     }
158
159     //check for full house
160
if ((two) & (three)){
161         house = true; three = false; two = false;
162         scratch1 = 0; scratch2 = 0; scratch3 = 0;
163     }
164
165     //check for lowStraight
166
if ((e[0] == e[1] + 1) & (e[1] == e[2] + 1) &
167         (e[2] == e[3] + 1) & (e[3] == e[4] + 1)) {
168             lowStraight = true;
169         }
170         
171         //check for highStraight
172
if ((e[0] == 13) & (e[1] == 12) &
173             (e[2] == 11) & (e[3] == 10) & (e[4] == 1)) {
174             highStraight = true;
175         }
176
177     //check for 4 of a kind
178
if ( ((e[0] == e[1]) & (e[1] == e[2]) & (e[2] == e[3])) |
179       ((e[4] == e[3]) & (e[3] == e[2]) & (e[2] == e[1])) ) {
180         four = true;
181         three = false; two = false;
182         scratch1 = 0; scratch2 = 0;
183     }
184
185     //check for flush in the 4 suits
186
if ( ((c[0] < 14) & (c[0] > 0)) && ((c[1] < 14) & (c[1] > 0))
187         && ((c[2] < 14) & (c[2] > 0)) && ((c[3] < 14) & (c[3]>0))
188         && ((c[4] < 14) & (c[4] > 0)) ){
189     flush = true; }
190         if ( ((c[0] < 27) & (c[0] > 13)) && ((c[1] < 27) & (c[1] > 13))
191                 && ((c[2] < 27) & (c[2] > 13)) && ((c[3] <27) & (c[3]>13))
192                 && ((c[4] < 27) & (c[4] > 13)) ){
193         flush = true; }
194         if ( ((c[0] < 40) & (c[0] > 26)) && ((c[1] < 40) & (c[1] > 26))
195                 && ((c[2] < 40) & (c[2] >26)) && ((c[3] <40) & (c[3]>26))
196                 && ((c[4] < 40) & (c[4] > 26)) ){
197         flush = true; }
198         if ( ((c[0] < 53) & (c[0] > 39)) && ((c[1] < 53) & (c[1] > 39))
199                 && ((c[2] < 53) & (c[2] >39)) && ((c[3]< 53) & (c[3]>39))
200                 && ((c[4] < 53) & (c[4] > 39)) ){
201         flush = true; }
202
203     String JavaDoc results = "Nothing";
204
205     //map boolean flags to english representation
206
if (flush) { results = "Flush"; }
207     if (three) { results = "Three of a kind"; }
208     if (twoPair) { results = "Two pair";
209     } else {
210             if (two) {
211         if ((scratch1 == 1) | (scratch1 > 10)){
212              results = "Pair of Jacks or better";
213             } else {
214              results = "A low pair";
215         }
216         }
217     }
218     if (house){ results = "Full house";}
219     if ((lowStraight) | (highStraight)){ results = "Straight";}
220     if (four){ results = "Four of a kind"; }
221     if ((lowStraight) & (flush)) { results = "Straight flush";}
222     if ((highStraight) & (flush)) { results = "Royal flush";}
223
224     return results;
225     }
226
227    /**
228     * Return payout ration compared to 1 from english representation
229     */

230     public int getPayout(String JavaDoc hand){
231     if (hand.equals("A low pair")) { return 0;}
232         if (hand.equals("Pair of Jacks or better")) { return 1;}
233         if (hand.equals("Two pair")) { return 2;}
234         if (hand.equals("Three of a kind")) { return 4;}
235         if (hand.equals("Straight")) { return 5;}
236         if (hand.equals("Flush")) { return 9;}
237         if (hand.equals("Full house")) { return 15;}
238         if (hand.equals("Four of a kind")) { return 40;}
239         if (hand.equals("Straight flush")) { return 150;}
240         if (hand.equals("Royal Flush")) { return 400;}
241
242     return 0;
243     }
244
245    /*
246     * Method used by one of the sorts to shift cards from right to left
247     */

248     private void shift(int[] f, int x){
249         for (int i = 4; i > x; i--){
250             f[i] = f[i - 1];
251     }
252     this.e = f;
253     }
254
255 }
256
Popular Tags