I can't seem to get the formatting right, so here is the pastebin if needed: http://pastebin.com/g6VmdYbM
public class Hangman {
///////////////////////////////////
// Global Variables
///////////////////////////////////
private static String[] correctPhrase = new String[5];
private static String[] currentGuessPhrase = new String[5];
private static char[] currentGuesses = new char[26];
private static int totalWrong = 0;
private static int totalGuesses = 0;
private static int count;
private static char guess;
///////////////////////////////////
// Methods
///////////////////////////////////
public static void makePhrase() {
//Clear guesses
for (int x = 0; x < currentGuesses.length; x++){
currentGuesses[x] = ' ';
}
totalWrong = 0;
totalGuesses = 0;
//Preset Phrases (Must be 5 words)
String[] phraseOne = { "this", "is", "a", "sample", "phrase" };
String[] phraseTwo = { "another", "phrase", "is", "right", "here" };
String[] phraseThree = { "finally", "this", "is", "the", "last" };
//Random words for selection
String[] wordBank = { "a", "ate", "apple", "banana", "bored", "bear",
"cat", "cow", "carried", "died", "during", "deer", "elephant",
"flame", "fire", "fruit", "forgave", "forged", "fears", "goat",
"good", "game", "gave", "greeted", "glory", "ham", "hairy",
"heaven", "horrible", "I", "illegal", "important", "jammed",
"juice", "kangaroo", "liar", "loved", "money", "miracles",
"monday", "named", "never", "noun", "now", "nor", "orange",
"obligated", "person", "people", "peeled", "quit", "retired",
"rain", "saved", "sunny", "soaring", "salmon", "sealed",
"today", "tomorrow", "trained", "the", "umbrella", "up",
"under", "violent", "violin", "when", "while", "year", "zoo" };
//Get phrase type
Scanner in = new Scanner(System.in);
System.out.println("\n(1) Random Words (2) Presets (3) Custom");
int phraseType = in.nextInt();
if (phraseType == 1){
for (int x = 0; x < 5; x++) {
correctPhrase[x] = wordBank[(int) Math.round(Math.random() * 61)];
}
} else if (phraseType == 2) {
int phrase = (int) Math.round(Math.random() * 2);
switch (phrase){
case 0: correctPhrase = phraseOne.clone();
case 1: correctPhrase = phraseTwo.clone();
case 2: correctPhrase = phraseThree.clone();
}
} else if (phraseType == 3){
Scanner in2 = new Scanner(System.in);
System.out.println("5 Word Phrase: ");
correctPhrase = in2.nextLine().split("\\s");
}
//Create duplicate with underscores
for (int x = 0; x < correctPhrase.length; x++) {
currentGuessPhrase[x] = correctPhrase[x].replaceAll(".", "_");
}
}
public static char getGuess() {
Scanner in = new Scanner(System.in);
// Retrieve next guess
System.out.println("\nGuess:");
char guessInput = in.next().charAt(0);
return Character.toLowerCase(guessInput);
}
public static boolean checkGuess(char guess) {
// Add to guessed chars
currentGuesses[totalGuesses] = guess;
totalGuesses++;
// Count number of occurrences
count = 0;
for (int x = 0; x < correctPhrase.length; x++) {
for (int a = 0; a < correctPhrase[x].length(); a++) {
if (correctPhrase[x].charAt(a) == guess) {
count++;
}
}
}
if (count == 0) {
return false;
} else {
return true;
}
}
public static void updateGuess(char guess) {
// Define char array from currentGuess for alerting
char[][] currentGuessArray = new char[currentGuessPhrase.length][];
for (int x = 0; x < currentGuessPhrase.length; x++) {
currentGuessArray[x] = currentGuessPhrase[x].toCharArray();
}
//Assign valid values of guess to currentGuessArray
for (int x = 0; x < correctPhrase.length; x++) {
for (int a = 0; a < correctPhrase[x].length(); a++) {
if (correctPhrase[x].charAt(a) == guess) {
currentGuessArray[x][a] = guess;
}
}
}
// Convert chars back to string array
for (int x = 0; x < currentGuessArray.length; x++) {
currentGuessPhrase[x] = new String(currentGuessArray[x]);
}
}
public static void drawBoard(){
// Print previous guesses
System.out.println("\nGuesses:\n");
for (int x = 0; x < currentGuesses.length; x++) {
System.out.print(currentGuesses[x] + " ");
}
// Draw hangman
System.out.print(" \n ");
if (totalWrong == 0){
System.out.print("\n______" +
"\n| |" +
"\n| " +
"\n| " +
"\n| " +
"\n| " +
"\n| ");
} else if (totalWrong == 1){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| " +
"\n| " +
"\n| " +
"\n| ");
} else if (totalWrong == 2){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| |" +
"\n| " +
"\n| " +
"\n| ");
} else if (totalWrong == 3){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| |" +
"\n| / " +
"\n| " +
"\n| ");
} else if (totalWrong == 4){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| |" +
"\n| / \\" +
"\n| " +
"\n| ");
} else if (totalWrong == 5){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| |-" +
"\n| / \\" +
"\n| " +
"\n| ");
} else if (totalWrong == 6){
System.out.print("\n______" +
"\n| |" +
"\n| O" +
"\n| -|-" +
"\n| / \\" +
"\n| " +
"\n| " +
"\n\n YOU DIED!");
//Print correct phrase
System.out.println("\n");
for (int x = 0; x < correctPhrase.length; x++){
System.out.print(correctPhrase[x] + " ");
}
}
//Print guessPhrase
System.out.println("\n");
for (int x = 0; x < currentGuessPhrase.length; x++){
System.out.print(currentGuessPhrase[x] + " ");
}
}
public static boolean goAgain(){
//Retreive yes/no
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Play again?", "Hangman", dialogButton);
if (dialogResult == 0 ){
return true;
} else {
return false;
}
}
///////////////////////////////////
// Main Method
///////////////////////////////////
public static void main(String[] args) {
boolean goAgain = true;
boolean isCorrect;
makePhrase();
while (goAgain){
//Print correct for debugging
/*for (int x = 0; x < correctPhrase.length; x++){
System.out.print(correctPhrase[x] + " ");
}
*/
drawBoard();
guess = getGuess();
isCorrect = checkGuess(guess);
//Update board
if (isCorrect) {
updateGuess(guess);
} else {
totalWrong++;
}
//Determine loss
if (totalWrong == 6){
drawBoard();
goAgain = goAgain();
if (goAgain){
makePhrase();
} else {
break;
}
}
//Determine win
if (Arrays.equals(correctPhrase, currentGuessPhrase)){
System.out.println("\nYOU WIN!" +
"\n O" +
"\n -|-" +
"\n / \\");
for (int x = 0; x < correctPhrase.length; x++){
System.out.print(correctPhrase[x] + " ");
}
goAgain = goAgain();
if (goAgain){
makePhrase();
}
}
}
}
}