Software & Finance





Java - Guess the Secret Word Sequentially Step by Step





 

Here you can find the complete source code Guessing a word Sequentially in Java Programming Language. You need to guess the first character of the word first and then the second character and move on.

 

The program will read the words from the given input file and will pick up a word and store it as a secret word. It starts with displaying the number characters as "*" and the following sequence is self explanatory.

 

 

Your secret word is: ********

Guess now  (To stop the program, enter #) :

tec

********

com

com*****

comcomp

com*****

comcom

com*****

comp

comp****

compu

compu***

comput

comput**

computr

comput**

computr

comput**

compute

compute*

computer

Congrats! You have guessed it correctly

 

 

 

 

 

Source Code


import java.io.*;
import java.lang.*;
import java.util.*;
 
class GuessWord
{
      public static int ReadWordsFromFile(String[] words)
      {
            try
            {
                  FileReader fr = new FileReader("words_input.txt");
                  BufferedReader br = new BufferedReader(fr);
                  int count = 0;
                  for (int i = 0; i < 100; i++)
                  {
                        String s = br.readLine();
                        if (s == null)
                              break;
                        words[count++] = s;
                  }
                  fr.close();
                  return count;
            }
            catch (FileNotFoundException e)
            {
                  System.out.println(e.getMessage());
                  return -1;
            }
            catch (IOException err)
            {
                  System.out.println(err.getStackTrace());
                  return -1;
            }
      }
 
      static public String ReadString()
      {
            try
            {
                  String inpString = "";
                  InputStreamReader input = new InputStreamReader(System.in);
                  BufferedReader reader = new BufferedReader(input);
                  return reader.readLine();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return "";
      }
 
      public static void main(String[] args)
      {
            System.out.println("Welcome to Guess a Word\n");
 
            String[] words = new String[100];
           
            int count = ReadWordsFromFile(words);
            if (count < 0)
            {
                  System.out.println("No words found in the file");
                  return;
            }
 
            if (words == null)
                  return; // Exception message was already shown
            int x = (int)(Math.random() * 100);
            int guessX = (x % count);
 
            String secretWord = words[guessX];
 
            int numChars = secretWord.length();
 
            System.out.print("Your secret word is: ");
            for(int i = 0; i < numChars; i++)
                  System.out.print("*");
            System.out.println();
 
            boolean bGuessedCorrectly = false;
            System.out.println("Guess now  (To stop the program, enter #) : ");
            int nextcharToPrint = 0;
            
            while (true)
            {
                  String choice = "";
                  if(nextcharToPrint > 0)
                  {
                     choice = secretWord.substring(0, nextcharToPrint);
                     System.out.print(choice);
                  }
                  choice += ReadString();
                  
 
                  if (choice.startsWith("#"))
                        break;
                  if (choice.compareTo(secretWord) == 0)
                  {
                        bGuessedCorrectly = true;
                        break;
                  }
                  for (int i = 0; i < numChars; i++)
                  {
                        if (i < secretWord.length() &&
                              i < choice.length())
                        {
                              if(i < nextcharToPrint)
                              {
                                 System.out.print(choice.charAt(i));
                                 continue;
                              }
                              if (secretWord.charAt(i) == choice.charAt(i))
                              {
                                    if(i == nextcharToPrint)
                                    {
                                       System.out.print(choice.charAt(i));
                                       nextcharToPrint++;
                                    }
                                    else
                                       System.out.print("*");
                              }
                              else
                                    System.out.print("*");
                        }
                        else
                              System.out.print("*");
                  }
                  System.out.println();
 
            }
            if (bGuessedCorrectly == false)
                  System.out.println("Unfortunately you did not guess it correctly. The secret word is: " + secretWord);
            else
                  System.out.println("Congrats! You have guessed it correctly");         
      }
}    
    

Output


// contents of words_input.txt

computer

science

school

schooling

information

technology

 

 

Welcome to Guess a Word 

 

Your secret word is: ********

Guess now  (To stop the program, enter #) :

tec

********

com

com*****

comcomp

com*****

comcom

com*****

comp

comp****

compu

compu***

comput

comput**

computr

comput**

computr

comput**

compute

compute*

computer

Congrats! You have guessed it correctly