1 /** ************************************************************************* 2 * Copyright (c) 2000: 3 * University of Alberta, 4 * Deptartment of Computing Science 5 * Computer Poker Research Group 6 * 7 * See "Liscence.txt" 8 ************************************************************************** */ 9 package org.alberta.poker; 10 11 import java.util.StringTokenizer; 12 13 /** 14 * Stores a Hand of Cards (up to a maximum of 7) 15 * 16 * @author Aaron Davidson 17 * @version 1.1.0 18 */ 19 public class Hand { 20 21 public final static int MAX_CARDS = 7; 22 23 private int[] cards; 24 Hand()25 public Hand() { 26 cards = new int[MAX_CARDS + 1]; 27 cards[0] = 0; 28 } 29 30 /** 31 * @param cs A string representing a Hand of cards 32 */ Hand(String cs)33 public Hand(String cs) { 34 cards = new int[MAX_CARDS + 1]; 35 cards[0] = 0; 36 StringTokenizer t = new StringTokenizer(cs, " -"); 37 while (t.hasMoreTokens()) { 38 String s = t.nextToken(); 39 if (s.length() == 2) { 40 Card c = new Card(s.charAt(0), s.charAt(1)); 41 if (c.getIndex() != Card.BAD_CARD) { 42 addCard(c); 43 } 44 } 45 } 46 } 47 48 /** 49 * Duplicate an existing hand. 50 * 51 * @param h the hand to clone. 52 */ Hand(Hand h)53 public Hand(Hand h) { 54 cards = new int[MAX_CARDS + 1]; 55 cards[0] = h.size(); 56 for (int i = 1; i <= cards[0]; i++) { 57 cards[i] = h.cards[i]; 58 } 59 } 60 61 /** 62 * Get the size of the hand. 63 * 64 * @return the number of cards in the hand 65 */ size()66 public int size() { 67 return cards[0]; 68 } 69 70 /** 71 * Remove the last card in the hand. 72 */ removeCard()73 public void removeCard() { 74 if (cards[0] > 0) { 75 cards[0]--; 76 } 77 } 78 79 /** 80 * Remove the all cards from the hand. 81 */ makeEmpty()82 public void makeEmpty() { 83 cards[0] = 0; 84 } 85 86 /** 87 * Add a card to the hand. (if there is room) 88 * 89 * @param c the card to add 90 * @return true if the card was added, false otherwise 91 */ addCard(Card c)92 public boolean addCard(Card c) { 93 if (c == null) { 94 return false; 95 } 96 if (cards[0] == MAX_CARDS) { 97 return false; 98 } 99 cards[0]++; 100 cards[cards[0]] = c.getIndex(); 101 return true; 102 } 103 104 /** 105 * Add a card to the hand. (if there is room) 106 * 107 * @param i the index value of the card to add 108 * @return true if the card was added, false otherwise 109 */ addCard(int i)110 public boolean addCard(int i) { 111 if (cards[0] == MAX_CARDS) { 112 return false; 113 } 114 cards[0]++; 115 cards[cards[0]] = i; 116 return true; 117 } 118 119 /** 120 * Get the a specified card in the hand 121 * 122 * @param pos the position (1..n) of the card in the hand 123 * @return the card at position pos 124 */ getCard(int pos)125 public Card getCard(int pos) { 126 if (pos < 1 || pos > cards[0]) { 127 return null; 128 } 129 return new Card(cards[pos]); 130 } 131 132 /** 133 * Add a card to the hand. (if there is room) 134 * 135 * @param c the card to add 136 * @return true if the card was added, false otherwise 137 */ setCard(int pos, Card c)138 public void setCard(int pos, Card c) { 139 if (cards[0] < pos) { 140 return; 141 } 142 cards[pos] = c.getIndex(); 143 } 144 145 /** 146 * Obtain the array of card indexes for this hand. First element contains 147 * the size of the hand. 148 * 149 * @return array of card indexs (size = MAX_CARDS+1) 150 */ getCardArray()151 public int[] getCardArray() { 152 return cards; 153 } 154 155 /** 156 * Bubble Sort the hand to have cards in descending order, but card index. 157 * Used for database indexing. 158 */ sort()159 public void sort() { 160 boolean flag = true; 161 while (flag) { 162 flag = false; 163 for (int i = 1; i < cards[0]; i++) { 164 if (cards[i] < cards[i + 1]) { 165 flag = true; 166 int t = cards[i]; 167 cards[i] = cards[i + 1]; 168 cards[i + 1] = t; 169 } 170 } 171 } 172 } 173 174 /** 175 * Get a string representation of this Hand. 176 */ toString()177 public String toString() { 178 String s = new String(); 179 for (int i = 1; i <= cards[0]; i++) { 180 s += " " + getCard(i).toString(); 181 } 182 return s; 183 } 184 } 185