gives me randoms of number and the wrong dealers hand please help me
publicclass blackjack{
publicstaticvoid main(String[] args){
int[]cards ={1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10};
int[]player =newint[10];
int[]dealer =newint[10];
int playercard = 0;
int dealercard = 0;
int cardnum = 0;
int playertotal = 0;
int dealertotal = 0;
for(int p = 0;p<cards.length;p++)
{
System.out.print(cards[p]+" ");
}
System.out.println();
for(int i = 0;i<cards.length;i++)//unsort cards randomly
{
int randomindex = (int)(Math.random()*cards.length);
int temp = cards[i];
cards[i]=cards[randomindex];
cards[randomindex]=temp;
}
for(int u = 0;u<cards.length;u++)
{
System.out.print(cards[u]+" ");
}
System.out.println();
//deal the cards
cardnum=0;
//System.out.println("Dealer Card 1 is " +cards[cardnum]);
dealer[dealercard]=cards[cardnum];//i.e. sets first card in dealer hand to the first card in the unsorted cards
cardnum++;
dealercard++;
System.out.println("Dealer Card 2 is " +cards[cardnum]);
dealer[dealercard]=cards[cardnum];
cardnum++;//keeps on going up on the unsorted cards.
System.out.println("Player Card 1 is " +cards[cardnum]);
player[playercard]=cards[cardnum];
cardnum++;
playercard++;//This is so it does overwrite what the previous player card was
System.out.println("Player Card 2 is " +cards[cardnum]);
player[playercard]=cards[cardnum];
playercard++;
cardnum++;
playertotal = (player[0]+player[1]);
System.out.println ("Player total is " +playertotal);
cardnum++;
dealertotal = (dealer[0]+dealer[1]);
//System.out.println("Dealer total is " +dealertotal);
while(true)//checks for blackjack.
{
if(playertotal==21)
{
System.out.println("You win. You had a blackjack");break;
}
if(dealertotal==21)
{
System.out.println("You lose. Dealer had blackjack.");break;
}
else
{
break;
}
}
while(playertotal!=21)//hitting
{
int p1 = Integer.parseInt(JOptionPane.showInputDialog (null,"Press 1 to hit and 2 to stand"));
System.out.print("Your cards are ");//prints player's cards.
for(int i = 0;i<player.length;i++)
{
if(player[i]!=0)
{
System.out.print(+player[i]+" ");
}
}
System.out.println();
if(p1==1)
{
System.out.println ("You drew a " +cards[cardnum]);
player[playercard] = cards[cardnum];
cardnum++;//keeps the cards position moving up
playertotal = playertotal + player[playercard];//adds up playertotal after hitting
playercard++;//keeps the player's hand moving up, so one card does not does overwrite the other.
System.out.println("Your total is "+playertotal+".");
if(playertotal>21)
{
System.out.println("You Bust");
break;
}
if(playertotal==21)
{
System.out.println("You got blackjack, you win!");
break;
}
}
if(p1==2)//if player hits 2 (stand), then break out of while loop
{
break;
}
}
while(dealertotal!=21)
{
System.out.print("Dealer's cards are ");//PROBLEM HERE
for(int i = 0;i<dealer.length;i++)
{
if(dealer[i]!=0)
{
System.out.print(+dealer[i]+" ");
}
}
System.out.println();
if(dealertotal>=17)
{
System.out.println("Dealer Stands");
System.out.println("Dealer's total was " +dealertotal+".");
break;
}
if(dealertotal<=17 && dealertotal<17)//if the dealer's total is less than 17, then the dealer hits.
{
dealer[dealercard] = cards[cardnum];
cardnum++;
dealertotal = dealertotal + dealer[dealercard];
dealercard++;
if(dealertotal>16 && dealertotal<21)
{
System.out.println("Dealer Stands");
System.out.println("Dealer's total was " +dealertotal+".");
break;
}
}
if(dealertotal>21)
{
System.out.println("Dealer Busts.");
System.out.println("Dealer's total was " +dealertotal+".");
break;
}
}
while(true)
{
if(dealertotal>playertotal && dealertotal<21 && playertotal<21)
{
System.out.println("The Dealer Wins");break;
}
if(dealertotal<playertotal && dealertotal><21 && playertotal<21)
{
System.out.println("You Win");break;
}
if(dealertotal>21 && playertotal>21)
{
System.out.println("You both bust.");break;
}
if(dealertotal==21)
{
System.out.println("Dealer got blackjack.");break;
}
if(playertotal==21)
{
System.out.println("You got blackjack.");break;
}
if(playertotal==21 && dealertotal==21)
{
System.out.println("The game is a push.");break;
}
if(playertotal==dealertotal)
{
System.out.println("Game is a push.");break;
}
break;
}
}
}
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10
5 10 10 8 4 4 1 10 5 3 8 3 9 3 7 2 9 8 10 10 8 10 6 6 2 7 3 7 10 9 6 6 10 10 1 10 5 10 2 7 10 5 9 4 4 2 1 1
Here's part of the output:
Dealer's cards are 1 6
Dealer's cards are 1 8
Dealer Stands
Dealer's total was 20.

