Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trying to restart a loop at a certain point

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trying to restart a loop at a certain point

    So I made a blackjack code in java and I need to find a way to replace the place where I added a system.exit with a way to ask the user if they'd like to play again and restart the loop, keep in mind that I don't need the program to restart as I'd like to keep the value of their chips considering if they've won or lost. Can anybody help me?

    Secondly, because it is a blackjack code, when it deals the cards, I would like for it to also print out K, Q or J but still consider it the number 11. Any ideas as to how I can do that?

    Can I make an Ace count as 1 and 11?

    Thanks so much, please remember, I am a very amateur programmer. I have just started coding so my knowledge is very limited. I am completely willing to learn however. Thanks so much and all input is appreciated.

    One last question, is there anyway to add the suits of the cards such as (clubs, spades etc.) but the actual signs and if the signs aren't possible at all then the letter ('C', 'S', 'H', 'J') will have to do I guess. Thanks again, looking forward to the responses.

    import java.util.Scanner;
    class Blackjack {
      public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        Scanner num = new Scanner (System.in);
        System.out.println("Welcome to Blackjack!");
        System.out.println("Here are 500 chips, enjoy your stay!");
        int rndmdel, rndmusercard1, rndmusercard2, rndmusersum,
          cardsuit=0, cardcount=1, counter=0, chips =500, bet=0;
        String userchoice = "hit";
        String tryagain = "";
        while (chips >0) {
          userchoice = "hit";
          rndmusersum=0;
          rndmdel = (int)((11-1+1)*Math.random()+1);
          rndmusercard1 = (int)((11-1+1)*Math.random()+1);
          rndmusercard2 = (int)((10-1+1)*Math.random()+1);
          System.out.println("How many chips would you like to bet?");
          bet = num.nextInt();
          if (bet/chips ==1 ) {
            System.out.println("All IN!");
          }else if (bet >= (chips/2)) {
            System.out.println("Oooooh! Big Baller!");
          }else if (bet <= (chips/10)) {
            System.out.println("Oh c'mon! Live a little, will ya?");
          }try {
            Thread.sleep(2000);
          } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
          }
          System.out.println("The dealer deals the cards!");
          try {
            Thread.sleep(2000);
          } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
          }
          System.out.println("The dealer's first card is " + rndmdel);
          try {
            Thread.sleep(2000);
          } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
          }
          System.out.println("Your first card is "+ rndmusercard1);
          try {
            Thread.sleep(2000);
          } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
          }
          System.out.println("Your second card is "+rndmusercard2);
          try {
            Thread.sleep(2000);
          } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
          }
          rndmusersum = rndmusercard1+rndmusercard2;
          System.out.println("The sum of your cards is " + rndmusersum);
          while (rndmusersum < 21 && userchoice.equalsIgnoreCase("hit")) {
            System.out.println("Would you like to hit or stay?");
            userchoice = scan.nextLine();
            if (userchoice.equalsIgnoreCase("hit")) {
              rndmusercard1 = (int)((11-1+1)*Math.random()+1);
              rndmusersum = rndmusersum + rndmusercard1;
              System.out.println("HIT ME!.....");
              try {
                Thread.sleep(400);
              } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
              }
              System.out.println(rndmusersum);
            }else {
              System.out.println(".......staaay!");
              System.out.println("You decided to stay at " +rndmusersum);
            }
            if (rndmusersum == 21) {
              System.out.println("Congratualtions! You win!");
              System.out.println("You just won " + bet + " chips!");
              chips = chips + bet;
              System.out.println("You now have " + chips + " chips!");
              try {
                Thread.sleep(2000);
              } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
              }
              System.exit(0);
            }
            else if (rndmusersum > 21) {
              System.out.println("BUSTED! Sorry you lose!");
              System.out.println("Unfortunately you lost " + bet + " chips!");
              chips = chips - bet;
              System.out.println("You now have " + chips + " chips!");
              try {
                Thread.sleep(2000);
              } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
              }
              System.exit(0);
            }
          }
          System.out.println("The dealer's first card was " +rndmdel);
          while (rndmdel <= rndmusersum) {
            rndmusercard1 = (int)((11-1+1)*Math.random()+1);
            rndmdel += rndmusercard1;
            System.out.println(".");
            try {
              Thread.sleep(200);
            } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
            }
            System.out.println("...");
            try {
              Thread.sleep(500);
            } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
            }
            System.out.println("...... HIT!");
            try {
              Thread.sleep(900);
            } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
            }
            System.out.println("The dealer gets a " +rndmusercard1);
            System.out.println("The dealer's total is " + rndmdel);
          }
          if (rndmdel > 21) {
            System.out.println("The dealer BUSTS! Congratuations! You win!"); 
            System.out.println("You just won " + bet + " chips!");
            chips = chips + bet;
            System.out.println("You now have " + chips + " chips!");
          }
          else if (rndmdel <= 21 & rndmdel > rndmusersum) {
            System.out.println("Sorry, the dealer wins this round");
            System.out.println("Unfortunately you lost " + bet + " chips!");
            chips = chips - bet;
            if (chips == 0) {
              System.out.println("All out of chips huh?!? Sorry, come back again later"); 
            } else {
              System.out.println("You now have " + chips + " chips!");
            }
          }
        }
      }
    }
     
    //replace system.exits with a keeping playing option
    // add K J and Q and A (1 and 11) 
    // incorporate arrays, methods and printstreams.


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Trying to restart a loop at a certain point

    You might want to think it as a state machine. So that you have a main loop and what it runs depends on some state indicator. Something like this:

    String gameState = "INITIALIZE"; // variable to keep track of the current game state
    boolean isRunning = true; // flag to keep the main loop running
     
    while (  isRunning ) { // the main loop
     
     
      switch ( gameState ) {
        case "INITIALIZE":
              /* here you set up your stuff, the chip amounts and maybe ask for a name etc.
              when you have  all the necessary stuff set up, you do
              gameState = "PLAY"; so that on the next loop the game will go to the next case; */
     
              gameState = "PLAY";
              break;
     
        case "PLAY": 
              /* here you run the game stuff, and when the end conditions are met, 
              you ask the user if he wants to quit or start another run.
     
              If the user says quit, then do gameState = "QUITTING" to do quitting stuff or if the user says
              new game, change the gameState to "INITIALIZE" to init a new game.*/
     
              gameState = "QUITTING";
              break;
     
         case "QUITTING": 
               /* display bye bye message, maybe save results, close streams and all that. Then make isRunning = false; so that the main loop will end 
               on the next roll. */
     
               System.out.println("Bye");
               isRunning = false;
               break;
      }
    }

    That way you have a clear structure on the game and its always clear on what "state" the game is in at each point. Plus it allows you to transition from state to state easily. For example if you eventually would like a build menu, you could add state "SHOW_MENU" and so on. Hope you got the point from all this rambling.

  3. The Following User Says Thank You to Wizand For This Useful Post:

    cod (June 8th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Trying to restart a loop at a certain point

    Quote Originally Posted by cod View Post
    So I made a blackjack code in java and I need to find a way to replace the place where I added a system.exit with a way to ask the user if they'd like to play again and restart the loop
    In general you can use the continue keyword to skip the current iteration of a loop to the end to start a new iteration. See Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics).

    Quote Originally Posted by cod View Post
    Secondly, because it is a blackjack code, when it deals the cards, I would like for it to also print out K, Q or J but still consider it the number 11. Any ideas as to how I can do that?
    If I'm not mistaken in blackjack face cards, i.e., J, Q and K, are worth 10 points each, not 11. When you generate a random number to represent the value of a card, generate a number between 1 and 13, inclusive, with 11, 12 and 13 representing J, Q and K, respectively. After that use an if statement to check if the random number is 11 or greater. If it is, change it back to 10. This give you a more accurate spread of probability of getting a card with a value of 10. Of course if you want it to be even more accurate, you can, for instance, use a Collection or a List to store card values from a predetermined number of deck of cards. Each time a card is dealt, remove the card value from the Collection/List. E.g., if you're using 1 deck, removing a 2 after it is dealt will prevent the possibility of dealing 5 2s to the player.

    Quote Originally Posted by cod View Post
    Can I make an Ace count as 1 and 11?
    It is the player's choice to treat an Ace as 1 or 11. Therefore after dealing a 1, you can write the code to ask the player for the value the player wants to use. You can add some intelligence to check the total value of cards after dealing a 1, and only ask the player if treating the 1 as 11 doesn't bust the player.

    Quote Originally Posted by cod View Post
    One last question, is there anyway to add the suits of the cards such as (clubs, spades etc.) but the actual signs and if the signs aren't possible at all then the letter ('C', 'S', 'H', 'J') will have to do I guess.
    I'm afraid I don't quite understand this question...

  5. #4
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: Trying to restart a loop at a certain point

    There is a BIG, and I mean "BIG" problem in your blackjack program. Consider the following Output I just got from your program:

    Welcome to Blackjack!
    Here are 500 chips, enjoy your stay!
    How many chips would you like to bet?
    10000
    Oooooh! Big Baller!
    The dealer deals the cards!
    The dealer's first card is 10
    Your first card is 10
    Your second card is 1
    The sum of your cards is 11
    Would you like to hit or stay?
    hit
    HIT ME!.....
    20
    Would you like to hit or stay?
    stay
    .......staaay!
    You decided to stay at 20
    The dealer's first card was 10
    .
    ...
    ...... HIT!
    The dealer gets a 3
    The dealer's total is 13
    .
    ...
    ...... HIT!
    The dealer gets a 2
    The dealer's total is 15
    .
    ...
    ...... HIT!
    The dealer gets a 1
    The dealer's total is 16
    .
    ...
    ...... HIT!
    The dealer gets a 1
    The dealer's total is 17
    .
    ...
    ...... HIT!
    The dealer gets a 1
    The dealer's total is 18
    .
    ...
    ...... HIT!
    The dealer gets a 5
    The dealer's total is 23
    The dealer BUSTS! Congratuations! You win!
    You just won 10000 chips!
    You now have 10500 chips!
    How many chips would you like to bet?


    Now You understand. I won 10000 chips whereas I had 500 only. Moreover, the dealer just hit 6 times! Max is 5. Also, where is double and split? I have solved these mistakes but I am just pointing them out for now. Solve them by yourself and don't forget to add user input prompt to ask whether user wants to continue or quit. (If user quits, give him (totalchips - 500)chips since 500 chips were given for free. If he gets 500 chips and chooses to quit, he gets 500-500 chips = 0 chips instead of 500 given for free. If result is negative then the person owes you otherwise, he takes what he won, just what he won (his profit). )

Similar Threads

  1. how to restart in java japplet?
    By game06 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 27th, 2013, 02:03 PM
  2. how to restart applet without closing it
    By hwoarang69 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2013, 07:03 PM
  3. how to restart my program
    By jack_nutt in forum Java Theory & Questions
    Replies: 15
    Last Post: November 16th, 2011, 07:39 PM
  4. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM

Tags for this Thread