0

The aim of this program is to create a random number generator, and select cards numbered 1-11 (11 being the Ace). The game rules are not as important as the finished working program, I do not need suits, money or hi-score tables.

I have attempted and failed to create a card generator that will, produce a random card value each time it is called for. As you will see in the code, the int card1 = 1 +r.nextInt(); returns the same value every time. If it calls a 4 for card1, all future card1(s) will also be a 4. This is not what I had in mind when creating the int value 1-11 inc. I had hoped the card1 would return a random value each time.

Next I attempted and failed to add the card total and the newCard1 to a current int. This also does not work as intended.

Here is the code I have attempted so far, and please remember when your explaining your corrections to me, I am a first year student, and have little to none basic knowledge:

  import java.util.Random;
  import java.util.Scanner;

  class blackj
{
public static void main(String[] args)
{
    Random r = new Random();
    Scanner keyboard = new Scanner(System.in);
    String name;

    boolean playing = true;
    boolean notPlaying = true;
    int card1 = 1 + r.nextInt(11);
    int card2 = 1 + r.nextInt(11);
    int dcard1 = 1 + r.nextInt(11);
    int dcard2 = 1 + r.nextInt(11);

    int ptotal = card1+card2;
    int dtotal = dcard1+dcard2;

    int pcurrent = ptotal+card1;
    int dcurrent =dtotal+dcard1;
    {   


        System.out.println("Welcome to Blackjack ! " );
            System.out.println("Score as close to 21 without going over to win ");
                System.out.println("What is your name?");
            name = keyboard.nextLine();
                System.out.println("Hello " + name);
                System.out.println("Let's play some BlackJack!");
            System.out.println("The dealer shows: \n\t\t" +dcard1 );
            System.out.println("Your first card is: \n\t\t " +card1 );
            System.out.println("Your second card is: \n\t\t" +card2  );
            System.out.println("Giving you a grand total of: " +ptotal );

        while (playing)
            {
            System.out.println("Would you like to (H)it or (S)tick?");
                Scanner hit1 = new Scanner(System.in);
                String a = hit1.nextLine();
                if(a.equals("s"))
            {
                System.out.println("You stick at " );
                System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
            }
                if(a.equals("h"))
            {
                System.out.println("Your next card is " +card2 );
                System.out.println("Giving you a new total of "+pcurrent );
                    if ((pcurrent >=22))
                System.out.println("You Busted! \nSorry! you lose");
            }
            else
            {
                System.out.println("Please press H or S");
            }

            }
      }


  }

  }

As you can see I haven't gotten to the dealers part yet.

Keith Payne
  • 3,002
  • 16
  • 30
  • http://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed this should help. – hs2345 Mar 15 '15 at 22:39
  • 1
    What a fantastic idea, do you get points for sarcasm in this forum? Which methods are you referring to? and how do you recommend I employ the methods to create, what I have already failed at doing? – Vincent Tyson Mar 15 '15 at 22:40
  • @Vincent I love that you're criticizing sarcasm, given the last line in your post. – Jorn Mar 15 '15 at 22:52

3 Answers3

2

Put another call to random within your while loop:

int newCard = 1 + r.nextInt(11);

This will set the variable to new random values (if you need to reset your original card variables, you can do that too). Then make sure you check the user's input against this new random card.

After you get this working, you might want to check out code review for some tips on structuring your program.

Community
  • 1
  • 1
mk.
  • 11,360
  • 6
  • 40
  • 54
  • I believe he only wants to generate only one next card in loop, but you got the point.... – sodik Mar 15 '15 at 22:41
0

The problem is that you're applying the randomization as you initialize your cards. Declare your cards this way instead:

int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);

Do your randomization in a method, like so:

public int getRandomInt()
{
    return 1 + r.nextInt(11);
}

And assign values to your cards like so:

card1 = getRandomInt();

Now, any time you assign a value to a card (even the same card repeatedly), you're going to get a random value.

System.out.println("Your next card is " +card2 );

can be replaced with

card2 = getRandomInt();
System.out.println("Your next card is " +card2 );
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • that looks like exactly what I was trying to do, the only problem is I have no idea how that should look. Can you give me a limited working example I can expand on myself? – Vincent Tyson Mar 15 '15 at 22:48
  • I really don't know what else you're asking to see now. Do you not understand how to apply a method? EDIT: Example added. If I do any more, I'm afraid you won't learn anything. – MarsAtomic Mar 15 '15 at 22:52
  • I don't know how to apply methods I haven't learn yet, sadly I am a complete beginner, and the "School work" we have done, has not approached this at all. EDIT: Thanks for the example I will learn it, as soon as I understand the logic behind the method. – Vincent Tyson Mar 15 '15 at 22:55
0

Using your current methodology, this will help you start a new round

    Random r = new Random();
    Scanner keyboard = new Scanner(System.in);
    String name;

    boolean playing = true;

    System.out.println("Welcome to Blackjack ! ");
    System.out.println("Score as close to 21 without going over to win ");
    System.out.println("What is your name?");
    name = keyboard.nextLine();
    System.out.println("Hello " + name);
    int card1, card2 = 0, dcard1, dcard2, ptotal, dtotal, pcurrent = 0, dcurrent;
    boolean newRound = true;
    while (playing) {
        if (newRound) {
            newRound = false;
            card1 = 1 + r.nextInt(11);
            card2 = 1 + r.nextInt(11);
            dcard1 = 1 + r.nextInt(11);
            dcard2 = 1 + r.nextInt(11);

            ptotal = card1 + card2;
            dtotal = dcard1 + dcard2;

            pcurrent = ptotal + card1;
            dcurrent = dtotal + dcard1;
            System.out.println("Let's play some BlackJack!");
            System.out.println("The dealer shows: \n\t\t" + dcard1);
            System.out.println("Your first card is: \n\t\t " + card1);
            System.out.println("Your second card is: \n\t\t" + card2);
            System.out.println("Giving you a grand total of: " + ptotal);
        }

        System.out.println("Would you like to (H)it or (S)tick?");
        Scanner hit1 = new Scanner(System.in);
        String a = hit1.nextLine();
        if (a.equals("s")) {
            System.out.println("You stick at ");
            System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
        }
        if (a.equals("h")) {
            System.out.println("Your next card is " + card2);
            System.out.println("Giving you a new total of " + pcurrent);
            if ((pcurrent >= 22)) {
                System.out.println("You Busted! \nSorry! you lose");
                newRound = true;
            }
        } else {
            System.out.println("Please press H or S");
        }

    }

Combine this with what the others said about card2 and the random card stuff, and you should be well on your way.

puj
  • 770
  • 5
  • 9
  • nothing like a little jave to make a man feel like an idiot :) there is a lot I don't understand in your revisions. The way you declared your int values, each related type =0, I assume that is to ensure each newRound has clean slate. The newRound = false, when in the code is it declared to be true? or is it supposed to be true at all? I'm starting to think my java instructor is an idiot, and can't teach the subject. I've learn more here on these forums than I have during the previous 6 months in college. Thanks for the help, I won't let you down – Vincent Tyson Mar 15 '15 at 23:05
  • Love the enthusiasm. You should set the `newRound = true` whenever you want the next loop in your `while` to deal a new hand. In your case, that means to assign new values to all of the cards. – puj Mar 15 '15 at 23:06
  • So if I were to add a would you like a new game yn line to the code, I could jump back to newRound and start again? – Vincent Tyson Mar 15 '15 at 23:13
  • Absolutely! That's a great idea. Use your `Scanner` at the end of your loop and check whether you should set `newRound = true`, or exit the loop entirely. – puj Mar 15 '15 at 23:15
  • I hope someone is still following this thread! I have most of the bugs out now, I just cannot make the random card generator work correctly. Every time I try to use the int class it stops working, I've tried using it in numerous places, but still the same fail to compile. – Vincent Tyson Mar 16 '15 at 17:09
  • I'd be interested in seeing what you have now – puj Mar 16 '15 at 21:08
  • I've almost got a working model, I just need help with totals in a loop, how do I post code in the comments section? or should I start a new question? The help box says "` code `" but when I post it doesn't have the code formatting like before – Vincent Tyson Mar 16 '15 at 21:42
  • Post a new question :) – puj Mar 16 '15 at 21:44
  • totals in a loop do not add up, new question :) – Vincent Tyson Mar 16 '15 at 21:56