/************************************************************************************* * from http://home.att.net/~gobruen/ * * * Tree52.java holds the cards and values * called by BlackJack.java * * * Class Tree52 is a binary tree with each node holding a key value for the node, * a string value that is the card, and the numerical value of the card. The * key in this case is passed from BlackJack.java and represents the location * of a card in a sorted deck. * * Methods: * insert() Places new nodes in the tree in a binary fashion * getval() Controls access to getVal() * getVal() returns the value of a card * getcard() Controls access to getCard() * getCard() returns the "face" of a card * makeCards() A small state machine is used to generate the four suits, one at a time * * * *************************************************************************************/ public class Tree52 { int key; String card; int value; Tree52 left; Tree52 right; Tree52(int k, String c, int v) { key = k; card = c; value = v; left = null; right = null; }//end Tree52() { left = null; right = null; }//end public void insert(int k, String c, int v) { if(k < key) { if(left == null) { left = new Tree52(k, c, v); } else { left.insert(k, c, v); } } else if(k > key) { if(right == null) { right = new Tree52(k, c, v); } else { right.insert(k, c, v); } } }//end insert /************ get the card value *******************/ int getval(int thekey){ int thevalue = getVal(thekey, 0); return thevalue; } /* * Traverse the tree and find a card value * */ int getVal(int thekey, int thevalue){ if(key == thekey){ thevalue = value; } if(left != null){ thevalue = left.getVal(thekey, thevalue); } if(right != null){ thevalue = right.getVal(thekey, thevalue); } return thevalue; } /*********** Get the card string ****************/ String getcard(int thekey){ String thecard = getCard(thekey, null); return thecard; } /* * Traverse the tree and find a card face * */ String getCard(int thekey, String thecard){ if(key == thekey){ thecard = card; } if(left != null){ thecard = left.getCard(thekey, thecard); } if(right != null){ thecard = right.getCard(thekey, thecard); } return thecard; } /************ make cards **********************/ /********************************************* * Card faces and values * A = Ace, J = Jack, Q = Queen, K = King * H = heart, D = diamond, C = club, S = spade * * Aces are assigned a value of 11, face cards 10 * all others take numerical values **********************************************/ public void makeCards(){ int cardNum = 1; String cardName = null; char suit = 'H';//start with hearts int cardVal = 0; for(int s = 1; s<5; s++){ for(int n = 1; n < 14; n++){ switch(n){ case 1: this.insert(cardNum, "A"+suit, 11); //aces break; case 11: this.insert(cardNum, "J"+suit, 10); //jacks break; case 12: this.insert(cardNum, "Q"+suit, 10); //queens break; case 13: this.insert(cardNum, "K"+suit, 10); //kings break; default: this.insert(cardNum, Integer.toString(n)+suit, n); break; } cardNum++; } switch(s){ case 1: suit = 'D';break; //diamonds case 2: suit = 'C';break; //clubs case 3: suit = 'S';break; //spades } } }//end makeCards /******* end methods *****************/ }//end class