This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Game Development and the fly likes need a little help with hangman game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "need a little help with hangman game" Watch "need a little help with hangman game" New topic
Author

need a little help with hangman game

Saran Sundararajan
Greenhorn

Joined: Nov 17, 2012
Posts: 2
import java.util.*;
import java.io.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.JPasswordField;

/**
* Write a description of class HangMan4 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HangManSchool {

private static int nRetry = 6;
private static int index;

public static void main(String[] args) {
String pw;
String pws;

JPasswordField passField = new JPasswordField();
int Word = JOptionPane.showConfirmDialog(null, passField,
"Enter Your Word:", JOptionPane.OK_CANCEL_OPTION);

pw = new String(passField.getPassword());

JPasswordField passField2 = new JPasswordField();
int ReWord = JOptionPane.showConfirmDialog(null, passField2,
"Re-Enter Your Word:", JOptionPane.OK_CANCEL_OPTION);
pws = new String(passField2.getPassword());

if (ReWord == JOptionPane.OK_OPTION
&& pw.equals(new String(passField2.getPassword()))) {
String out = "Congratulations, Let The Games Begin!";
JOptionPane.showMessageDialog(null, out);
}

else if (pw != pws) {
String out = "The Word Was Re-Entered Wrong, Please Try Again.";
JOptionPane.showMessageDialog(null, out);
System.exit(0);
}
if (pw == pws && Word == ReWord)
System.out.println("Congratulations, Let The Games Begin!");

if (ReWord == JOptionPane.CANCEL_OPTION
|| ReWord == JOptionPane.CLOSED_OPTION) {
String out = "Thanks For Using HangMan V.05!";
JOptionPane.showMessageDialog(null, out);
}

Scanner kb = new Scanner(System.in);
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.print(" ");

int length = pw.length();
char[] letters = new char[length];
String word = pw.toLowerCase();
int o = 0;

while (length > 0) {
System.out.print("_ ");

length--;
}
System.out.println("\nThe Game Has Now Begun");

while (nRetry != 0) {
System.out.println("\nPlease enter a letter (A-Z)");
String guesses = kb.nextLine();
String guessLetter = guesses.toLowerCase();
if (guessLetter.length() > 1) {
String out = "ERROR MESSAGE!: Please Enter Only One Letter For Each Guess!";
JOptionPane.showMessageDialog(null, out);
}
System.out.println("The letter that you have guessed is: " + guessLetter);

if (word.contains(guessLetter)) {
System.out.println("Good job, you guessed the letter in the word!");

index = pw.indexOf(guessLetter);
letters[index] = guessLetter.charAt(o);

} else {
nRetry = nRetry - 1;
System.out.println("Sorry, try again.");
}

printHangMan(word);
printMatchedWord(word, letters, guessLetter, index, pw);
isMatching(word, letters);
Repeat(guessLetter);

}

}

private static void Repeat(String guessLetter){
{
if(guessLetter == guessLetter)
System.out.println("You Already Guessed This Letter");
}
}

/**
* Prints the hangman depending on retries left
*/
private static void printHangMan(String word) {
if (nRetry == 6) {
System.out.println("---------");
System.out.println("| |");
System.out.println("| |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" ------------");
} else if (nRetry == 5) {
System.out.println("Now You Just Have" + " " + nRetry + " "
+ "Lives Left.");
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" --------------");
} else if (nRetry == 4) {
System.out.println("Now You Just Have" + " " + nRetry + " "
+ "Lives Left.");
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" --------------");
} else if (nRetry == 3) {
System.out.println("Now You Just Have" + " " + nRetry + " "
+ "Lives Left.");
System.out.println(" ---------");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" | |");
System.out.println(" < |");
System.out.println(" |");
System.out.println(" --------------");
} else if (nRetry == 2) {
System.out.println("Now You Just Have" + " " + nRetry + " "
+ "Lives Left.");
System.out.println(" ----------");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" 0 |");
System.out.println(" | |");
System.out.println(" > < |");
System.out.println(" |");
System.out.println(" --------------");
} else if (nRetry == 1) {
System.out.println("Now You Just Have" + " " + nRetry + " "
+ "Lives Left.");
System.out.println("Now You Just Have" + " " + 1 + " "
+ "Lives Left.");
System.out.println(" ----------");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" 0 * |");
System.out.println(" | |");
System.out.println(" > < |");
System.out.println(" |");
System.out.println(" --------------");
} else {
System.out.println("Game Over!");
System.out.println(" ---------");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" * 0 * |");
System.out.println(" | |");
System.out.println(" > < |");
System.out.println(" |");
System.out.println(" --------------");
String out = "Sorry, The Hidden Word Was" + " " + word + "!";
JOptionPane.showMessageDialog(null, out);

System.exit(0);
}
}

/**
* Prints the matched letter to the screen
*/
private static void printMatchedWord(String word, char[] letters, String guessLetter, int index, String pw) {

for (int i = 0; i < letters.length; i++) {
System.out.print("-");
}



}

/**
* @param word - The word the player needs to guess
* @param letters - Character array of matched letters
*/
private static void isMatching(String word, char[] letters) {
String enteredWord = new String(letters);
if (enteredWord.equals(word)) {
String out = "Congratulations, You Won!";
JOptionPane.showMessageDialog(null, out);
System.exit(0);

}
}
}

all i need is a loop that makes a word with two or more of the same letter word. like if you type in pokemon it wont recognize the second o and will loop on forever until you die.
and how do you make it look like real code on the website with the indents ad stuff. thanks guys.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> how do you make it look like real code on the website with the indents ad stuff

click the 'edit' button beside your post
highlight/select all of your posted code
click the 'code' button (above the typing area)
click preview to make sure its OK
click 'submit' (?)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need a little help with hangman game