Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I need help figuring out the logic errors in this final project.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I need help figuring out the logic errors in this final project.

    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.


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: I need help figuring out the logic errors in this final project.

    Execute your code and ask specific questions. We can't analyze and correct your algorithm

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help figuring out the logic errors in this final project.

    figure out the logic errors
    How are you trying to debug the code so you can understand what it is doing that you want to change?
    I use println() statements that print out the values of variables as the code executes so I can see what the computer sees as the code executes. If you know what the program is supposed to do, seeing what it is really doing will help you change it so it does what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Gingerbread Fetus (December 5th, 2013)

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I need help figuring out the logic errors in this final project.

    Quote Originally Posted by Norm View Post
    How are you trying to debug the code so you can understand what it is doing that you want to change?
    I use println() statements that print out the values of variables as the code executes so I can see what the computer sees as the code executes. If you know what the program is supposed to do, seeing what it is really doing will help you change it so it does what you want.
    That was a really big help. I've never tried that approach before. Seems kind of obvious when I think about it, but I really just needed a better practice for debugging. I've got the game to work now, though there may be one bug left to fix. Thanks a ton.

Similar Threads

  1. problem in final year project
    By hellzone110 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 11th, 2013, 10:49 AM
  2. JAVA1 - Final Project
    By ch103 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2011, 02:48 PM
  3. final project idea
    By zulqar in forum AWT / Java Swing
    Replies: 4
    Last Post: November 3rd, 2010, 07:28 AM
  4. final project idea
    By zulqar in forum Java Theory & Questions
    Replies: 1
    Last Post: November 1st, 2010, 12:02 PM
  5. need idea for final year project
    By asadkhan in forum Java Applets
    Replies: 0
    Last Post: August 22nd, 2010, 01:36 AM