Results 1 to 2 of 2
  1. #1
    DAFLU1D is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default newbie needing help with java hangman game

    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class HangMan {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner (System.in);
    int YesNo;
    char Guess;
    int indexes;
    String Word, guessString;
    String blanks;
    int Lives;
    Boolean Chartrue;
    Boolean game;
    String Guessed;

    YesNo = JOptionPane.showConfirmDialog(null, "Would you like to play hangman? ", "Welcome", JOptionPane.YES_NO_OPTION);
    if (YesNo == 0){ //if they want to play hangman
    //do{
    blanks = "";
    Guessed = "";
    Lives = 6;
    Chartrue = false;
    game = true;


    Word = JOptionPane.showInputDialog ("P2 Enter a Phrase: "); // phrase to be guessed
    for(int i=0; i<Word.length();i++){
    blanks = blanks + "_ "; //inserts underscores
    }

    do{


    for(int i = 0; i < Word.length();i++){

    if (' ' == Word.charAt(i)){
    if (i==0){
    blanks = ' ' + blanks.substring(1,blanks.length());

    }
    else{
    blanks = blanks.substring(0, (i*2)) + ' ' + blanks.substring((i*2)+1, blanks.length());
    }
    }//if true
    }//end for
    JOptionPane.showMessageDialog(null,blanks); //inserts underscores
    //guessString = "";
    guessString = JOptionPane.showInputDialog("Player 1 input a letter:", JOptionPane.PLAIN_MESSAGE); //guessing letter

    Guess = guessString.charAt(0); //stores current guess

    while (Guessed.indexOf(Guess) != -1){ //indexes guessed letters
    Guess = JOptionPane.showInputDialog("No, not that letter. \nPlayer 1 input a letter:").charAt(0); //error message if you enter an incorrect or guessed letter
    }



    Guessed = (Guessed + Guess); //stores guessed letters
    Chartrue = false;
    for(int i = 0; i < Word.length();i++){

    if (Guess == Word.charAt(i)){
    Chartrue = true;
    if (i==0){
    blanks = Guess + blanks.substring(1,blanks.length());

    }
    else{
    blanks = blanks.substring(0, (i*2)) + Guess + blanks.substring((i*2)+1, blanks.length());
    }
    }//if true
    }//end for


    if (Chartrue==true){ //if letter is right
    JOptionPane.showMessageDialog(null, "Yay! you guessed a correct letter!");
    JOptionPane.showMessageDialog(null, blanks);
    }

    else { //if letter is wrong
    JOptionPane.showMessageDialog(null, "Oh...You've guessed an incorrect letter");
    Lives = Lives-1;
    }

    if (Lives == 6 ) { //if you have 6 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | \n | \n | \n __________ \r ", blanks, JOptionPane.PLAIN_MESSAGE);
    }

    if (Lives == 5 ) { //when you have 5 lives left
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | \n | \n __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);

    }
    if (Lives == 4 ) { //if you have 4 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | |\n | | \n __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);
    }

    if (Lives == 3 ) { //if you have 3 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | ---|\n | | \n __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);
    }

    if (Lives == 2 ) { //if you have 2 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | ---|---\n | | \n __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);
    }


    if (Lives == 1 ) { //if you have 1 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | ---|---\n | |\n| ^\n | |\n" +
    " __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);
    }

    if (Lives == 0 ) { //if you have 0 lives
    JOptionPane.showMessageDialog(null, "__________\n | |\n | (_) \n" +
    " | ---|---\n | |\n| ^\n | ||\n" +
    " __________ \r", blanks, JOptionPane.PLAIN_MESSAGE);

    JOptionPane.showMessageDialog(null, "Sorry you are out of chances your word was:"+ Word);
    }


    boolean win = true;


    do{

    for(int i = 0; i < Word.length();i++){
    if ((Guessed.indexOf((Word.charAt(i))) == -1)){
    win = false;

    }

    }

    if (win==true){
    int playAgain = JOptionPane.showConfirmDialog(null,"You guessed the correct word!\nWant to play again?", "Do you?", JOptionPane.YES_NO_OPTION);
    if (playAgain == 0){
    win = false;
    }

    }

    }while(win == false);

    game = false;
    }while (game == true);

    }


    JOptionPane.showMessageDialog (null, "Okay thanks for looking at my project have a nice day!", "Goodbye", JOptionPane.ERROR_MESSAGE);

    }

    }

    no errors or anything but when it runs the program ends after the first guess any help would be great thanks!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: newbie needing help with java hangman game

    It so hard to read your code. Would you mind formatting your code using the [code][/code] tag.

Similar Threads

  1. A newbie to Java needing help on coding error
    By Turo in forum New To Java
    Replies: 5
    Last Post: 08-28-2011, 05:11 AM
  2. Hangman Game
    By getshum in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 04:01 AM
  3. Hangman Game // HELP //
    By K-Scale in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 12:01 AM
  4. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 06:47 PM
  5. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •