I have recently begun trying to learn Java and decided to make a basic Hangman game. The game uses a text file to retrieve a random word for the game. I am looking for some general feedback on how I can improve the structure and efficiency of my code.
public class Hangman {
private static int lives = 8; // live count
private static char guess; // stores user input
private static String guesses = ""; //string for each letter that has been guessed
private static boolean Solved = false;
private static void check_input(char i, ArrayList<String> word1){ //check if guess is correct and modify variables accordingly
int count = 0; // keeps track if the guess was found in the word
for(int a = 0; a < theword.length(); a++){ // loops through each letter in string
if(theword.charAt(a) == Character.toLowerCase(i)){
System.out.printf(Character.toUpperCase(i) + " was Correct!\n");
word1.set(a, Character.toString(i)); // replaces *'s
count +=1; // updates count
}
}
if (count == 0){
lives = lives-1;
}
/*
int pos = theword.indexOf(Character.toLowerCase(i));
if (pos == -1) {
lives = lives-1;
// letter not found
} else {
System.out.printf(Character.toUpperCase(i) + " was Correct!\n");
word1.set(pos, Character.toString(i));
// letter exists at zero-based position pos
// CAUTION: it might also exist in later positions!
}*/
}
private static void build_hangman(int l) {
switch (l) { //switch case for each live case
case (7):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
//System.out.println(" ______________");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ___________");
break;
case (6):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ___________");
break;
case (5):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" || ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ___________");
break;
case (4):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" | | |");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" ___________");
break;
case (3):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" | | |");
System.out.println(" | | 0");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" ___________");
break;
case (2):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" | | |");
System.out.println(" | | 0");
System.out.println(" | | /|\\");
System.out.println(" | | ");
System.out.println(" | | ");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" ___________");
break;
case (1):
System.out.printf("Incorrect! Try Again! You have %d Lives\n", l);
System.out.println(" ______________");
System.out.println(" | | |");
System.out.println(" | | 0");
System.out.println(" | | /|\\");
System.out.println(" | | |");
System.out.println(" | | ");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" ___________");
break;
case (0):
Solved = true;
System.out.println(" ______________");
System.out.println(" | | |");
System.out.println(" | | 0");
System.out.println(" | | /|\\");
System.out.println(" | | |");
System.out.println(" | | / \\");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" ___________");
System.out.println("Game Over :( The Answer was " + theword);
System.out.print("Would you like to play again? (y/n) :");
break;
default:
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ||");
System.out.println(" ___________");
break;
}
}
public static String getword(String f){
File file = new File(f);
Random rand = new Random();
try {
Scanner scanwords = new Scanner(file).useDelimiter("\n");
List<String> words = new ArrayList<String>();
String sb;
while(scanwords.hasNext()) {
sb = scanwords.next();
words.add(sb);
}
String[] wordArray = words.toArray(new String[0]);
String theword = wordArray[rand.nextInt(wordArray.length)];// pick random word from those stored in array
//
System.out.println(theword); //debug print code comment out to not see answer
return theword; // return it
}
catch (FileNotFoundException e){
System.out.println("Looks Like your File Got an Error"); // catch invalid file exception
System.exit(0);
return "error";
}
}
public static String theword;
private static final String filename = PUT FILE NAME HERE IN QUOTES;//be sure put exact location
public static void main(String args[]) {
ArrayList<String> placeholder = new ArrayList<String>();
Scanner input = new Scanner(System.in);
char[] correct = {};
char guess;// guess char
while (true) { //game loop
System.out.println("Lets Play Hangman! You will be allowed " + lives + " incorrect guesses. Good Luck!");
theword = getword(filename);// call get word function
for (int i = 0; i < theword.length() - 1; i++) {
placeholder.add(Character.toString('*'));
}
while (Solved == false) {
if (placeholder.contains(Character.toString('*'))) {
Solved = false;
} else {
//System.out.println(placeholder);
System.out.println("You Win! You correctly guessed " + theword);
Solved = true;
System.exit(0);//debug
}
System.out.printf("Live Count: %d \t", lives); //display lives
System.out.print("The Word: "); // display word placeholders
System.out.print(placeholder + " ");
if (guesses != null && !guesses.isEmpty()) { // check if player has guessed
System.out.println("Guesses " + guesses); // display guesses
} else {
System.out.println("Guesses: None");
}
System.out.print("Guess a Letter: ");
guess = input.next().charAt(0);//get first char of input
guesses += guess; //add gues to string of guesses
check_input(guess, placeholder); // call check_input method
build_hangman(lives); // print hangman based on no. of lives
}
System.exit(0);// exit if solved
}
}
//public boolean check_guess(){
// if (
}