SOEN_student
SOEN_student

Reputation: 3

How can I randomly display a single character from a given word

Here is an update as to where I am at and what I am stuck on based on what @camickr suggested. The issue that I am coming across now is that since I have to have a return statement at the end I can only return the ArrayList letters.

Also every time the hint button is pressed only one character appears in the solution location and it is [], yet no actual letters that make up the solution appear.

public String generateLetterHint(int count, String word) {

    String[] answerArray = word.split("");
    ArrayList<String> letters = new ArrayList<String>(Arrays.asList(answerArray));
    //StringBuilder string = new StringBuilder();
    Collections.shuffle(letters);

    while (!letters.isEmpty()) {
        String letter = letters.remove(0);
        System.out.println(letter);
    }
    return letters.toString();
}    

Any help is appreciated!

Upvotes: 0

Views: 139

Answers (2)

camickr
camickr

Reputation: 324128

One way it to add each (unique) letter of the String to an ArrayList.

Then you can use the Collections.shuffle(...) method to randomize the letters.

Each time the "Hint" button is pressed you:

  1. get the letter at index 0
  2. "remove" the letter from position 0
  3. give the hint.

Now the next time the "Hint" button is clicked there will be a different letter at index 0.

Of course each time the user guesses a correct letter you would need to "remove" that letter from the ArrayList as well.

Edit:

Simple example showing proof of concept:

import java.util.*;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        String answer = "answer";

        String[] answerArray = answer.split("");
        ArrayList<String> letters = new ArrayList<String>(Arrays.asList(answerArray));

        Collections.shuffle( letters );

        while (!letters.isEmpty())
        {
            String letter = letters.remove(0);
            System.out.println(letter);
        }
    }
}

In you real code you would only create the ArrayList once and do the shuffle once when you determine what the "answer" word is.

Then every time you need a hint you can simply invoke a method that does:

public String getHint(ArrayList letters)
{
    return (letters.isEmpty() ? "" : letters.remove(0);
}

This will simply return an empty string if there are no more hints. Although a better solution would be to disable the hint button once the hints are finished.

Upvotes: 3

Jonathan Georgelous
Jonathan Georgelous

Reputation: 3

Its working for only one answer. You can modify then work with multiple answer at the same time. When you you send a string to function, it gives you a letter that is unique inside from the string.

package com.Stackoverflow;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomlyString {
    private static List<Character> selectableLetters;   // it store randomly selectable letters
    private static boolean isFirst = true;

    private static char letterHint(String answer) {
        Random rnd = new Random();

        // when function starts in first time, split the string letter by letter
        if (isFirst) {
            selectableLetters = splitString(answer);
        }
        isFirst = false;

        if(!selectableLetters.isEmpty()) {
            int hintIndex = rnd.nextInt(selectableLetters.size());  // select a letter randomly
            char hint = selectableLetters.get(hintIndex);   // put this letter to hint
            selectableLetters.remove(hintIndex);    // then remove this letter from selectableLetters, this is for don't select the same letter twice
            return hint;
        } else {
            System.out.println("There is no hint!");
            return ' ';
        }
    }

    private static List<Character> splitString(String string) {
        List<Character> chars = new ArrayList<>();

        // split the string to letters and add to chars
        for (char c: string.toCharArray()) {
            chars.add(c);
        }
        return chars;
    }

    public static void main(String[] args) {
        String answer = "Monkey";

        for(int i=0; i<7; i++) {
            System.out.println("Hints: " + letterHint(answer)); // it writes hint to screen
            System.out.println("Other letters: " + selectableLetters);  // currently selectable letters for hint
        }
    }
}

Upvotes: 0

Related Questions