Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

How to keep looping

Thread view: 
guitarromantic@gmail.com - 18 Feb 2006 14:47 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.