/**
*
* @author junqi
*/
public class Hangman {
// instant variable or attributes of the class
int correct=0 ;
int wrong= 0 ;
int total= 0 ;
char [] newDisguise;
public void disguisedword()
{
char [] Disguseses = new char [secretword.length()];
// create a char array with the length of secretword
for(int i = 0;i<secretword.length();i++)
{
Disguseses[i] = '?';
// assigning '?' into all the array locations
}
System.
out.
print("The Disguised word is <<");
for(int i = 0;i<secretword.length();i++)
{
System.
out.
print(Disguseses
[i
]);
//printing out the disguise word all values in the array.
}
newDisguise = Disguseses; // assigning this local char array variable to instant char array variable
}
public void SecretWord
(String Secretword
)
{
this.secretword = Secretword; // assigning value to attribute secretword
}
public boolean makeGuess
(String guess
)
{
boolean complete = false; // set to false
int found = 0; // if its not completed
int cfound = 0; // correct guess
for(int i = 0;i<secretword.length();i++)
{
if(guess.charAt(0) == secretword.charAt(i))
{ // if user guess is equals to secretword replace attribute char array location with guess
newDisguise[i]=guess.charAt(0);
cfound = 1; //
}
}
if(cfound==1)
{ // if its correct total will increment by 1 and print out the result statement how many guess
// and how many wrong
total++;
System.
out.
printf("Correct, Guesses made %d with %d wrong\n",total,wrong
);
}
else
{ // if cfound = 0 means its a wrong guess
wrong++; // wrong guesss + 1
total++;// total guess +1
System.
out.
printf("Incorrect, Guesses made %d with %d wrong\n",total,wrong
);
}
System.
out.
print("The Disguised word is <<");
for(int i =0;i<secretword.length();i++)
{
System.
out.
print(newDisguise
[i
]); // printing the updated disguise word
}
for(int i = 0;i<secretword.length();i++)
{
if(newDisguise[i]=='?')
{ // checking if the disguise word is completed or not
found = 1; // if not completed
break;
}
else
{
found = 2; // if completed
}
}
if(found==2)
{
complete = true; // return true if completed
}
return complete; // return false and true
}
public boolean inputval
(String guess
)
{ // to check if the user enter more than 1 character
boolean value = false;
if(guess.length()==1)
{// checking the length of the user input
value = true;
}
return value;
}
public boolean checkInt
(String guess
)
{
// catching if the user enter a digit
try
{
Integer.
parseInt(guess
); // if user input can be converted into int
return true; // means user entered a number
}
{
return false; // not a digit.
}
}
}
/**
*
* @author junqi
*/
public class HangmanTest {
public static void main
(String [] args
)
{
Scanner read
= new Scanner
(System.
in);
Hangman round1 = new Hangman();
Hangman round2 = new Hangman();
int breaker=0; // used a loop condition
StudentInfo();
while(breaker!=-1) // whole game will loop uptil breaker is = -1
{
System.
out.
println("--------Lets play a round of Hangman---------");
System.
out.
println("Would you like to key in the secretword?(yes or no)");
choice = read.nextLine(); // give user a choice to enter keyword
if (choice.equalsIgnoreCase("yes"))
{ // if user enter yes
System.
out.
println("Please enter the secretWord");
secretword = read.nextLine().toLowerCase(); // read the user secret word
round1.SecretWord(secretword); // user secretword will be assigned to object 1 secret word
}
else
{
round1.SecretWord("secret"); // default keyword if the user enters no
}
round1.disguisedword(); // display the disguised word
System.
out.
println("Please enter your guess here");
ch = read.nextLine().toLowerCase(); // prompting for user guess
boolean real = round1.inputval(ch);//check how many character input
boolean checkint = round1.checkInt(ch); //check if it is a number
if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
{
while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
{ // if user enters more than 1 char or enters a digit
if(round1.inputval(ch)==false)
{ // if user enters more than 1 char prompt again
System.
out.
println("Please enter single Alphabet only");
ch = read.nextLine().toLowerCase();
}
else if(round1.checkInt(ch)==true)
{ //if user enters a digit prompt again
System.
out.
println("Please enter alphabet only");
ch = read.nextLine().toLowerCase();
}
} // will check until the one character and its a alphabet is entered
}
while (round1.makeGuess(ch)==false) // will loop until disguised word is completed and return a true
{ // system checking the user input against the secretword
System.
out.
println("Please enter your guess here");
ch = read.nextLine().toLowerCase(); // keep prompting until true is returned
real = round1.inputval(ch);// check is it more than one char
checkint = round1.checkInt(ch);//check is it an int
if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
{
while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
{
if(round1.inputval(ch)==false)
{
System.
out.
println("Please enter single Alphabet only");
ch = read.nextLine().toLowerCase();
}
else if(round1.checkInt(ch)==true)
{
System.
out.
println("Please enter an alphabet only");
ch = read.nextLine().toLowerCase();
}
}
}
}
// prompt the user if they want to to continue
System.
out.
println("Would you like to stop the program?(press -1 to stop)");
breaker = read.nextInt();//read their choice
read.nextLine();
if(breaker ==-1)
{
break;
}
// Its the same as the above program
System.
out.
println("------Lets play another round of Hangman--------");
System.
out.
println("Would you like to key in the secretword?(yes or no)");
choice = read.nextLine();
if (choice.equalsIgnoreCase("yes"))
{
System.
out.
println("Please enter the secretWord");
secretword = read.nextLine().toLowerCase();
round2.SecretWord(secretword);
}
else
{
round2.SecretWord("zouk");
}
round2.disguisedword();
ch = read.nextLine().toLowerCase();
real = round2.inputval(ch);
checkint = round1.inputval(ch);
if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
{
while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
{
if(round1.inputval(ch)==false)
{
System.
out.
println("Please enter single Alphabet only");
ch = read.nextLine().toLowerCase();
}
else if(round1.checkInt(ch)==true)
{
System.
out.
println("Please enter an alphabet only");
ch = read.nextLine().toLowerCase();
}
}
}
while (round2.makeGuess(ch)==false)
{
System.
out.
println("Please enter your guess here");
ch = read.nextLine().toLowerCase();
real = round2.inputval(ch);
checkint = round1.inputval(ch);
if (real==false || checkint==true)// checking if either the input has more than 1 char or number.
{
while(round1.inputval(ch)==false || round1.checkInt(ch)==true)
{
if(round1.inputval(ch)==false)
{
System.
out.
println("Please enter single Alphabet only");
ch = read.nextLine().toLowerCase();
}
else if(round1.checkInt(ch)==true)
{
System.
out.
println("Please enter an alphabet only");
ch = read.nextLine().toLowerCase();
}
}
}
}
System.
out.
println("Would you like to continue?(press-1 to stop)");
breaker = read.nextInt();
read.nextLine();
}
System.
out.
println("--------Thank you for playing----------");// if user enters -1
}
public static void StudentInfo()
{
System.
out.
println("My Name is: Tan Jun Qi");
System.
out.
println("Murdoch StudentID: 33872846");
System.
out.
println("Mode of enrollment: External");
System.
out.
println("Tutor name: Aaron Yeo");
System.
out.
println("Workshop: Friday 4.15pm ");
}
}