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