001: package poker.business;
002:
003: import java.util.Vector;
004: import poker.spec.*;
005:
006: /**
007: *
008: * EnhyDraw!, beta 4, 5/21/99
009: *
010: * Copyright 1999, Larry Wolcot & Daryl Tempesta
011: * ALL rights reserved. Not for commercial use
012: * without written permission from both authors.
013: *
014: * This class handles all of the cardGame rules,
015: * distribution of cards, hand evaluation, etc.
016: */
017: public class DealerImpl implements Dealer {
018:
019: int[] e = { 0, 0, 0, 0, 0 };
020:
021: public Vector dealFiveCards() {
022: Vector this Hand = new Vector(5);
023: Vector usedCards = new Vector();
024: for (int i = 0; i < 5; i++) {
025: this Hand.addElement(new Integer(0));
026: }
027: for (int i = 0; i < 5; i++) {
028: this Hand = getUnusedCard(usedCards, this Hand, i);
029: }
030: return this Hand;
031: }
032:
033: /**
034: * Insert an unused card at index, into theseCards
035: */
036: public Vector getUnusedCard(Vector usedCards, Vector theseCards,
037: int index) {
038: boolean shouldLoop = true;
039: boolean isUsed = false;
040: Integer aRandomCard = null;
041: Integer this Card = null;
042:
043: //Loop until we draw a uique, unused card
044: while (shouldLoop) {
045: shouldLoop = false;
046: aRandomCard = new Integer(((int) (Math.random() * 52) + 1));
047: if (usedCards.contains(aRandomCard)) {
048: shouldLoop = true;
049: } else {
050: shouldLoop = false;
051: }
052: }
053: usedCards.addElement(aRandomCard);
054: theseCards.setElementAt(aRandomCard, index);
055:
056: return theseCards;
057: }
058:
059: /**
060: * This method drops the selected cards and draws new ones
061: */
062: public Vector drawMoreCards(Vector haveCards, Vector dropCards) {
063: Vector allCards = new Vector(0);
064:
065: //initialize a new Vector with the contents of another.
066: //Yeah, there are better ways to do it. Feeling liniar?
067: for (int i = 0; i < haveCards.size(); i++) {
068: allCards.addElement(haveCards.elementAt(i));
069: }
070: String flag;
071:
072: //draw a new card for every one select to drop
073: for (int i = 0; i < 5; i++) {
074: flag = (String) dropCards.elementAt(i);
075: if (flag.equals("DROP")) {
076: haveCards = getUnusedCard(haveCards, haveCards, i);
077: }
078: }
079:
080: return haveCards;
081: }
082:
083: /**
084: * This is one scary method. It checks the hand!
085: * Not bad work for a mathaphobic :)
086: */
087: public String checkHand(Vector cardsInHand) {
088: // init all of my boolean flags
089: boolean flush = false;
090: boolean lowStraight = false;
091: boolean highStraight = false;
092: boolean straightFlush = false;
093: boolean two = false;
094: boolean three = false;
095: boolean twoPair = false;
096: boolean four = false;
097: boolean house = false;
098: Integer[] card = new Integer[5];
099: int[] c = { 0, 0, 0, 0, 0 };
100: int[] d = { 0, 0, 0, 0, 0 };
101:
102: // set up cards and strip suits
103: for (int i = 0; i < 5; i++) {
104: card[i] = (Integer) cardsInHand.elementAt(i);// root card
105: c[i] = card[i].intValue(); // get int value
106: d[i] = c[i];// copy to d[] array for scratch work
107: while (d[i] > 13) {
108: d[i] -= 13;
109: }//strip suits from d[]
110: }
111:
112: // Sort from high to low, put results into e[]
113: for (int i = 0; i < 5; i++) {
114: if (d[i] >= e[0]) {
115: shift(e, 0);
116: e[0] = d[i];
117: }
118: if ((d[i] >= e[1]) & (d[i] < e[0])) {
119: shift(e, 1);
120: e[1] = d[i];
121: }
122: if ((d[i] >= e[2]) & (d[i] < e[1])) {
123: shift(e, 2);
124: e[2] = d[i];
125: }
126: if ((d[i] >= e[3]) & (d[i] < e[2])) {
127: shift(e, 3);
128: e[3] = d[i];
129: }
130: if ((d[i] >= e[4]) & (d[i] < e[3])) {
131: shift(e, 4);
132: e[4] = d[i];
133: }
134: }
135:
136: //Check for 2 and 3 of a kind
137: //This is one of the scariest sorts I've ever created! ;)
138: int scratch1 = 0;
139: int scratch2 = 0;
140: int scratch3 = 0;
141: for (int i = 0; i < 4; i++) {
142: for (int x = i + 1; x < 5; x++) {
143: if (e[x] == e[i]) {
144: if ((two == true) & (e[x] != scratch1)) {
145: if (e[x] != scratch3) {
146: twoPair = true;
147: scratch2 = e[x];
148: }
149: } else {
150: if (e[x] != scratch3) {
151: two = true;
152: scratch1 = e[x];
153: }
154: }
155: if (x < 4) {
156: for (int y = x + 1; y < 5; y++) {
157: if (e[y] == e[x]) {
158: three = true;
159: scratch3 = e[y];
160: if (e[y] == scratch1) {
161: two = false;
162: scratch1 = 0;
163: } else {
164: if (e[y] == scratch2) {
165: twoPair = false;
166: scratch2 = 0;
167: }
168: }
169: }
170: }
171: }
172: }
173: }
174: }
175:
176: //check for full house
177: if ((two) & (three)) {
178: house = true;
179: three = false;
180: two = false;
181: scratch1 = 0;
182: scratch2 = 0;
183: scratch3 = 0;
184: }
185:
186: //check for lowStraight
187: if ((e[0] == e[1] + 1) & (e[1] == e[2] + 1)
188: & (e[2] == e[3] + 1) & (e[3] == e[4] + 1)) {
189: lowStraight = true;
190: }
191:
192: //check for highStraight
193: if ((e[0] == 13) & (e[1] == 12) & (e[2] == 11) & (e[3] == 10)
194: & (e[4] == 1)) {
195: highStraight = true;
196: }
197:
198: //check for 4 of a kind
199: if (((e[0] == e[1]) & (e[1] == e[2]) & (e[2] == e[3]))
200: | ((e[4] == e[3]) & (e[3] == e[2]) & (e[2] == e[1]))) {
201: four = true;
202: three = false;
203: two = false;
204: scratch1 = 0;
205: scratch2 = 0;
206: }
207:
208: //check for flush in the 4 suits
209: if (((c[0] < 14) & (c[0] > 0)) && ((c[1] < 14) & (c[1] > 0))
210: && ((c[2] < 14) & (c[2] > 0))
211: && ((c[3] < 14) & (c[3] > 0))
212: && ((c[4] < 14) & (c[4] > 0))) {
213: flush = true;
214: }
215: if (((c[0] < 27) & (c[0] > 13)) && ((c[1] < 27) & (c[1] > 13))
216: && ((c[2] < 27) & (c[2] > 13))
217: && ((c[3] < 27) & (c[3] > 13))
218: && ((c[4] < 27) & (c[4] > 13))) {
219: flush = true;
220: }
221: if (((c[0] < 40) & (c[0] > 26)) && ((c[1] < 40) & (c[1] > 26))
222: && ((c[2] < 40) & (c[2] > 26))
223: && ((c[3] < 40) & (c[3] > 26))
224: && ((c[4] < 40) & (c[4] > 26))) {
225: flush = true;
226: }
227: if (((c[0] < 53) & (c[0] > 39)) && ((c[1] < 53) & (c[1] > 39))
228: && ((c[2] < 53) & (c[2] > 39))
229: && ((c[3] < 53) & (c[3] > 39))
230: && ((c[4] < 53) & (c[4] > 39))) {
231: flush = true;
232: }
233:
234: String results = "Nothing";
235:
236: //map boolean flags to english representation
237: if (flush) {
238: results = "Flush";
239: }
240: if (three) {
241: results = "Three of a kind";
242: }
243: if (twoPair) {
244: results = "Two pair";
245: } else {
246: if (two) {
247: if ((scratch1 == 1) | (scratch1 > 10)) {
248: results = "Pair of Jacks or better";
249: } else {
250: results = "A low pair";
251: }
252: }
253: }
254: if (house) {
255: results = "Full house";
256: }
257: if ((lowStraight) | (highStraight)) {
258: results = "Straight";
259: }
260: if (four) {
261: results = "Four of a kind";
262: }
263: if ((lowStraight) & (flush)) {
264: results = "Straight flush";
265: }
266: if ((highStraight) & (flush)) {
267: results = "Royal flush";
268: }
269:
270: return results;
271: }
272:
273: /**
274: * Return payout ration compared to 1 from english representation
275: */
276: public int getPayout(String hand) {
277: if (hand.equals("A low pair")) {
278: return 0;
279: }
280: if (hand.equals("Pair of Jacks or better")) {
281: return 1;
282: }
283: if (hand.equals("Two pair")) {
284: return 2;
285: }
286: if (hand.equals("Three of a kind")) {
287: return 4;
288: }
289: if (hand.equals("Straight")) {
290: return 5;
291: }
292: if (hand.equals("Flush")) {
293: return 9;
294: }
295: if (hand.equals("Full house")) {
296: return 15;
297: }
298: if (hand.equals("Four of a kind")) {
299: return 40;
300: }
301: if (hand.equals("Straight flush")) {
302: return 150;
303: }
304: if (hand.equals("Royal Flush")) {
305: return 400;
306: }
307:
308: return 0;
309: }
310:
311: /*
312: * Method used by one of the sorts to shift cards from right to left
313: */
314: private void shift(int[] f, int x) {
315: for (int i = 4; i > x; i--) {
316: f[i] = f[i - 1];
317: }
318: this.e = f;
319: }
320:
321: }
|