0

Making a hangman style of game

I have the random word now. How do I replace the letters of the word with an asterix * so that when the program starts the word is shown as *.

I assume that when someone inputs a letter for the hangman game you get the index of that character in the word and then replace the corresponding *.

public class JavaApplication10 {

public static String[] wordArray = new String[1];
public static String file_dir = "Animals.txt";
public static String selectedWord = "";
public static char[] wordCharacter = new char[1];
Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws IOException {
    wordArray = get_word(file_dir);
    selectedWord = select_word(wordArray);

    System.out.println(selectedWord);  
}

public static String[] get_word(String file_dir) throws IOException {
    FileReader fileReader = new FileReader(file_dir);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    List<String> lines = new ArrayList<String>();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    return lines.toArray(new String[lines.size()]);
}

public static String select_word(String[] wordArray) {
    Random rand = new Random();
    int lines = Math.abs(rand.nextInt(wordArray.length)- 1);
    return wordArray[lines];
}

}

Jay W
  • 1
  • 2
  • "I just can't seem to get it to work properly" most likely you are doing something wrong, but we can't guess what it would be unless we see what you have done. – Peter Lawrey Sep 13 '15 at 09:50
  • Conceptually, to answer first part, if you know how many words are there in `Animals.txt`, just generate a random number between 1 and number of words in `Animals.txt` using `Random` class, start reading your file and break the loop when it equals to random number, and you have your random word. But again please put some source code if you want help with your solution – Shrikant Havale Sep 13 '15 at 09:56

3 Answers3

0

If you know how many lines are there you could use Random method in java with a specific range to pick out a line at random.

Then you could read the file line-by-line till you reach that random line and print it.

// Open the file
FileInputStream fstream = new FileInputStream("testfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

String strLine;
int counter=0;

//While-loop -> Read File Line By Line till the end of file
//And will also terminate when the required line is printed 
while ((strLine = br.readLine()) != null && counter!=randomValue){
    counter++;

    //You need to set randomValue using the Random method as suggested
    if(counter==randomValue)
        // Print the content on the console
        System.out.println (strLine+"\n");
}

//Close the input stream
br.close();
Riddhesh Sanghvi
  • 1,218
  • 1
  • 12
  • 22
0

Assuming Java 8:

// Loading ...
Random R = new Random(System.currentTimeMillis());
List<String> animals = Files.readAllLines(Paths.get(path));

// ...
// When using
String randomAnimal = animals.get(R.nextInt(animals.size()));
nobeh
  • 9,784
  • 10
  • 49
  • 66
  • 1
    You're right. I was playing to see if I can use a proper stream method for random access (e.g. `findAny`) but forgot to remove in the end. – nobeh Sep 13 '15 at 10:13
0

Answer of your first question :

  1. First you have to get the total number of lines
  2. Then you have to generate a random number between 1 and that total number.
  3. Finally, get the required word

     try {
            InputStream is = new BufferedInputStream(new FileInputStream("D:\\test.txt"));
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            boolean empty = true;
            while ((readChars = is.read(c)) != -1) {
                empty = false;
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n') {
                        ++count;
                    }
                }
            }
            int noOfLines = count+1;
            System.out.println(noOfLines);
            Random random = new Random();
            int randomInt = random.nextInt(noOfLines);
            FileReader fr = new FileReader("D:\\test.txt");
            BufferedReader bufferedReader = 
                new BufferedReader(fr);
            String line = null;
            int counter =1;
            while((line = bufferedReader.readLine()) != null) {
                if(counter == randomInt)
                {
                  System.out.println(line); // This the word you want
                }
                counter++;
    
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } 
        finally {
            //is.close();
        }
    
Rahman
  • 3,755
  • 3
  • 26
  • 43