-4

I want to create a hangman game in Java, but I'm unsure on how to set the whole thing up. I am working on a system that the more letters they have, they have a less chance of appearing on the hangman puzzle.

    import java.ultil.Scanner;
    public class Hangman {
      public static void main(string[]args); // Let's try a main?
      scr = newScanner(system.in); // used for keyboard input
      string words = "Hi:Bye:Hello:Cheese:Puppies";
      int length = words.length();
      }
    }

How can get a random word from the variable, "words", and calculate it's length?

Please keep in mind, I'm not the best at coding Java.

1vannn
  • 59
  • 1
  • 1
  • 5
  • 4
    Put the words in a list and choose one at a random index. – Sotirios Delimanolis May 14 '13 at 18:08
  • 9
    Your snippet is full of syntax errors. – Jivings May 14 '13 at 18:09
  • @Jivings I was confused with void(string[] args) – Nick Freeman May 14 '13 at 18:09
  • Where is the `main`? Why not use an array considering your `int length` will return a useless value? – Tdorno May 14 '13 at 18:18
  • 2
    In all fairness, someone whose Java skill level is reflected in the code posted in this question is not ready to tackle anything else than "my first Java program" tutorial. – Marko Topolnik May 14 '13 at 18:19
  • When creating your dictionary, select words such that you have more short words than long words. Then the odds of getting a short word will be greater than the odds of getting a long word. – rob May 14 '13 at 18:20
  • I do not deny your reasoning here, Marko. When I was writing this question, I began to realize that I knew maybe two, or three things in Java. Thanks for knocking some sense into me, back to the textbooks. – 1vannn May 14 '13 at 18:52
  • Download an IDE like Eclipse. It's a free development program which makes it really easy to code in Java. – Haque1 May 14 '13 at 18:55
  • 1
    Haque1, that's what I'm doing right now. :) Thanks for the suggestion. :) – 1vannn May 14 '13 at 19:03
  • Scrappedcola, I don't think this is a copycat post. It's just an attempt at making a game, which turned out to be a big problem that needs to be fixed. – 1vannn May 14 '13 at 19:04

5 Answers5

4

This answers the part with picking a random word and finding its length.

String words = "Hi:Bye:Hello:Cheese:Puppies";
String[] wordsAsArray = words.split(":");

int index = new Random().nextInt(wordsAsArray.length);

String randomWord = wordsAsArray[index];
System.out.println("Random word: '" + randomWord + "'. It is of length: " + randomWord.length());
machinery
  • 3,793
  • 4
  • 41
  • 52
3

If you want words with the highest counts to be used, then your question shouldn't be about a random word. It should be about sorting words by length, THEN picking a random word from the top N longest words

Create an array and add your words

    StringTokenizer tokenizer = new StringTokenizer("Boy:Chicken:mango", ":");

    String [] words = new String [tokenizer.countTokens()];
            int counter =0;
            while (tokenizer.hasMoreElements()) {
                String word = (String) tokenizer.nextElement();
                words[counter] = word;
                counter++;
            }

If you want to pick words with highest count, then sort the words here by highest count.

You could place in hashmap, then iterate picking the ones with highest

HashMap<String, Integer> count = new HashMap<String, Integer>();

    for (String word : words) {
        if (!count.containsKey(word)) {
            count.put(word, word.length());
        }
    }

Pick a random word

Random numGen= new Random();
String word = words [numGen.nextInt(words.size)];

For an actually efficient sort, you need to write your own comparator for the words (based on length, but for longer words first), then create a PriorityQueue, add the words in there and when you do remove() you'll get the word with highest length.

William Falcon
  • 9,813
  • 14
  • 67
  • 110
1
String words = "Hi:Bye:Hello:Cheese:Puppies";

String[] wordsSplit = words.split(":");

And then randomize the resultant array. But like others have pointed out, you might as well start with an array of words.

See the documentation: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

You've got syntax errors. Get an IDE.

Nico
  • 3,471
  • 2
  • 29
  • 40
  • Your absolutely right, I wrote that script in class without Eclipse, and since I'm pretty new to the language, I understand that I would really need an IDE. – 1vannn May 14 '13 at 18:57
1

To get the length of any given String just use:

string.length();

In order to get a random word (and each word have different chances of appearing depending on its length), first you need to add your words to some sort of list, like this:

ArrayList<String> words = new ArrayList<String>();
words.add("word");
words.add("word2");
words.add("etc");

The following method will return a random word from the list. The longer the word, the less chances it has of being selected:

String selectRandomWord(ArrayList<String> words){
int lengthOfLongestWord = 200;
List<Integer> wordsTimesLength = new ArrayList<Integer>();

for (int i = 0;i<words.size();i++){
  for (int e = 0;e<Math.pow(words.get(i).length,-1)*lengthOfLongestWord;e++){

  wordsTimesLength.add(i);
  }
}

int randomIndex = generator.nextInt(wordsTimesLength.size());

return words.get(wordsTimesLength.get(randomIndex));
}

Note you need to change lengthOfLongest to if you have a word with more than 200 characters (I don't think there is any but just in case). To use the method you can simply call it like this:

selectRandomWord(words);
0x6C38
  • 6,796
  • 4
  • 35
  • 47
0

Here's some code that should work for what you're trying to do:

public class Hangman {

    public static void main(String[] args) {
        Scanner scr = new Scanner(System.in); //Used for keyboard input
        List<String> words = new ArrayList<String>(); {
            words.add("Hi");
            words.add("Bye");
            words.add("Hello");
            words.add("Cheese");
            words.add("Puppies");
        }

        Random numberGenerator = new Random(); //Creates Random object
        int randomNum = numberGenerator.nextInt(words.size()); //Return a num between 0 and words.size()-1

        System.out.println(randomNum); //Prints the random number outputted by the generator
        System.out.println(words.get(randomNum)); //Retrieves the String located at the index value 
        System.out.println(words.get(randomNum).length()); //Returns the size of the words
    }
}
Haque1
  • 575
  • 1
  • 11
  • 21
  • His question was: "How can get a random word from the variable, "words", and calculate it's length?" My answer get's him both the word and its length-- the rest is up to him to figure out – Haque1 May 14 '13 at 18:47
  • Good point. My plan would be to separate the words into difficulty groups, such as Hard, Medium, and Easy. I'm not entirely sure where I'll go from there though. – 1vannn May 14 '13 at 18:55