Share on Google+Share on Google+

Jallal
Not sure what I am missing ? Any ideas?
1 Answer(s)      5 years ago
Posted in : Java Beginners

import java.util.*; public class HangMan2 { char guess; String userInputs; String word; static String[] userInput; static Scanner scan = new Scanner(System.in); static Random rand = new Random();

    // The following routine will determin if the character c
    // is inside the String str.  A true is returned if it is inside.

    static boolean isIn(char c, String str)
    {

        if(str.length()==0)
            return false;

        for(int i=0;i<str.length();i++)

            if (c==str.charAt(i));
        return true;
    }

    // If userInputs contains "ard" and strToGuess contains "aardvark" then
    // the following routine prints out an output that looks something like:
    //
    // Current Status for userInpts=ard
    // a a r d _ a r _

    // This routine returns true if all letters were guessed, otherwise false is returned.

     static boolean printCurrStatus(String strToGuess, String userInputs)
           {


         String status="";
         for (int i1=0;i1<strToGuess.length();i1++)
         {
         status+="_";
         }
         System.out.println(status); 

         for (int i2=0;i2<strToGuess.length();i2++)
         {
         for (int i3=0;i3<userInputs.length();i3++)
         {
         char c=userInputs.charAt(i3);

         if (userInputs.charAt(i3)==strToGuess.charAt(i2))
         {

         int len =status.length();
         String substr=status.substring(0,i2);
         substr+=c;
         substr+=status.substring(i2+1,len);
         status=substr;
         System.out.println(status);


    }
         }
         }

//** Fill in Details

if (status.equals(strToGuess)) return true; else return false; }

    // The following routine will return a random String from the list of words:
    // elephant, tiger, monkey, baboon, barbeque, giraffe,  simple, zebra, 
    // porcupine, aardvark

    static String getNextWordToGuess()
    {

             Random generator = new Random();
              String[] array  = { "aardvark","porcupine","zebra","simple","giraffe","barbeque","baboon", "monkey","tiger","elephant"};
              int rnd = generator.nextInt(array.length);

              return array[rnd];




        //********** Fill in Details
        // HINT: a switch statement can be quite useful here
    }

    // The following routine plays the hangman game. It calls getNextWordToGuess to
    // get the word that should be guessed.  It then has a loop which outputs the 
    // following prompt:
    // Enter next letter
    //
    // A String named userInputs stores all letters selected already.  
    // Then the routine printCurrStatus is called to print out the current status of
    // the guessed word.  If printCurrStatus returns true, we are done.

    static void playGame()
    {  
        String word = HangMan2.getNextWordToGuess();
        boolean current=true;


    String userInputs;

        do{


            System.out.println("Hello, and welcome to Hangman!");
                System.out.println("Enter next letter");
                userInputs= scan.next();

                current= HangMan2.printCurrStatus(word,userInputs);

                int l=word.length();
                String [] userInput=new String[l-1];

                userInput[l-1]=userInputs;
                 System.out.println(userInput[l-1]);

        }while(printCurrStatus(word, userInputs)==false);







    }

//********** Fill in Details


// main will call playGame to play the hangman game.
// Then main will issue the prompt:
// Do you want to play again (y or n)
// If the answer is "y", then call playGame again, otherwise exit

public static void main(String[] args)
{
    playGame();
    String retry_ans;

      boolean retry= false;
      do{
    System.out.println("Would you like to try again?");
    retry_ans = scan.next();
    if(retry_ans.equalsIgnoreCase("yes"))
    {
        retry = true;
        playGame();
    }

    }while(retry = true);


}
}

Ads

View Answers

March 31, 2012 at 12:59 PM


No one really......???????










Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads