Vincent Tyson
Vincent Tyson

Reputation: 29

Simple blackjack game; gone awry: help needed

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.

Upvotes: 0

Views: 175

Answers (3)

MarsAtomic
MarsAtomic

Reputation: 10673

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 );

Upvotes: 0

puj
puj

Reputation: 770

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.

Upvotes: 0

mk.
mk.

Reputation: 11710

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.

Upvotes: 2

Related Questions