Results 1 to 2 of 2
  1. #1
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default help me make a word guesser

    hi

    in this code the computer will pick a random word from the list and try to guess it (the computer is guessing it).

    well in this code if the computer doesn't guess the word it will stay in an infinit loop until the computer guesses the word

    can anyone help me make a code to make the computer guess the word letter by letter

    I'm using bluej software to write the java code:

    this is my main code:

    Java Code:
    import java.util.*;
    
    // bluej is actually a bad name of a class because it should start with capital letter.
    public class WordGuesser {
    
    public static void main(String[]args) {
    System.out.println("start");
    ArrayList words = new ArrayList();
    
    
    words.add("hello");
    words.add("car");
    words.add("dog");
    words.add("plane");
    words.add("plant");
    words.add("help");
    words.add("job");
    words.add("java");
    words.add("blue");
    words.add("bathroom");
    
    Die die = new Die(words.size());
    
    System.out.println(die);
    
    
    boolean isCorrect = false;
    while(!isCorrect) {
    System.out.println("What word am I thinking of?");
    String guess = ""; // read in guess using System.in
    
    isCorrect = die.equals(guess);
    if(!isCorrect) {
    System.out.println("Sorry that wasn't correct.");
    } else {
    System.out.println("That's correct. I was thinking of " + die);
    }
    } 
    System.out.println("finish");
    }
    }

    this is the die code which it uses to pick a random word :



    Java Code:
    public class Die 
    {
      private int noFaces;
      private int faceValue;
    
    
      /**
       * @param faces the number of faces on the new die
       */
      public Die( int faces )
      {
        if (faces > 1)
        {
          noFaces = faces;
        }
        else
        {
          noFaces = 2;
        } // if
    
           // initialise faceValue as a default 
        faceValue = roll(); 
    
      } // constructor
    
    
      /**
       * @return the value the current face is to
       */
      public int getFaceValue()
      // this method may not be used in the completed game
      {
        System.out.println("Face has value: " + faceValue);
        return faceValue;
      } 
    
    
      /**
       * @param newFaceValue  the value to set the face to
       */
      public void setFaceValue(int newFaceValue)
      {
        if (newFaceValue > noFaces) {
          faceValue = noFaces; 
        }
        else {
          faceValue = newFaceValue;
        } 
    
        System.out.println("Face set to value: " + faceValue);
      } 
    
    
      /**
       * @return the value a newly-selected face is set to
       */
      public int roll() 
      {
        faceValue = (int) (Math.random() * noFaces) + 1;
        // System.out.println("New face set to value: " + faceValue);
        return faceValue;
      } 
    
    }



    and this is some code that was given to me and I was asked to use but I didn't know how to use it so if there is a way to use it please tell me how:


    Java Code:
    import java.util.Scanner;
    
    /**
     * InputReader reads text input from the text terminal. 
     */
    public class InputReader
    {
      private Scanner reader;
    
      /**
       * Create a new InputReader that reads from the terminal
       */
      public InputReader()
      {
        reader = new Scanner(System.in);
      }
    
      public String getString()
      {
        String input = reader.nextLine();
        return input;
      }
        
      public int getInt()
      {
        int input = reader.nextInt();
        reader.nextLine();
        return input;
      }
    
    }

  2. #2
    nobody58 is offline Member
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default

    I just started a 6 moths course and this is my 1st month soI'm not that good at java (don't know much so please help me)

Similar Threads

  1. positining of word
    By vividcooper in forum New To Java
    Replies: 5
    Last Post: 01-16-2010, 10:21 PM
  2. WORD Translation HELP PLZ :(
    By sammypants in forum New To Java
    Replies: 2
    Last Post: 11-24-2009, 01:46 PM
  3. Word
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 03:25 AM
  4. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:33 PM
  5. Replies: 2
    Last Post: 04-28-2008, 10:56 AM

Posting Permissions

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