How do I add random letter in java (for blackjack game)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devilinthebox
    New Member
    • Feb 2008
    • 6

    How do I add random letter in java (for blackjack game)

    I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks.

    Code:
    // February 8, 2008
    // The "BlackJack" class.
    import java.awt.*;
    import hsa.Console;
    import java.util.Random; // Only the Random class
    import java.util.*;      // All classes in the java.util package
    
    public class BlackJack
    {
        static Console c;           // The output console
        
        public static void main (String[] args)
        {
            c = new Console ();
    
            char another;   
            c.println("******************");
            c.println("B-L-A-C-K-J-A-C-K");
            c.println("******************");
            c.println ("Your Cards:");
            int n = (int)(10.0 * Math.random()) + 1;
            int m = (int)(10.0 * Math.random()) + 1;
            c.print ("|" + n);
            c.println ("| |" + m + "|");
            int total;
            total = n+m;
            c.println ("You now have " + total);
            if (total <=21)
            {
            c.println ();
            c.print ("Do you want another? (y/n) ");
            another = c.readChar();
                    if (another == 'y' | another == 'Y')
            {
                    int a = (int)(10.0 * Math.random()) + 1;
                    c.println ("You have added the card " + a);
                    c.println ("You now have in total " + (n+m+a));
            }
            else if (total >21)
            {
            c.println ();
            c.print ("Your busted!");
            }
            }
        } // main method
    } // BlackJack class
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Make an array of characters and fill it with 0-9, J, Q, K, A. Then, use Random to generate which index you need and that's your card.

    Comment

    • devilinthebox
      New Member
      • Feb 2008
      • 6

      #3
      Please include the code of that, I dont know what arrays are.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Sun's Java Tutorial can explain them to you far better than I.

        Comment

        • jimhawkss
          New Member
          • Feb 2008
          • 10

          #5
          I think what you are trying to do is have the program print out a letter instead of the numbers 11, 12, 13, & 1 Right?
          I am assuming you are printing the numbers to the screen right.
          if so make a Switch after your random number but befor you display

          switch (your number variable)
          {
          case 1: System.printlin e("A"); break;
          case 11: System.printlin e("J"); break;
          case 12: System.printlin e("Q"); break;
          case 13: System.printlin e("K"); break;
          }

          Just have your program remember the numbers. Player only has to see the cards and nothing else.

          Hopefully this helps.


          Part I forgot:
          Also you will want to keep track of the numbers seperately so you can always print out the right letters. you can Make a Case 14. Just make sure that when you reference the array you have your method say something like
          (If Intname == 13) total += 10;
          same for 11, and 12. That way when they have a face card you aren't giving them more then what it is actually worth.
          Last edited by jimhawkss; Feb 10 '08, 05:58 AM. Reason: forgot somthing

          Comment

          • ukrainehigh
            New Member
            • Mar 2010
            • 2

            #6
            how do say do you want another card?

            Comment

            • pbrockway2
              Recognized Expert New Member
              • Nov 2007
              • 151

              #7
              Originally posted by ukrainehigh
              how do say do you want another card?
              Rather than resurrect an old thread you might do better to start your own. It's also best to ask the question as precisely as possible - usually with your own code and a statement about what is going wrong (compiler message or a description of the runtime behaviour.)

              Comment

              • ukrainehigh
                New Member
                • Mar 2010
                • 2

                #8
                I dont want to put it up in public since it's going to get marked soon is there anyway to pm you?

                Comment

                Working...