# Blackjack problem

• 08-24-2013, 06:28 AM
dojob
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:
Quote:

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
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;     }```
• 08-24-2013, 05:28 PM
jim829
Re: Blackjack problem
The following statement:

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
• 08-25-2013, 05:34 AM
dojob
Re: Blackjack problem
Quote:

Originally Posted by jim829
The following statement:

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
• 08-25-2013, 05:51 AM
jim829
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
• 09-05-2013, 09:55 PM
pianodave1984
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.