# How to make this happen !

Page 1 of 1

## 1 Replies - 246 Views - Last Post: 02 March 2013 - 02:41 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314028&amp;s=1fdc31aa51dfa2a2dc397860c73335d3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sumerti

Reputation: 0
• Posts: 1
• Joined: 02-March 13

# How to make this happen !

Posted 02 March 2013 - 02:29 AM

I want to create a program for simple Blackjack game. Not with the aces and kings and queens but just with numbers from 1 to 11.
The player will get 2 cards first, and the dealer will get 2 cards too but only one will be shown to the player. Then if the player will be asked, "Hit" or "Stand". If player says "Hit" another card(third card) will be given and the total of 3 cards will be shown. And then if the total is less than 21, the player will be again asked for "hit" or "stand" (This is where I am getting confused)
And if the user says "Stand", the dealer will be given cards until dealer's total reaches 17 and then the totals of dealer and player will be compared and the winner will be decided.
If the user enters stop, the program will exit and if the user enters any other value, the game will again start. The program should not crash.

So, I have done pretty much everything except- how to ask for "hit"/"Stand" again after the user has entered "hit" once. Here's my code-
I read the rules This is my homework but I'm not asking to solve it for me. Just telling/ explaining the logic will do. Any kind of help would be greatly appreciated.
Thanks for looking,
Regards,
Sumer.
```import javax.swing.JOptionPane;
import java.util.Random;
public class Practice2 {
int[] dealer;
int[] player;
Random ran;
int i = 2;
int z = 2;

Practice2(){
dealer = new int[22];
player = new int[22];
ran = new Random();
}//end practice 2

int countcards(int[] temp){
int count = 0;
for (int i=0; i < temp.length; i++){
count = count + temp[i];
}
return count;
}// end meyhod

public static boolean check (int p, int d){
if (p > d & p<21)
return true;
else if (p==21){
return true;
}
else if (d>p & d<21);
return false;
}// end check

int dealcard(){

double x = ran.nextDouble();
int acard = ran.nextInt(11)+1;

return acard;
}//end meythod

/* public int[] getPlayerScore(){
return player;
}
*/

public static void main(String[] args) {
String input;

Practice2 mygame = new Practice2();

do{
mygame.player[0] = mygame.dealcard();
mygame.player[1] = mygame.dealcard();
System.out.println("player [0] is " +mygame.player[0]);
System.out.println("player [1] is " +mygame.player[1]);
mygame.countcards(mygame.player);
System.out.println("player total is " +mygame.countcards(mygame.player));
} while(mygame.countcards (mygame.player)>21);

do {
mygame.dealer[0] = mygame.dealcard();
mygame.dealer[1] = mygame.dealcard();
System.out.println("dealer [1] is " +mygame.dealer[1]);
mygame.countcards(mygame.dealer);
System.out.println("dealer total is " +mygame.countcards(mygame.dealer));
} while(mygame.countcards (mygame.dealer)>21);

input = JOptionPane.showInputDialog(null,
"** New Game ** \n" +
" Your Card: " + mygame.player[0] + " , " + mygame.player[1] + "        Dealer's First Card: " + mygame.dealer[0] +
"\n Your Score: " + mygame.countcards(mygame.player));
//System.out.println(input);

if (input.equalsIgnoreCase("stand")){
while ( mygame.countcards(mygame.dealer)< 17) {

mygame.dealer[mygame.i] = mygame.dealcard();

mygame.countcards(mygame.dealer);
//System.out.println("Dealer total after 2 int x "+mygame.countcards(mygame.dealer));

System.out.println("dealer ["+mygame.i +"]" + mygame.dealer[mygame.i]);
System.out.println("Dealer total after stand int x "+mygame.countcards(mygame.dealer));
mygame.i++;
}// end while
}// end if

else if (input.equalsIgnoreCase("hit")){

mygame.player[mygame.z] = mygame.dealcard();
mygame.z++;
input = JOptionPane.showInputDialog(null,
"** New Game ** \n" +
" Your Card: " + mygame.player[0] + " , " + mygame.player[1] + "        Dealer's First Card: " + mygame.dealer[0] +
"\n Your Score: " + mygame.countcards(mygame.player));
mygame.countcards(mygame.player);
System.out.println("player" + mygame.z + mygame.player[mygame.z]);

System.out.println("player total after hit " + mygame.countcards(mygame.player));}

//end while

else if (input.equalsIgnoreCase("stop")) {
JOptionPane.showMessageDialog(null,"B'bye ");
System.exit(0);
}// end else if
else {
main(args);
}// end else

if ((check(mygame.countcards(mygame.player),mygame.countcards(mygame.dealer))== true)){System.out.println("Player won");}
else if ((check(mygame.countcards(mygame.player),mygame.countcards(mygame.dealer))== false)){System.out.println("dealer won");}

}//end main

}//end class practice2
```

Is This A Good Question/Topic? 0

## Replies To: How to make this happen !

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: How to make this happen !

Posted 02 March 2013 - 02:41 AM

Just as you've done in other places, the hit/stand question will be asked in a loop, a do/while seems appropriate, until the user stands or busts.