Hangman Program Example Public Class Hangmanproject
Hangman Program Example Public Class Hangmanproject
// CONSTANTS
// VARIABLES
int totalMistake = 0;
boolean b = false;
// PROGRAM CODE
showWelcome();
SecretWord word = new SecretWord();
System.out.println("Your clue: " + word.getClue());
KeyboardInput input = new KeyboardInput();
while (totalMistake < 4 && !word.isSolved())
{
System.out.println(word);
char c = input.getNewLetter();
if (!word.update(c))
totalMistake = totalMistake + 1 ;
}
if (word.isSolved())
System.out.println("Great, you done it.");
else
System.out.println("Sorry, too many errors.");
}
public static void showWelcome()
{
System.out.println( "Welcome to Hangman Game:)!!" );
}
}
{
return secret.equalsIgnoreCase(visible);
}
}
public class KeyboardInput
import java.util.ArrayList;
import java.util.Scanner;
public class KeyboardInput
{
// Properties
ArrayList<String> keyList ;
boolean chosen;
//Constructor
public KeyboardInput()
{
keyList = new ArrayList<String>();
}
// Adds and stores the entered key in the Arraylist
public void keyChosen( String key )
{
keyList.add( key );
}
// Checks if the entered key has been chosed before
public boolean hasBeenChosen( String key)
{
chosen = keyList.contains( key );
return chosen;
}
public char getNewLetter()
{
Scanner scan=new Scanner( System.in);
char a;
boolean chosen;
String aTmp; // used to convert a into a String for compatibility with Group 6
do
{
System.out.println("Enter a Letter");
a = scan.next().charAt( 0);
aTmp = "" + a;
chosen = hasBeenChosen( aTmp);
if(!chosen)
keyChosen( aTmp);
else
{
System.out.println("You've already used that one. Please enter a new Letter");
}
}
// we repeat the process until we're out of trials.
while(chosen);
return a;
}
}