1 package poker.business; 2 3 import java.util.Vector ; 4 import poker.spec.*; 5 16 public class DealerImpl implements Dealer{ 17 18 int[] e = { 0, 0, 0, 0, 0}; 19 public Vector dealFiveCards(){ 20 Vector thisHand = new Vector (5); 21 Vector usedCards = new Vector (); 22 for (int i = 0; i <5; i++){ 23 thisHand.addElement(new Integer (0)); 24 } 25 for (int i = 0; i <5; i++){ 26 thisHand = getUnusedCard(usedCards, thisHand, i); 27 } 28 return thisHand; 29 } 30 31 34 public Vector getUnusedCard(Vector usedCards, Vector theseCards,int 35 index){ 36 boolean shouldLoop = true; 37 boolean isUsed = false; 38 Integer aRandomCard = null; Integer thisCard = null; 39 40 while (shouldLoop){ 42 shouldLoop = false; 43 aRandomCard = new Integer (((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 56 public Vector drawMoreCards(Vector haveCards, Vector dropCards){ 57 Vector allCards = new Vector (0); 58 59 for (int i = 0; i < haveCards.size(); i++){ 62 allCards.addElement(haveCards.elementAt(i)); 63 } 64 String flag; 65 66 for (int i = 0; i < 5; i++){ 68 flag = (String )dropCards.elementAt(i); 69 if (flag.equals("DROP")){ 70 haveCards = getUnusedCard(haveCards,haveCards, i); 71 } 72 } 73 74 return haveCards; 75 } 76 77 81 public String checkHand(Vector cardsInHand){ 82 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 [] card = new Integer [5]; 93 int[] c = { 0, 0, 0, 0, 0}; 94 int[] d = { 0, 0, 0, 0, 0}; 95 96 for (int i = 0; i < 5; i++){ 98 card[i] = (Integer )cardsInHand.elementAt(i); c[i] = card[i].intValue(); d[i] = c[i]; while (d[i] > 13) { d[i] -= 13; } } 103 104 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 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 if ((two) & (three)){ 161 house = true; three = false; two = false; 162 scratch1 = 0; scratch2 = 0; scratch3 = 0; 163 } 164 165 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 if ((e[0] == 13) & (e[1] == 12) & 173 (e[2] == 11) & (e[3] == 10) & (e[4] == 1)) { 174 highStraight = true; 175 } 176 177 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 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 results = "Nothing"; 204 205 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 230 public int getPayout(String 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 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 |