/************************************************************************** * from http://home.att.net/~gobruen/ * * *BlackJack.java * * Called by GameDriver.java, needs Tree52.java to run * * * Methods: * * blackjack() main driver for game * newDeck() creates a "deck" which is the array passed to shuffle() * shuffle() takes an inorder array and randomizes the order * isNum() boolean determines if entry is a number * showHand() displays a "hand" passed to it which is an array of card valeus * cleanHand() "clears" the hands at the table when a game is over, empties array * introduction() intro script calls bjStart() * bjStart() ascii art * help() general help * ****************************************************************************/ import java.lang.Math.*; import java.io.*; import java.util.*; public class BlackJack { public static int blackjack(int bank, String player)throws IOException{ System.out.println("\n\n\n\n"); introduction(); BufferedReader clin = new BufferedReader(new InputStreamReader(System.in));//setup for command line reading int d[] = newDeck(); shuffle(d); Tree52 cards = new Tree52(); cards.makeCards(); String action = "start"; int bet = 0; int playerHand = 0; int dealerHand = 0; int maxH = 12; int pHcnt = 0; int dHcnt = 0; String pHand[] = new String[maxH]; String dHand[] = new String[maxH]; int cardCount = 1; int state = 1; final int BET = 1; final int PLAY1 = 2; final int PLAY2 = 3; final int DBUST = 4; final int PBUST = 5; final int DWIN = 6; final int PWIN = 7; final int PUSH = 8; final int SHUFFLE = 9; while(!action.equals("leave")){ switch(state){ case BET: bet = 0; playerHand = 0; dealerHand = 0; pHcnt = 0; dHcnt = 0; pHand = cleanHand(pHand); dHand = cleanHand(dHand); System.out.println("The dealer is staring at you and tapping the deck"); System.out.print("["+player+", "+bank+"]Action: "); action = clin.readLine(); if(action.length()>0){ action = action.toLowerCase(); if(isNum(action)){ bet = Integer.parseInt(action.trim()); if(bet<=bank){ if((bet>=5)&&(bet<=500)){ bank = bank - bet; state = PLAY1; }else{ System.out.println("Table minimum is $5, limit is $500"); state = BET; } }else{ System.out.println("Your bank is "+bank+" and you're trying to bet "+bet); state = BET; } }else{ if(action.equals("hit")||action.equals("stay")){ System.out.println("You can only do that after you've placed a bet and the cards are dealt."); }else{ if(action.equals("help")){ help(); }else{ System.out.println("The dealer looks bored"); } } }//end bet if } break; case PLAY1: System.out.print("---------- New deal ----------"); System.out.print("\nYou get: "); System.out.print(cards.getcard(d[cardCount])); pHand[pHcnt] = cards.getcard(d[cardCount]); pHcnt++; playerHand = cards.getval(d[cardCount]); cardCount++; System.out.print("\nDealer gets: "); System.out.print(cards.getcard(d[cardCount])); dHand[dHcnt] = cards.getcard(d[cardCount]); dHcnt++; dealerHand = cards.getval(d[cardCount]); cardCount++; System.out.print("\nYou get: "); System.out.print(cards.getcard(d[cardCount])); pHand[pHcnt] = cards.getcard(d[cardCount]); pHcnt++; if((playerHand == 11)&&(cards.getval(d[cardCount]) == 11)){ playerHand = playerHand + 1; }else{ playerHand = playerHand + cards.getval(d[cardCount]); } cardCount++; System.out.print("\nDealer gets: "); System.out.print(cards.getcard(d[cardCount])); dHand[dHcnt] = cards.getcard(d[cardCount]); dHcnt++; if((dealerHand == 11)&&(cards.getval(d[cardCount]) == 11)){ dealerHand = dealerHand + 1; }else{ dealerHand = dealerHand + cards.getval(d[cardCount]); } cardCount++; System.out.print("\n\nPlayer hand: "); showHand(pHand); System.out.print("Dealer hand: "); showHand(dHand); if((playerHand==21)&&(dealerHand==21)){ state = PUSH; }else{ if(playerHand==21){ state = PWIN; }else{ if(dealerHand==21){ state = DWIN; }else{ state = PLAY2; } } } break; case PLAY2: if(playerHand<=20){ System.out.print("Hit or Stay\n"); System.out.print("["+player+", "+bank+"]Action: "); action = clin.readLine(); action = action.toLowerCase(); if(action.equals("hit")){ while((!action.equals("stay"))&&(playerHand<=20)){ System.out.print("\nYou get: "); System.out.print(cards.getcard(d[cardCount])); pHand[pHcnt] = cards.getcard(d[cardCount]); pHcnt++; if((playerHand >= 11)&&(cards.getval(d[cardCount]) == 11)){ playerHand = playerHand + 1; }else{ playerHand = playerHand + cards.getval(d[cardCount]); } cardCount++; System.out.print("\nPlayer hand: "); showHand(pHand); if(playerHand<=20){ System.out.print("\nAction: "); action = clin.readLine(); } }//end while }//end hit if }//end 18 if if(playerHand==21){ state = PWIN; }else{ if(playerHand>21){ state = PBUST; }else{ if(dealerHand>playerHand){ state = DWIN; }else{ while((dealerHand= 11)&&(cards.getval(d[cardCount]) == 11)){ dealerHand = dealerHand + 1; }else{ dealerHand = dealerHand + cards.getval(d[cardCount]); } cardCount++; System.out.print("\nDealer hand: "); showHand(dHand); if(dealerHand>21){ state = DBUST; }else{ if(dealerHand==21){ state = DWIN; }else{ if(dealerHand>playerHand){ state = DWIN; } } } }//end while } }//end dealer action }//end phand check break; case DBUST: System.out.println("Dealer busts"); System.out.println(); bank = bank + (bet *2); if(cardCount>=40){ state = SHUFFLE; }else{ state = BET; } break; case PBUST: System.out.println("Player busts"); System.out.println(); if(cardCount>=40){ state = SHUFFLE; }else{ state = BET; }break; case DWIN: System.out.println("Dealer wins"); System.out.println(); if(cardCount>=40){ state = SHUFFLE; }else{ state = BET; }break; case PWIN: System.out.println("Player wins"); System.out.println(); bank = bank + (bet *2); if(cardCount>=40){ state = SHUFFLE; }else{ state = BET; }break; case PUSH: System.out.println("Push"); System.out.println(); bank = bank + bet; if(cardCount>=40){ state = SHUFFLE; }else{ state = BET; }break; case SHUFFLE: System.out.println("Dealer is shuffling the deck..."); shuffle(d); cardCount = 1; state = BET; break; default: break; } }//end main while return bank; }//end blackjack /************* create deck ***************/ public static int[] newDeck(){ int size = 52; int deck[] = new int[size]; for(int i = 0; i < 52; i++){ deck[i] = i+1; } return deck; }//end newDeck /* public static void printArray(int q[]) { for(int i = 0; i < q.length; i++) { System.out.print(q[i]); System.out.print(" "); } System.out.print("\n"); }//end printArray */ /************** shuffle deck *****************/ static void shuffle(int[] a) { for (int lastPlace = a.length-1; lastPlace > 0; lastPlace--) { int randLoc = (int)(Math.random()*(lastPlace+1)); int temp = a[randLoc]; a[randLoc] = a[lastPlace]; a[lastPlace] = temp; } }//end shuffle public static boolean isNum(String s) { int count=0; for(int j=0; j