Results 1 to 6 of 6
  1. #1
    Dnzt-98 is offline Member
    Join Date
    Nov 2016
    Posts
    3
    Rep Power
    0

    Default Help with code // How to use While

    How to use while, to create function play again,i need help!!

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
     
      public class Hangman {
      
          public static void main(String[] args) {
               
             Random rand = new Random();
             Scanner scan = new Scanner(System.in);
         
              String word, guess, convertDashArray;
             char check;
             int random = 0, counter = 0;
             boolean hanged = false;
             boolean matched = false;
      
              String[] alphabetArray = {" A ", " B ", " C ", " D ", " E ", " F ", " G ", " H ", " I ", " J ", " K ", " L ", " M ",
                                        " N ", " 0 ", " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ", " X ", " Y ", " Z "};
              
              String[] wordsArray = {"elephant", "star", " candy", "wings", "shopping", "steak", "couch", "toy", "cat", "mouse",
                                     "elevator", "taxi", "airplane", "bus", "car", "flower", "cheese", "apple", "orange", "coat",
                                     "running", "sweater", "blanket", "chocolate", "chicken", "computer", "rice", "dance", "doll",
                                     "duck", "alligator", "snake", "calculator", "castle", "balloon", "laptop", "turtle", "pencil",
                                     "fridge", "police", "antartica", "dinner", "fire", "cookie", "pizza", "church", "village",
                                     "pistachio", "reptile", "rose", "apartment", "random", "words", "dancing", "soccer", "hockey",
                                     "panther", "christmas", "bookworm", "buffalo", "glowworm", "ivory", "microwave", "oryx", "antelope",
                                     "arabian", "knight", "rickshaw", "january", "february", "march", "april", "may", "june", "july",
                                     "august", "september", "october", "november", "december", "dragon", "panda", "kungfu", "karate",
                                     "shark", "dolphin", "seagulls", "salmon", "crab", "whale", "leopard", "lion", "cheetah", "hippopotamus",
                                     "calendar", "racoon", "google", "yahoo", "amazon", "lamborghini", "ferrari", "lotus", "toyota", "honda",
                                     "eskimo", "igloo", "eagle", "helicopter", "globe", "jamaica", "korea", "indonesia", "japan", "nigeria",
                                     "india", "pakistan", "america", "brazil", "canada", "mexico", "switzerland", "ghana", "bangladesh", "pen",
                                     "iceland", "greenland", "cranberry", "honey", "money", "california", "texas", "virginia", "Illinois",
                                     "chicago", "massachusetts", "pennsylvania", "alaska", "florida", "louisiana", "arizona", "kentucky",
                                     "maryland", "colorado", "georgia", "tennessee", "iowa", "nebraska", "missouri", "burger", "brownie",
                                     "machine", "mechanic", "teacher", "student", "student", "office", "glass", "friend", "chair", "bed", "pug",
                                     "beagle", "bulldog", "chihuahua", "boxer", "pomeranian", "poodle", "dachshund", "bible", "easter", "button",
                                     "gymnast", "clown", "ventriloquist", "piano", "violin", "guitar", "blueberry", "envelope", "tiger",
                                     "passport", "navigate", "swimming", "dinosaur", "marshmallow", "pink", "fish", "vegetable", "potato",
                                     "plant", "bench", "window", "curtain", "plate", "yogurt", "spinach", "grass", "tomato", "smart", "parrot",
                                     "sparrow", "cloud", "jeep", "bicycle", "account", "salad", "onion", "book", "manager", "vacation", "turkey",
                                     "afghanistan", "iran", "iraq", "libya", "morocco", "spain", "germany", "scotland", "argentina", "australia",
                                     "bahamas", "austria", "bahrain", "cambodia", "nigeria", "kuwait", "croatia", "denmark", "fiji", "france",
                                     "greece", "indonesia", "ireland", "italy", "israel", "jordan", "kazakhstan", "kenya", "madagascar", "micronesia",
                                     "mauritius", "nepal", "oman", "philippines", "seychelles", "zinbabwe", "vietnam", "venezuela", "qatar", "oman",
                                     "excavator", "truck", "hedgehog", "deer", "bear", "rabbit", "tractor", "bulldozer", "treadmill", "wine"};
      
              char[] dashArray;
      
              random = rand.nextInt(wordsArray.length);
              word = wordsArray[random];
              
              dashArray = new char[word.length()];   
              
              System.out.println("------------------: HANGMAN GAME INSTRUCTIONS :------------------");
              System.out.println("1. You can enter either a letter or try to guess the word any time.");
              System.out.println("2. You have six chances before the hangman is hanged (you fail).");
              System.out.println("3. If you try to guess the word, but the guess is wrong, program will read the first letter only.");
              System.out.println("4. If you guess the word correctly, you win.");
              
              System.out.println();
      
              System.out.print("Letters available: ");
              
              for(int c=0; c<alphabetArray.length; c++)
              {
                  System.out.print(alphabetArray[c]);
              }
              
              System.out.println();
      
              for(int a=0; a<dashArray.length; a++)
              {
                  dashArray[a] = '-';
                  System.out.print(dashArray[a]);           
              }
              
             System.out.print("\t" + word.length() + " letters!");
              
              while(hanged == false)
              {        
                  System.out.println();
                  
                  System.out.print("Enter a guess (letter or word): ");
                  guess = scan.nextLine();
                  check = guess.charAt(0);
      
                  for(int b=0; b<dashArray.length; b++)
                  {                
                      if(check == word.charAt(b))                    
                      {                    
                          alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                          
                          dashArray[b] = guess.charAt(0);
                          
                          matched = true;
                     }
                     
                     else
                     {
                         alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
                     }
     
                         System.out.print(dashArray[b]);   
                 }  
                 
                 if(matched == false)
                 {
                     counter++;
                 }
                 
                 matched = false;
                 
                 convertDashArray = new String(dashArray);
                 
                 if(word.equals(guess) || convertDashArray.equals(word))  
                     {
                         System.out.println("\tGood guess, you win!");
                        
                         hanged = true;
                         
                         break;
                     }
     
                 System.out.println();   
                 
                 if(counter == 0)
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t_____|");
                 }
                 
                 else if(counter == 1)  
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t_____|");
                     hanged = false;
                 }
                 
                 else if(counter == 2)
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t_____|");
                     hanged = false;                
                 }
                
                 else if(counter == 3)
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t/|   |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t_____|");
                     hanged = false;                
                 }
                 
                 else if(counter == 4)
                 {                
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t/|\\  |");
                     System.out.println("\t\t\t\t\t     |");
                     System.out.println("\t\t\t\t\t_____|");
                     hanged = false; 
                     
                 }
                 
                 else if(counter == 5)
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t/|\\  |");
                     System.out.println("\t\t\t\t\t/    |");
                     System.out.println("\t\t\t\t\t_____|");
                     hanged = false;                 
                 }
                              else if(counter == 6)
                 {
                     System.out.println("\t\t\t\t\t _____");
                     System.out.println("\t\t\t\t\t |   |");
                     System.out.println("\t\t\t\t\t 0   |");
                     System.out.println("\t\t\t\t\t/|\\  |");
                     System.out.println("\t\t\t\t\t/ \\  |");
                     System.out.println("\t\t\t\t\t_____|");
                     System.out.println("\tSorry you lost, the word was: " + word);
                     hanged = true;                    
                 }
                 
                 System.out.println();
                 
                 System.out.print("Letters available: ");
                 
                 for(int d=0; d<alphabetArray.length; d++)
                 {                         
                     switch (guess) 
                     {
                         case "a":
                             alphabetArray[0] = " ";
                             break;
                         case "b":
                             alphabetArray[1] = " ";
                             break;
                         case "c":
                             alphabetArray[2] = " ";
                             break;
                         case "d":
                             alphabetArray[3] = " ";
                             break;
                         case "e":
                             alphabetArray[4] = " ";
                             break;
                         case "f":
                             alphabetArray[5] = " ";
                             break;
                         case "g":
                             alphabetArray[6] = " ";
                             break;
                         case "h":
                             alphabetArray[7] = " ";
                             break;
                         case "i":
                             alphabetArray[8] = " ";
                             break;
                         case "j":
                             alphabetArray[9] = " ";
                             break;
                         case "k":
                             alphabetArray[10] = " ";
                             break;
                         case "l":
                             alphabetArray[11] = " ";
                             break;
                         case "m":
                            alphabetArray[12] = " ";
                             break;
                         case "n":
                             alphabetArray[13] = " ";
                             break;
                         case "o":
                             alphabetArray[14] = " ";
                             break;
                         case "p":
                             alphabetArray[15] = " ";
                             break;
                         case "q":
                             alphabetArray[16] = " ";
                             break;
                         case "r":
                             alphabetArray[17] = " ";
                             break;
                         case "s":
                             alphabetArray[18] = " ";
                             break;
                        case "t":
                             alphabetArray[19] = " ";
                             break;
                         case "u":
                             alphabetArray[20] = " ";
                             break;
                         case "v":
                             alphabetArray[21] = " ";
                             break;
                         case "w":
                             alphabetArray[22] = " ";
                             break;
                         case "x":
                             alphabetArray[23] = " ";
                             break;
                         case "y":
                             alphabetArray[24] = " ";
                             break;
                         case "z":
                             alphabetArray[25] = " ";
                             break;
                     }
                                     
                    System.out.print(alphabetArray[d]);
                 }
             }  
         }
     }
    Last edited by Norm; 11-11-2016 at 04:34 AM. Reason: Size removed

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help with code // How to use While

    Can you explain what your problem is?

    What does the code do now?
    What do you want to change about the way it executes?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Dnzt-98 is offline Member
    Join Date
    Nov 2016
    Posts
    3
    Rep Power
    0

    Default Re: Help with code // How to use While

    Hello,I want to create a way when the game is over, the "Play Again" option appears. Where and how to put this in my code, this "while" function.

  4. #4
    Dnzt-98 is offline Member
    Join Date
    Nov 2016
    Posts
    3
    Rep Power
    0

    Default Re: Help with code // How to use While

    Quote Originally Posted by Norm View Post
    Can you explain what your problem is?

    What does the code do now?
    What do you want to change about the way it executes?
    Hello,I want to create a way when the game is over, the "Play Again" option appears. Where and how to put this in my code, this "while" function.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with code // How to use While

    The first thing to do is break your code up into methods.
    Trying to add a loop around that lot will make it even messier than it already is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with code // How to use While

    Here are a couple other observations. You have already shown here

    Java Code:
    alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
    That you understand character arithmetic to help index into an array.

    Yet in lines 210 thru 290 you have this brute force switch statement. With a little
    thought, you might find a cleaner way to index those values. And you should
    have a default case statement (just in case - no pun intended).

    And, when you print out the hangman, why not just use an array to build the
    hangman and add the body parts (ugh - sounds macabre) to the array as
    appropriate. Then call a method to print out the hangman.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Posting code to forum : Code to code tags
    By rushtojess in forum Suggestions & Feedback
    Replies: 2
    Last Post: 04-21-2015, 09:16 PM
  2. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •