Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Hangman minor issues

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman minor issues

    I need to get it to say what letters have already been guessed. And I need it to cut the game off when the user wins. Oh and one last little thing. For some reason, it is printing the correct letters on the hangman dashes at the top of the hangman.


    import java.io.*; //for file
    import java.util.*; //for scanner

    public class Lab65 {
    static String userG = "";
    public static void main(String[] args)throws FileNotFoundException{

    int wrong = 0; // keeps track of user guesses
    String word = readwords();
    boolean temp = false;

    String[] words = new String[word.length()];
    String[] words2 = new String[word.length()];

    //methods to be used
    userGuess(word, words, words2);
    printhm(wrong);

    //introduce user to game
    System.out.println("Welcome to hangman!");
    System.out.println("Begin guessing letters!");
    System.out.println("These letters have already been guessed!");
    userGuess(words);

    //keep track of wrong user guesses

    while(wrong <6) {
    temp = add(words, words2);
    userGuess(words);

    if(temp == false) {
    wrong++;
    printhm(wrong);
    System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
    System.out.println();
    System.out.println(userG);

    System.out.println();
    System.out.println(" These letters have already been guessed!");
    System.out.println("Make another guess!");

    }else{ //user guess is correct
    printhm(wrong);
    System.out.println("Correct guess!");
    System.out.println(" These letters have already been guessed!");

    System.out.println(userG);
    System.out.println("Make another guess!");

    }
    }
    }
    //display hangman
    public static void printhm(int wrong) {

    //hangman display
    if(wrong == 0) {
    System.out.println(" ------");
    System.out.println(" | |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 1){
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 2) {
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println(" | |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 3) {
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/| |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 4){
    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 5){
    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println("/ |");
    System.out.println(" |");
    }
    else {

    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println("/ \\ |");
    System.out.println(" |");
    System.out.println( "You lost! Better luck next time!!" );
    }

    }
    // keep track of wrong guesses
    public static String readwords() throws FileNotFoundException {
    Scanner input = new Scanner(new File("hangman.txt"));
    String[] word = new String[10];
    String rword = "";

    for(int i = 0; i < word.length; i++){
    if (input.hasNext()) {

    word[i] = input.next();

    }else {
    break;

    }
    }

    Random rand = new Random();

    int r = rand.nextInt(9);

    rword = word[r];
    return rword;
    }

    public static void userGuess(String y, String[] m, String[] words2){
    for (int i=0; i < y.length(); i++){
    m[i] = "-";
    }
    for (int x = 0; x < y.length(); x++){
    if (x + 1 < y.length())
    words2[x] = y.substring(x, x + 1);
    else
    words2[x] = y.substring(x, y.length());
    }
    }

    public static boolean add(String[] m, String[] ma) { //find if letters are in the word
    Scanner console = new Scanner(System.in);
    String letters = console.next();
    boolean found = false;

    for(int i = 0; i < ma.length; i++) {
    if(ma [i].equals(letters)){
    m[i] = letters;
    found = true;
    }
    }
    if(found) {
    return true;
    }
    else {
    return false;
    }
    }

    public static void userGuess(String[] m) {
    for(int i = 0; i < m.length; i++) {
    System.out.print(m[i] );
    }

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hangman minor issues

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Can you post the program's output that shows what it does when executed and add some comments explaining what is wrong?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman minor issues


    import java.io.*; //for file
    import java.util.*; //for scanner

    public class Lab65 {
    static String userG = "";
    public static void main(String[] args)throws FileNotFoundException{

    int wrong = 0; // keeps track of user guesses
    String word = readwords();
    boolean temp = false;

    String[] words = new String[word.length()];
    String[] words2 = new String[word.length()];

    //methods to be used
    userGuess(word, words, words2);
    printhm(wrong);

    //introduce user to game
    System.out.println("Welcome to hangman!");
    System.out.println("Begin guessing letters!");
    System.out.println("These letters have already been guessed!");
    userGuess(words);

    //keep track of wrong user guesses

    while(wrong <6) {
    temp = add(words, words2);
    userGuess(words);

    if(temp == false) {
    wrong++;
    printhm(wrong);
    System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
    System.out.println();
    System.out.println(userG);

    System.out.println();
    System.out.println(" These letters have already been guessed!");
    System.out.println("Make another guess!");

    }else{ //user guess is correct
    printhm(wrong);
    System.out.println("Correct guess!");
    System.out.println(" These letters have already been guessed!");

    System.out.println(userG);
    System.out.println("Make another guess!");

    }
    }
    }
    //display hangman
    public static void printhm(int wrong) {

    //hangman display
    if(wrong == 0) {
    System.out.println(" ------");
    System.out.println(" | |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 1){
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println(" |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 2) {
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println(" | |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 3) {
    System.out.println(" --------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/| |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 4){
    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println(" |");
    System.out.println(" |");
    }
    else if (wrong == 5){
    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println("/ |");
    System.out.println(" |");
    }
    else {

    System.out.println(" ---------");
    System.out.println(" | |");
    System.out.println(" 0 |");
    System.out.println("/|\\ |");
    System.out.println("/ \\ |");
    System.out.println(" |");
    System.out.println( "You lost! Better luck next time!!" );
    }

    }
    // keep track of wrong guesses
    public static String readwords() throws FileNotFoundException {
    Scanner input = new Scanner(new File("hangman.txt"));
    String[] word = new String[10];
    String rword = "";

    for(int i = 0; i < word.length; i++){
    if (input.hasNext()) {

    word[i] = input.next();

    }else {
    break;

    }
    }

    Random rand = new Random();

    int r = rand.nextInt(9);

    rword = word[r];
    return rword;
    }

    public static void userGuess(String y, String[] m, String[] words2){
    for (int i=0; i < y.length(); i++){
    m[i] = "-";
    }
    for (int x = 0; x < y.length(); x++){
    if (x + 1 < y.length())
    words2[x] = y.substring(x, x + 1);
    else
    words2[x] = y.substring(x, y.length());
    }
    }

    public static boolean add(String[] m, String[] ma) { //find if letters are in the word
    Scanner console = new Scanner(System.in);
    String letters = console.next();
    boolean found = false;

    for(int i = 0; i < ma.length; i++) {
    if(ma [i].equals(letters)){
    m[i] = letters;
    found = true;
    }
    }
    if(found) {
    return true;
    }
    else {
    return false;
    }
    }

    public static void userGuess(String[] m) {
    for(int i = 0; i < m.length; i++) {
    System.out.print(m[i] );
    }

    }

    }
    Sorry this is my first post. I hope I did it right this time.


    -a--a---- --------
    | |
    0 |
    |
    |
    |
    Correct guess!
    These letters have already been guessed!




    That is some of the output

    --- Update ---

    import java.io.*; //for file
    import java.util.*; //for scanner
     
    public class Lab65 {
        static String userG = "";
        public static void main(String[] args)throws FileNotFoundException{
     
            int wrong = 0; // keeps track of user guesses
            String word = readwords();
            boolean temp = false;
     
            String[] words = new String[word.length()];
            String[] words2 = new String[word.length()];
     
            //methods to be used
            userGuess(word, words, words2);
            printhm(wrong);
     
            //introduce user to game
            System.out.println("Welcome to hangman!");
            System.out.println("Begin guessing letters!");
            System.out.println("These letters have already been guessed!");
            userGuess(words);
     
            //keep track of wrong user guesses  
     
            while(wrong <6) {   
                temp = add(words, words2);
                userGuess(words);
     
                if(temp == false) { 
                    wrong++;
                    printhm(wrong);
                    System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
                    System.out.println();
                    System.out.println(userG);
     
                    System.out.println();
                    System.out.println(" These letters have already been guessed!");
                    System.out.println("Make another guess!");
     
                }else{    //user guess is correct
                    printhm(wrong);
                    System.out.println("Correct guess!");
                    System.out.println(" These letters have already been guessed!");
     
                    System.out.println(userG);
                    System.out.println("Make another guess!");
     
                }
            }
        }
        //display hangman
        public static void printhm(int wrong) {
     
            //hangman display 
            if(wrong == 0) {
                System.out.println("  ------");
                System.out.println(" |      |");
                System.out.println("        |");
                System.out.println("        |");
                System.out.println("        |");
                System.out.println("        |");
            }
            else if (wrong == 1){
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println("          |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 2) {
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println(" |        |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 3) {
                System.out.println("  --------");
                System.out.println(" |        |");
                System.out.println(" 0        |");
                System.out.println("/|        |");
                System.out.println("          |");
                System.out.println("          |");
            }
            else if (wrong == 4){
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("           |");
                System.out.println("           |");
            }
            else if (wrong == 5){
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("/          |");
                System.out.println("           |");
            }
            else {
     
                System.out.println("  ---------");
                System.out.println(" |         |");
                System.out.println(" 0         |");
                System.out.println("/|\\       |");
                System.out.println("/ \\       |");
                System.out.println("           |");
                System.out.println( "You lost! Better luck next time!!" );
            }
     
        }
        // keep track of wrong guesses
        public static String readwords() throws FileNotFoundException {  
            Scanner input = new Scanner(new File("hangman.txt"));   
            String[] word = new String[10];  
            String rword = "";  
     
            for(int i = 0; i < word.length; i++){  
                if (input.hasNext())  {
     
                    word[i] = input.next();    
     
                }else {
                    break;  
     
                }
            }        
     
            Random rand = new Random();  
     
            int r = rand.nextInt(9);  
     
            rword = word[r];  
            return rword;  
        } 
     
        public static void userGuess(String y, String[] m, String[] words2){
            for (int i=0; i < y.length(); i++){
                m[i] = "-";
            }
            for (int x = 0; x < y.length(); x++){
                if (x + 1 < y.length())
                    words2[x] = y.substring(x, x + 1);
                else
                    words2[x] = y.substring(x, y.length());
            }
        }
     
        public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
            Scanner console = new Scanner(System.in);
            String letters = console.next();
            boolean found = false;
     
            for(int i = 0; i < ma.length; i++) {
                if(ma [i].equals(letters)){
                    m[i] = letters;
                    found = true;
                }
            }
            if(found) {
                return true;
            }
            else {
                return false;
            }
        }
     
        public static void userGuess(String[] m) {
            for(int i = 0; i < m.length; i++) { 
                System.out.print(m[i] );
            }
     
        }
     
    }

    Sorry, I just figured it out.



    Welcome to hangman!
    Begin guessing letters!
    These letters have already been guessed!
    --------- [DrJava Input Box]
    --------- --------
    | |
    0 |
    |
    |
    |
    Incorrect guess!5 more guesses left!



    These letters have already been guessed!
    Make another guess!
    [DrJava Input Box]
    -a--a---- --------
    | |
    0 |
    |
    |
    |
    Correct guess!
    These letters have already been guessed!

    Make another guess!








    Some output

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hangman minor issues

    What was wrong with the way it executed? Where is the problem?
    Can you add some comments saying what is wrong with the output and post what the output should look like.

    Suggestion for testing: Have readWords() return the same String:
            return "testing"; //rword;           //<<<<<<<<<<<<<<<
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman minor issues

    The output should look like --------
    |
    |
    |
    |
    ------ <-----these should keep track of the letters already guessed.
    Also, it needs to stop the program when the user wins the game.
    The problem with the current output is that it puts the guessed letters on the hang part of the hangman

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hangman minor issues

    The problem with the current output is that it puts the guessed letters on the hang part of the hangman
    Please post the program's current output when executed with comments saying what is wrong with it.

    needs to stop the program when the user wins the game.
    Write a method that tests if the user has won. Call it and stop the program if it returns true.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner Help Runs but has Minor Error!
    By laxinnn in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 13th, 2013, 12:06 PM
  2. Need some minor help.
    By XxDarkstarxX in forum What's Wrong With My Code?
    Replies: 58
    Last Post: February 10th, 2013, 10:56 PM
  3. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  4. Java vending machine, minor thing.
    By Aprok in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2011, 02:35 PM
  5. [SOLVED] Please help with this... I think I have a minor coding error
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:02 AM

Tags for this Thread