Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Hangman

$
0
0
Hey guys, first off I want to say that I am very new to java and programming in general. I am a junior in HS in my CS1 class and we haven't exactly covered advanced methods of doing things. As you can see by the title, the project we are working on is hangman. The only rules for the project are that we must have a user make up a word and input it into the password field. Then we just give the use 8 tries to guess the word. I have tried looking for examples, but all of those consist of people using text documents containing a list a words. Mine, on the other hand, has a user enter a word. Also, I know that this code is extremely crude as I not know a whole lot.

Here is my code

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import java.util.Scanner;
public class Hangman_New
{
    public static void main(String[] args)
    {
        String secretWord = "";
        Scanner scan = new Scanner(System.in);

        String m0 = ("      __________\n     |         |\n     |         |\n               |\n               |\n               |\n               |\n               |\n               |\n__________________________");
        String m1 = ("      __________\n     |         |\n     |         |\n     o         |\n               |\n               |\n               |\n               |\n               |\n__________________________");
        String m2 = ("      __________\n     |         |\n     |         |\n     o         |\n     |         |\n               |\n               |\n               |\n               |\n__________________________");
        String m3 = ("      __________\n     |         |\n     |         |\n     o         |\n    \\|         |\n               |\n               |\n               |\n               |\n__________________________");
        String m4 = ("      __________\n     |         |\n     |         |\n     o         |\n    \\|/        |\n               |\n               |\n               |\n               |\n__________________________");
        String m5 = ("      __________\n     |         |\n     |         |\n     o         |\n    \\|/        |\n     |         |\n               |\n               |\n               |\n__________________________");
        String m6 = ("      __________\n     |         |\n     |         |\n     o         |\n    \\|/        |\n     |         |\n    /          |\n               |\n               |\n__________________________");
        String m7 = ("      __________\n     |         |\n     |         |\n     o         |\n    \\|/        |\n     |         |\n    / \\        |\n               |\n               |\n__________________________");

        JPasswordField passField = new JPasswordField();
        int userChoice = JOptionPane.showConfirmDialog(
                null, passField, "Enter your secret word:", 
                JOptionPane.OK_CANCEL_OPTION);

        if (userChoice == JOptionPane.OK_OPTION)
        {
            secretWord = new String(passField.getPassword());
            System.out.println("Your secret word was: \"" + secretWord + "\"");
        }
        else;

        System.out.print("Choose your first letter: ");
        String firstLetter = scan.nextLine();

        if(secretWord.contains(firstLetter))
        {
            System.out.println(m0);
        }
        else;
        {
            System.out.println(m1);
        } 

        System.out.print("Choose your second letter: ");
        String secondLetter = scan.nextLine();

        if (secretWord.contains(secondLetter))
        {
            if (secretWord.contains(firstLetter))
            {
                System.out.println(m0);
            }
            else;
            {
                System.out.println(m1);
            }
        }
        else;
        {
            if (secretWord.contains(firstLetter))
            {
                System.out.println(m1);
            }
            else;
            {
                System.out.println(m2);
            }
        }

        System.out.print("Choose your third letter: ");
        String thirdLetter = scan.nextLine();

        if (secretWord.contains(thirdLetter))
        {
            if (secretWord.contains(secondLetter))
            {
                if (secretWord.contains(firstLetter))
                {
                    System.out.println(m0);
                }
                else;
                {
                    System.out.println(m1);
                }
            }
            else;
            {
                System.out.println(m1);
            }
        }
        else;
    }
}


I thought that I was doing well until I couldn't find a way to check to see if the entered letter was in the secret word that was entered at the beginning. I tried things like secretWord.contains() and secretWord.indexof() but couldn't find a solution.

If someone could point me in the right direction, that would be greatly appreciated.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>