Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 16 Secret Word Game! objective: Write a class that will be used by a program

ID: 3861256 • Letter: L

Question

Lab 16 Secret Word Game! objective: Write a class that will be used by a program driver to play a word guessing game. This game works similar to, "Hangman" where the player only has 5 tries to guess the word. They enter in a letter at a time and those letters are revealed in the word. Unlike hangman each turn counts and not just every time they pick a letter that's not in the word. First download the driver, and place it into the source folder (src) of your project. Next create a class called SecretWord This class has three instance variables o secretWord: the word the user has to guess o hintWord: the word with the guess letters revealed o numberOfTurns: keeps track of the number of guesses This class only requires a default constructor o You set the secret word o Number of turns to a default value of 0 The hint word is constructed using asterisk for every letter that's in the secret word Accessors for every instance variable Mutators for every instance variable o CHECK FOR VALID VALUES! Other methods o guess Letter: This method returns nothing and requires a single character parameter. The whenever the letter is found in the secret word, then that letter replaces that a in the hint word. HINT: The string method toCharArray may be very useful.

Explanation / Answer


public class SecretWord {
   private String secretWord;
   private StringBuilder hintWord;
   private int numOfTurn;
  
   public SecretWord(){
       numOfTurn = 0;
   }
   public void guessLetter(char ch){
       for(int i=0;i<secretWord.length();i++){
           if(secretWord.charAt(i)==ch){
               hintWord.setCharAt(i, secretWord.charAt(i));
           }
       }
   }
   public String getSecretWord() {
       return secretWord;
   }
   public void setSecretWord(String secretWord) {
       this.secretWord = secretWord;
       this.hintWord = new StringBuilder(secretWord);
       for(int i=0;i<secretWord.length();i++){
           hintWord.setCharAt(i, '*');
       }
   }
   public StringBuilder getHintWord() {
       return hintWord;
   }
   public void setHintWord(StringBuilder hintWord) {
       this.hintWord = hintWord;
   }
   public int getNumOfTurn() {
       return numOfTurn;
   }
   public void setNumOfTurn(int numOfTurn) {
       this.numOfTurn = numOfTurn;
   }

}


//======================== Driver ===========================

import java.util.Scanner;


public class SecretWordDriver {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       SecretWord word = new SecretWord();
       word.setSecretWord("secretword");/// HERE YOU CAN SET ANY RANDOM WORD
       word.setNumOfTurn(5);
       System.out.println("Welcome to the Word guessing game! You hava "+word.getNumOfTurn()+" ties to guess the secret word!");
       //System.out.println(word.getHintWord());
       int turn=0;
       char ch;
       String str;
       System.out.println("Current hint is "+word.getHintWord());
       while(turn<word.getNumOfTurn()){
           turn++;
           System.out.println("Guess the lowercase latter");
           ch = sc.next().charAt(0);
           word.guessLetter(ch);
           System.out.println(" "+word.getHintWord());
           System.out.println("Guess secret word");
           str = sc.next();
           if(str.equalsIgnoreCase(word.getSecretWord())){
               System.out.println("You Win !");
               System.out.println("Game is Over");
               System.out.println("Would you like to try again(Y/N)");
               ch = sc.next().charAt(0);
               if(ch=='Y'||ch=='y'){
                   turn =0;
                   word.setSecretWord("secretword");/// HERE YOU CAN SET ANY RANDOM WORD
                   continue;
               }
               else
                   break;
           }
           if(!str.equalsIgnoreCase(word.getSecretWord()) && turn==word.getNumOfTurn()){
               System.out.println("You Loss !");
               System.out.println("Game is Over");
               System.out.println("Would you like to try again(Y/N)");
               ch = sc.next().charAt(0);
               if(ch=='Y'||ch=='y'){
                   turn =0;
                   word.setSecretWord("secretword");/// HERE YOU CAN SET ANY RANDOM WORD
                   continue;
               }
               else
                   break;
           }
           System.out.println("Keep Trying");
       }
   }

}

===================Output=========================

Welcome to the Word guessing game! You hava 5 ties to guess the secret word!
Current hint is
    **********
Guess the lowercase latter
s
   s*********
Guess secret word
secre
Keep Trying
Guess the lowercase latter
c
   s*c*******
Guess secret word
secretword
You Win !
Game is Over
Would you like to try again(Y/N)
n