This is really basic stuff but I just can't seem to get my brain to
function; a poke in the right direction would be really useful!
I'm writing a BlackJack class and I'm just putting together the game
itself. I'm sure you're all familiar with the rules, but essentially
I'm using an JOptionPane to ask the user to twist/stick and then doing
so based on their input. My problem is then asking them to twist/stick
again once they've clicked twist, without just nesting if statements. I
know there's an easier way but I'm damned if I can remember.. here's my
code:
DeckOfCards newDeck = new DeckOfCards();
HandOfCards newHand = new HandOfCards();
newDeck.shuffle();
//deal two cards
newHand.addCard(newDeck.pickNextCard());
newHand.addCard(newDeck.pickNextCard());
//show 'em what they've got in their hand
newHand.display();
if (newHand.isBust() == false)
{
JFrame frame = new JFrame();
Object[] options = {"Twist", "Stick"};
int choice = JOptionPane.showOptionDialog(frame,
"Would you like to
stick or twist?",
"Blackjack Game",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //don't use a
custom icon
options, //the titles
of the buttons
options[0]); //default
button title
if (choice == JOptionPane.YES_OPTION) {
// User clicked twist.
newHand.addCard(newDeck.pickNextCard()); //deal another card
if (newHand.isBust() == true)
{
System.out.println("You are bust!");
}
else
{
newHand.display(); //show them their new hand
}
} else if (choice == JOptionPane.NO_OPTION) {
// User clicked stick.
newHand.display(); //show them their closing score
}
}
steve_marjoribanks@hotmail.com - 18 Feb 2006 16:43 GMT
Maybe you could use a while loop so that while i<=21 for example, it
would continue to loop through the process of displaying an option pane
asking whether they want another card or not and then if they do, add
the value of that card onto the existing value of i?
guitarromantic@gmail.com - 18 Feb 2006 17:16 GMT
Ahh, I feel so stupid now! I just changed my "if (newHand.isBust() ==
false)" to "while (newHand.isBust() == false)" and it works! I knew
there was some simple solution.. thanks!
Alan Krueger - 19 Feb 2006 07:29 GMT
> Ahh, I feel so stupid now! I just changed my "if (newHand.isBust() ==
> false)" to "while (newHand.isBust() == false)" and it works! I knew
> there was some simple solution.. thanks!
Some people object to using == to compare boolean values since it
returns a boolean itself.
Why not just say "while (!newHand.isBust()) ..."?
guitarromantic@gmail.com - 19 Feb 2006 13:35 GMT
True, that would be more efficient, thankyou.