Results 1 to 5 of 5
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Blackjack problem

    I am working on a program where need to display the sum of Blackjack with number of instances of 21.

    But i am facing some problem here where the string of the ranks is not displayed correctly.
    I have marks the fallacy in red as below.
    Not sure where is the problem, as it is most correct for most situation

    Output:
    Case 1:
    picks[i]: 48 :: picks[i] % 13: 9 :: rank: 10 :: picks[i] % 13: 9
    picks[i]: 24 :: picks[i] % 13: 11 :: rank: Queen :: picks[i] % 13: 11
    The sum of 10 and Queen(10 10) is 20

    Case 2:
    picks[i]: 48 :: picks[i] % 13: 9 :: rank: 10 :: picks[i] % 13: 9
    picks[i]: 21 :: picks[i] % 13: 8 :: rank: 9 :: picks[i] % 13: 8
    The sum of 10 and 9(10 9) is 19

    Case 3:
    picks[i]: 19 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    picks[i]: 50 :: picks[i] % 13: 11 :: rank: Queen :: picks[i] % 13: 11
    The sum of 7 and Queen(7 10) is 17

    Case 4:
    picks[i]: 44 :: picks[i] % 13: 5 :: rank: 6 :: picks[i] % 13: 5
    picks[i]: 30 :: picks[i] % 13: 4 :: rank: 5 :: picks[i] % 13: 4
    The sum of 6 and 5(6 5) is 11

    Case 5:
    picks[i]: 34 :: picks[i] % 13: 8 :: rank: 9 :: picks[i] % 13: 8
    picks[i]: 4 :: picks[i] % 13: 4 :: rank: 5 :: picks[i] % 13: 4
    The sum of 9 and 5(9 5) is 14

    Case 6:
    picks[i]: 14 :: picks[i] % 13: 1 :: rank: 5 :: picks[i] % 13: 1
    picks[i]: 50 :: picks[i] % 13: 11 :: rank: Queen :: picks[i] % 13: 11
    The sum of 5 and Queen(2 10) is 12

    Case 7:
    picks[i]: 29 :: picks[i] % 13: 3 :: rank: 4 :: picks[i] % 13: 3
    picks[i]: 23 :: picks[i] % 13: 10 :: rank: Jack :: picks[i] % 13: 10
    The sum of 4 and Jack(4 10) is 14

    Case 8:
    picks[i]: 39 :: picks[i] % 13: 0 :: rank: 4 :: picks[i] % 13: 0
    picks[i]: 7 :: picks[i] % 13: 7 :: rank: 8 :: picks[i] % 13: 7
    The sum of 4 and 8(11 8) is 19

    Case 9:
    picks[i]: 37 :: picks[i] % 13: 11 :: rank: Queen :: picks[i] % 13: 11
    picks[i]: 33 :: picks[i] % 13: 7 :: rank: 8 :: picks[i] % 13: 7
    The sum of Queen and 8(10 8) is 18

    Case 10:
    picks[i]: 44 :: picks[i] % 13: 5 :: rank: 6 :: picks[i] % 13: 5
    picks[i]: 6 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    The sum of 6 and 7(6 7) is 13

    Case 11:
    picks[i]: 45 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    picks[i]: 41 :: picks[i] % 13: 2 :: rank: 3 :: picks[i] % 13: 2
    The sum of 7 and 3(7 3) is 10

    Case 12:
    picks[i]: 19 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    picks[i]: 49 :: picks[i] % 13: 10 :: rank: Jack :: picks[i] % 13: 10
    The sum of 7 and Jack(7 10) is 17

    Case 13:
    picks[i]: 13 :: picks[i] % 13: 0 :: rank: 7 :: picks[i] % 13: 0
    picks[i]: 24 :: picks[i] % 13: 11 :: rank: Queen :: picks[i] % 13: 11
    The sum of 7 and Queen(11 10) is 21

    Case 14:
    picks[i]: 10 :: picks[i] % 13: 10 :: rank: Jack :: picks[i] % 13: 10
    picks[i]: 47 :: picks[i] % 13: 8 :: rank: 9 :: picks[i] % 13: 8
    The sum of Jack and 9(10 9) is 19

    Case 15:
    picks[i]: 5 :: picks[i] % 13: 5 :: rank: 6 :: picks[i] % 13: 5
    picks[i]: 8 :: picks[i] % 13: 8 :: rank: 9 :: picks[i] % 13: 8
    The sum of 6 and 9(6 9) is 15

    Case 16:
    picks[i]: 21 :: picks[i] % 13: 8 :: rank: 9 :: picks[i] % 13: 8
    picks[i]: 9 :: picks[i] % 13: 9 :: rank: 10 :: picks[i] % 13: 9
    The sum of 9 and 10(9 10) is 19

    Case 17:
    picks[i]: 14 :: picks[i] % 13: 1 :: rank: 10 :: picks[i] % 13: 1
    picks[i]: 3 :: picks[i] % 13: 3 :: rank: 4 :: picks[i] % 13: 3
    The sum of 10 and 4(2 4) is 6

    Case 18:
    picks[i]: 13 :: picks[i] % 13: 0 :: rank: 10 :: picks[i] % 13: 0
    picks[i]: 40 :: picks[i] % 13: 1 :: rank: 4 :: picks[i] % 13: 1
    The sum of 10 and 4(11 2) is 13

    Case 19:
    picks[i]: 6 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    picks[i]: 49 :: picks[i] % 13: 10 :: rank: Jack :: picks[i] % 13: 10
    The sum of 7 and Jack(7 10) is 17

    Case 20:
    picks[i]: 19 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    picks[i]: 32 :: picks[i] % 13: 6 :: rank: 7 :: picks[i] % 13: 6
    The sum of 7 and 7(7 7) is 14
    The number of instances of Blackjack is 1
    Java Code:
        public static void main(String[] args) {       
            Scanner input = new Scanner(System.in);
            
            final int NO_OF_PICKS = 2;
            int count = 0;  
            int[] picks = new int[NO_OF_PICKS];
            String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
               
            for(int i = 0; i < 20; i++){
                System.out.print("Case " + (i + 1) + ": " );
                if(isBlackJack(ranks,picks)){
                    count++;
                }
            }
            
            System.out.println("The number of instances of Blackjack is " + count);    
        }
        
        public static boolean isBlackJack(String[] ranks, int[] picks){
            int[] valueOfPicks = new int[picks.length];
            final int NO_OF_CARDS = 52;
            
            for(int i = 0; i < picks.length; i++){
                picks[i] = (int)(Math.random() * NO_OF_CARDS);
                System.out.print("\n" + "picks[i]: " + picks[i] + " :: ");
                ranks[i] = ranks[picks[i] % 13];
                System.out.print("picks[i] % 13: " + picks[i] % 13 + " :: ");
                System.out.print("rank: " + ranks[i] + " :: ");
                valueOfPicks[i] = returnValue(picks[i] % 13);
                System.out.print("picks[i] % 13: " + picks[i] % 13 + " " );
            }
            
           System.out.println();
           int sum = valueOfPicks[0] + valueOfPicks[1];
           System.out.print("The sum of " + ranks[0] + " and " + ranks[1]);
           System.out.print("(" + valueOfPicks[0] + " " + valueOfPicks[1] + ")");
           System.out.println(" is " + sum);
           System.out.println();
    
            if(sum == 21){
                return true;
            }
            
            return false;
        }
        
        public static int returnValue(int i){
            if(i == 0){
                return 11;
            }
            else if(i >= 1 && i <= 9){
                return i + 1;
            }
    
            return 10;
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,810
    Rep Power
    5

    Default Re: Blackjack problem

    The following statement:

    Java Code:
    ranks[i] = ranks[picks[i] % 13];
    in your isBlackJack() method looks suspicious. Do you really want to be overwriting your data? You are replacing ranks[0] and ranks[1] with random values.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Blackjack problem

    Quote Originally Posted by jim829 View Post
    The following statement:

    Java Code:
    ranks[i] = ranks[picks[i] % 13];
    in your isBlackJack() method looks suspicious. Do you really want to be overwriting your data? You are replacing ranks[0] and ranks[1] with random values.

    Regards,
    Jim
    Shouldn't ranks[0] and ranks[1] be random value? A random 2 cards is picked from the deck.
    What do you mean by overwriting the data? I have print the output of ranks[i] = ranks[picks[i] % 13]; in line 26 and it appears that it display the correct output.

    but it seems there is a problem in the output when calling System.out.print("The sum of " + ranks[0] + " and " + ranks[1]); in line 35
    Last edited by dojob; 08-25-2013 at 06:38 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,810
    Rep Power
    5

    Default Re: Blackjack problem

    You are passing the String array ranks to your method. When you change ranks in the method, the array ranks in main also gets changed.
    Do you want to do that? I could also be misunderstanding your intention.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    pianodave1984's Avatar
    pianodave1984 is offline Aspiring Javaman
    Join Date
    Sep 2013
    Location
    West Chester PA
    Posts
    8
    Rep Power
    0

    Default Re: Blackjack problem

    Yeah, it looks like you're passing the array coordinates to the string. While I haven't mastered blackjack (I can't get the stupid Ace problem yet), Whenever I have any situation like that I always use two Arrays, one for the card value, and the other for the Card name.
    Last edited by pianodave1984; 09-05-2013 at 10:55 PM. Reason: changed "the to string" to "to the string"... because I know english

Similar Threads

  1. Blackjack
    By jediassassin37 in forum New To Java
    Replies: 2
    Last Post: 10-24-2012, 05:01 AM
  2. Need help with BlackJack Program
    By SilverSoul in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 02:52 PM
  3. Need help...blackjack not working.
    By g2mediagroup in forum New To Java
    Replies: 5
    Last Post: 05-03-2011, 07:40 AM
  4. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 06:40 PM
  5. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 06:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •