The final project for my class had to be done in a group. The goal of the assignment is to create a blackjack program, it can use as many classes and whatever methods we want. I'm very tired of trying to figure out the logic errors on my own though, and it's due soon. Here are the source codes.
blackjackHand: This is the object that contains card objects.
import java.util.ArrayList; import java.util.Random; public class BlackJackHand { ArrayList<Card> Hand = new ArrayList<Card>(); int sumValue = 0; public BlackJackHand() { this.Hand = Hand; } public int getBlackJackValue() { for(int i = 0;i < (Hand.size()); i++) { sumValue += Hand.get(i).getValue(); } return sumValue; } public int resetBlackJackValue() { sumValue = 0; return sumValue; } public int getCardCount() { return this.Hand.size(); } public void clearHand() { this.Hand.clear(); } public void addCard( Card C ) { this.Hand.add( C ); } public int returnHand() { return sumValue; } public Card getCard( int C) { return Hand.get(C); } }
card class: This class gives the behavior of the cares object
public class Card { public final static int CLUBS = 0; // Codes for the suits and values public final static int DIAMONDS = 1; public final static int HEARTS = 2; public final static int SPADES = 3; public final static int ACE = 1; public final static int JACK = 11; public final static int QUEEN = 12; public final static int KING = 13; public int Value; public int Suit; //constructor public Card(int Value, int Suit) { this.Value = Value; this.Suit = Suit; } public int getSuit() { return Suit; } public void setSuit( int S ) { S = Suit; } public int getValue() { switch ( Value ) { case 1: return 11; case 2: return 2; case 3: return 3; case 4: return 4; case 5: return 5; case 6: return 6; case 7: return 7; case 8: return 8; case 9: return 9; case 10: return 10; case 11: return 10; case 12: return 10; default: return 10; } } public void setValue( int V ) { V = Value; } //print suite public String getSuitAsString() { switch ( Suit ) { case SPADES: return "Spades"; case HEARTS: return "Hearts"; case DIAMONDS: return "Diamonds"; default: return "Clubs"; } } public String getValueAsString() { switch ( Value ) { case 1: return "Ace"; case 2: return "2"; case 3: return "3"; case 4: return "4"; case 5: return "5"; case 6: return "6"; case 7: return "7"; case 8: return "8"; case 9: return "9"; case 10: return "10"; case 11: return "Jack"; case 12: return "Queen"; default: return "King"; } } public String toString() { return this.getValueAsString() + " of " + this.getSuitAsString(); } }//end card class
Deck Class: This class gives us the behaviors for a deck object
import java.util.Random; import java.util.ArrayList; import java.util.Collections; public class Deck { //constructor public Random generator; private int cardcount = 52; ArrayList<Card> deck = new ArrayList<Card>(); public Deck() { for(int j=1; j<14; j++) { for(int i=0; i<5; i++ ) { deck.add(new Card( i , j )); } } } //shuffle utility public void shuffle() { Collections.shuffle(this.deck); } public Card dealCard() { int index = (int) (Math.random() * cardcount); Card tempCard = deck.get(index); deck.remove(index); this.cardcount = cardcount - 1; return tempCard; } }
Here is the main code for the Blackjack program itself:
import java.util.Scanner; import java.util.ArrayList; public class Blackjack { public static void main(String[]args) { Scanner input = new Scanner(System.in); int money = 100; int bet; boolean end = false; boolean play = true; boolean win; boolean doubleDown = false; BlackJackHand dealer = new BlackJackHand(); BlackJackHand player = new BlackJackHand(); System.out.println("Welcome to Blackjack!"); while (money >=0) { bet = 0; do { Deck deck = new Deck(); System.out.println("You have "+money+" Dollars"); System.out.println("How much would you like to bet?"); bet = input.nextInt(); if ( bet > money || bet <1 ) { System.out.println("Please bet between 1 and "+ money +""); bet = input.nextInt(); } deck.shuffle(); player.addCard(deck.dealCard()); player.addCard(deck.dealCard()); dealer.addCard(deck.dealCard()); dealer.addCard(deck.dealCard()); player.getBlackJackValue(); System.out.println("The Dealer is showing "+ dealer.getCard(1)+""); System.out.println("You Have "+ player.getCard(0) +" and the "+ player.getCard(1)+""); System.out.println("You are at "+player.returnHand()+""); if (player.returnHand() == 8 || player.returnHand() == 11) { System.out.println("You are at "+player.returnHand()+". Would you like to DOUBLE DOWN?"); System.out.println("1 = yes\n2 = no"); int pick = input.nextInt(); switch ( pick ) { case 1: doubleDown = true; player.addCard(deck.dealCard()); bet = bet*2; System.out.println("The Dealer shows the " + dealer.getCard(0) + " and the " + dealer.getCard(1) + "."); player.resetBlackJackValue(); dealer.resetBlackJackValue(); player.getBlackJackValue(); dealer.getBlackJackValue(); System.out.println("You got the " + player.getCard(2) + " Your new score is: " + player.returnHand()); while(dealer.returnHand() <= 17) { System.out.println("The house has " + dealer.returnHand()); dealer.addCard(deck.dealCard()); dealer.resetBlackJackValue(); dealer.getBlackJackValue(); System.out.println("The house hits and now has " + dealer.returnHand()); } if (player.returnHand() > dealer.returnHand() && player.returnHand() <=21) { money += bet; System.out.println("You win!!!"); }//end if else if (dealer.returnHand() > player.returnHand() && dealer.returnHand() <= 21) { money -= bet; System.out.println("You have less than the house, You lose."); } else if (dealer.returnHand() == player.returnHand()) { System.out.println("You Have the same as the house, It's a tie and you get your bet back."); } else if (dealer.returnHand() > 21) { money += bet; System.out.println("The house is bust. You Win."); } default: break; }//end switch }//end doubledown if if (doubleDown == false) { end = false; int hs = 1; do{ System.out.println("Would you like to Hit or Stay?\n1 = Hit\n2 = Stay"); hs = input.nextInt(); switch ( hs ) { case 1: Card nc = deck.dealCard(); player.addCard(nc); player.resetBlackJackValue(); player.getBlackJackValue(); System.out.println("Your new card is " + nc ); System.out.println("Your new total is: " + player.returnHand()); if ( player.returnHand() > 21) { System.out.println("You BUST with " + player.returnHand() +". You lose $" + bet +""); money -=bet; end = true; break; } else if(player.returnHand() == 21 ) { System.out.println("You win $" + bet +""); money +=bet; end = true; break; } case 2: end = false; break; } } while (hs == 1 && end == false); if(end == false) { System.out.println("The Dealer shows the " + dealer.getCard(0) + " and the " + dealer.getCard(1) + "."); dealer.resetBlackJackValue(); dealer.getBlackJackValue(); while(dealer.returnHand() < 17) { dealer.getBlackJackValue(); System.out.println("The house has " + dealer.returnHand()); dealer.resetBlackJackValue(); dealer.addCard(deck.dealCard()); dealer.getBlackJackValue(); System.out.println("The house hits and now has " + dealer.returnHand()); if (player.returnHand() > dealer.returnHand() && player.returnHand() <=21) { money += bet; System.out.println("You win!!!"); break; } if (player.returnHand() <= 21 && dealer.returnHand() >21) { System.out.println("Dealer BUSTS at " + dealer.returnHand() +". You win $" + bet +""); money = money + bet; break; } } } if (player.returnHand() > dealer.returnHand() && player.returnHand() <=21) { money += bet; System.out.println("You win!!!"); }//end if else if (dealer.returnHand() > player.returnHand() && dealer.returnHand() <= 21) { money -= bet; System.out.println("The house has " + dealer.returnHand() + "."); System.out.println("You have less than the house, You lose."); } else if (dealer.returnHand() == player.returnHand()) { System.out.println("You Have the same as the house, It's a tie and you get your bet back."); } }//end if player.clearHand(); player.resetBlackJackValue(); dealer.clearHand(); dealer.resetBlackJackValue(); player.resetBlackJackValue(); }//end do-while while( play = true); bet =0; doubleDown = false; }//while money >=0; System.out.println("Looks Like your out of money!"); }//end main }//end blackjack
The big problem right now is that the code for the dealer is only executing once. That is, the dealer will hit once, and then quit. Even if it is already above 17, or even above 21.
Also, the game continues when money = 0.
Lastly, our shuffle method doesn't work.
Also, I know that in blackjack you double down at 10 or 11, not 8.