Lab 16 Secret Word Game! objective: Write a class that will be used by a program
ID: 3861293 • 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
import java.util.*;
public class SecretWord {
static String secretword;
static String hintword="";
static int numberofturns;
static char arr[];
static Scanner sc=new Scanner(System.in);
SecretWord(String secretword){
this.secretword=secretword;
this.numberofturns=0;
for(int i=0;i<secretword.length();i++){
this.hintword+='*';
}
arr=new char[secretword.length()];
//System.out.println(hintword);
}
public static String getsecretword(){
return secretword;
}
public static String gethintword(){
return hintword;
}
public static int getnumberofturns(){
return numberofturns;
}
public static void guessletter(char val){
int ind=0;
int valint,valint2;
for(int i=0;i<secretword.length();i++){
if(secretword.charAt(i)==val){
ind=i;
arr[i]=secretword.charAt(i);
}
}
System.out.println();
for(int j=0;j<arr.length;j++){
valint=arr[j];
if(valint>=97 && valint<=122)
System.out.print(arr[j]);
else
System.out.print("*");
}
System.out.println();
System.out.println("Guess the secret word:");
System.out.println();
for(int k=0;k<arr.length;k++){
valint2=arr[k];
if(valint2>=97 && valint2<=122)
System.out.print(arr[k]);
else{
char sim=getrand();
System.out.print(sim);
}
}
System.out.println();
numberofturns++;
}
public static char getrand(){
char ch='s';
Random r=new Random();
int i=r.nextInt(26);
switch(i){
case 1:ch='a';
break;
case 2:ch='b';
break;
case 3:ch='c';
break;
case 4:ch='d';
break;
case 5:ch='e';
break;
case 6:ch='f';
break;
case 7:ch='g';
break;
case 8:ch='h';
break;
case 9:ch='i';
break;
case 10:ch='j';
break;
case 11:ch='k';
break;
case 12:ch='l';
break;
case 13:ch='m';
break;
case 14:ch='n';
break;
case 15:ch='o';
break;
case 16:ch='p';
break;
case 17:ch='q';
break;
case 18:ch='r';
break;
case 19:ch='s';
break;
case 20:ch='t';
break;
case 21:ch='u';
break;
case 22:ch='v';
break;
case 23:ch='w';
break;
case 24:ch='x';
break;
case 25:ch='y';
break;
case 26:ch='z';
break;
}
return ch;
}
public static void main(String[] args) {
SecretWord s=new SecretWord("beefy");
boolean ret=false;
System.out.println("current Hint is:");
String word=getsecretword();
for(int i=0;i<word.length();i++)
System.out.print("*");
System.out.println();
int turns=0;
do{
System.out.println("Guess a lower Case letter:");
char letter=sc.next().charAt(0);
guessletter(letter);
turns=getnumberofturns();
ret= checkwin();
if(ret==true){
System.out.println("ur guess is correct:");
return;
}
}while(turns<secretword.length());
System.out.println("you exeeded the chances limit:");
}
public static boolean checkwin(){
int count=0;
for(int i=0;i<secretword.length();i++){
char cd=secretword.charAt(i);
if(arr[i]==cd)
count++;
}
if(count==secretword.length())
return true;
else
return false;
}
}
output:
current Hint is:
*****
Guess a lower Case letter:
e
*ee**
Guess the secret word:
heewp
Guess a lower Case letter:
b
bee**
Guess the secret word:
beeme
Guess a lower Case letter:
f
beef*
Guess the secret word:
beefc
Guess a lower Case letter:
y
beefy
Guess the secret word:
beefy
ur guess is correct:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.