Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-15-2010, 02:48 PM
Member
 
Join Date: Mar 2010
Posts: 14
Rep Power: 0
nobody58 is on a distinguished road
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:

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 :



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:


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

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-15-2010, 02:49 PM
Member
 
Join Date: Mar 2010
Posts: 14
Rep Power: 0
nobody58 is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
positining of word vividcooper New To Java 5 01-16-2010 10:21 PM
WORD Translation HELP PLZ :( sammypants New To Java 2 11-24-2009 01:46 PM
Word right2001 New To Java 2 04-07-2009 03:25 AM
Word OLE Java Tip SWT 0 07-25-2008 02:33 PM
How to make online jsp forms from Microsoft word forms in java jiten.mistry Advanced Java 2 04-28-2008 10:56 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 06:00 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org