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 3 of 3

Thread: Hangman Program for CP1

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman Program for CP1

    Okay, so I'm in high school and taking a mandatory Computer Programming class. I have to create a hangman program that the user can play over and over again using different words. However, whenever the user plays again, it just uses the same word from before. Can anyone help me?

    import java.util.*;
    import java.io.*;
    import java.net.*;

    public class Hangman
    {
    public static String getRandomWord()
    {
    try
    {
    URL page = new URL("http://projecteuler.net/project/words.txt");
    BufferedReader br= new BufferedReader(new InputStreamReader(page.openStream()));
    String pageSource=br.readLine();
    pageSource=pageSource.replace("\"","");
    String[] words=pageSource.split(",");
    int randNum=(int)(Math.random()*words.length);
    String word=words[randNum];
    word=word.toLowerCase();
    return word;
    }
    catch(Exception e)
    {
    return "hippopotamus";
    }
    }

    public static String Masquerade(String Word, String UsedLetters)
    {
    String NewWord= "";
    for (int i=0;i<Word.length();i++)
    {
    String letter = Word.substring(i,i+1);
    if (UsedLetters.contains(letter))
    {
    NewWord+=letter;
    }
    else
    {
    NewWord = NewWord+" _ ";
    }
    }
    NewWord=NewWord.replace(" ", " ");
    NewWord=NewWord.trim();
    return NewWord;
    }

    public static String Noose(int Miss)
    {
    if(Miss==1)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println("__|_______ \n");
    }

    if(Miss==2)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println("__|_______ \n");
    }

    if(Miss==3)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | / ");
    System.out.println(" | / ");
    System.out.println("__|_______ \n");
    }

    if(Miss==4)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if(Miss==5)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | \\ | ");
    System.out.println(" | \\| ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if (Miss==6)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | \\ | / ");
    System.out.println(" | \\|/ ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if (Miss==7)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | (x_x) ");
    System.out.println(" | \\ | / ");
    System.out.println(" | \\|/ ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }
    return "";
    }

    public static void main(String[] args)
    {
    String Word=getRandomWord();
    int Miss=0;
    String UsedLetters="";

    for (int i=0;i<100;i++)
    {
    Scanner Maryann=new Scanner(System.in);
    System.out.println("Guess a letter: ");
    String Input=Maryann.nextLine();
    String Guess=Input.toLowerCase();
    UsedLetters+=Guess;
    System.out.println(Masquerade(Word, UsedLetters));

    if (Word.contains(Guess))
    {
    Miss+=0;
    }
    else
    {
    Miss+=1;
    }
    Noose(Miss);

    if (Miss>=7)
    {
    System.out.println("You just lost the game.");
    System.out.println("The word was "+Word+".");
    System.out.println("Care to play again? Type y for yes, and n for no.");
    String Response=Maryann.nextLine();

    if(Response.equals("y"))
    {
    Miss=0;
    UsedLetters="";
    getRandomWord();
    }
    else if (Response.equals("n"))
    {
    break;
    }
    else
    {
    System.out.println("Type y for yes, and n for no.");
    }
    }

    if(!Masquerade(Word, UsedLetters).contains("_"))
    {
    System.out.println("Congratulations! You won!");
    System.out.println("The word was "+Word+".");
    System.out.println("Care to play again? Type y for yes, and n for no.");
    String Response=Maryann.nextLine();

    if (Response.equals("y"))
    {
    Miss=0;
    UsedLetters="";
    getRandomWord();
    }
    else if (Response.equals("n"))
    {
    break;
    }
    else
    {
    System.out.println("Care to play again? Type y for yes, and n for no.");
    }
    }
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman Program for CP1

    So, I'm taking a mandatory computer programming class at my high school, and we are supposed to create a hangman program that the user can play over and over again using different words. However, when the user plays again, mine uses the same word as before. Any help?

    Code=Java

    import java.util.*;
    import java.io.*;
    import java.net.*;

    public class Hangman
    {
    public static String getRandomWord()
    {
    try
    {
    URL page = new URL("http://projecteuler.net/project/words.txt");
    BufferedReader br= new BufferedReader(new InputStreamReader(page.openStream()));
    String pageSource=br.readLine();
    pageSource=pageSource.replace("\"","");
    String[] words=pageSource.split(",");
    int randNum=(int)(Math.random()*words.length);
    String word=words[randNum];
    word=word.toLowerCase();
    return word;
    }
    catch(Exception e)
    {
    return "hippopotamus";
    }
    }

    public static String Masquerade(String Word, String UsedLetters)
    {
    String NewWord= "";
    for (int i=0;i<Word.length();i++)
    {
    String letter = Word.substring(i,i+1);
    if (UsedLetters.contains(letter))
    {
    NewWord+=letter;
    }
    else
    {
    NewWord = NewWord+" _ ";
    }
    }
    NewWord=NewWord.replace(" ", " ");
    NewWord=NewWord.trim();
    return NewWord;
    }

    public static String Noose(int Miss)
    {
    if(Miss==1)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println("__|_______ \n");
    }

    if(Miss==2)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | ");
    System.out.println(" | ");
    System.out.println("__|_______ \n");
    }

    if(Miss==3)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | / ");
    System.out.println(" | / ");
    System.out.println("__|_______ \n");
    }

    if(Miss==4)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if(Miss==5)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | \\ | ");
    System.out.println(" | \\| ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if (Miss==6)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | ('_') ");
    System.out.println(" | \\ | / ");
    System.out.println(" | \\|/ ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }

    if (Miss==7)
    {
    System.out.println("\n _______ ");
    System.out.println(" | | ");
    System.out.println(" | (x_x) ");
    System.out.println(" | \\ | / ");
    System.out.println(" | \\|/ ");
    System.out.println(" | | ");
    System.out.println(" | / \\ ");
    System.out.println(" | / \\ ");
    System.out.println("__|_______ \n");
    }
    return "";
    }

    public static void main(String[] args)
    {
    String Word=getRandomWord();
    int Miss=0;
    String UsedLetters="";

    for (int i=0;i<100;i++)
    {
    Scanner Maryann=new Scanner(System.in);
    System.out.println("Guess a letter: ");
    String Input=Maryann.nextLine();
    String Guess=Input.toLowerCase();
    UsedLetters+=Guess;
    System.out.println(Masquerade(Word, UsedLetters));

    if (Word.contains(Guess))
    {
    Miss+=0;
    }
    else
    {
    Miss+=1;
    }
    Noose(Miss);

    if (Miss>=7)
    {
    System.out.println("You just lost the game.");
    System.out.println("The word was "+Word+".");
    System.out.println("Care to play again? Type y for yes, and n for no.");
    String Response=Maryann.nextLine();

    if(Response.equals("y"))
    {
    Miss=0;
    UsedLetters="";
    getRandomWord();
    }
    else if (Response.equals("n"))
    {
    break;
    }
    else
    {
    System.out.println("Type y for yes, and n for no.");
    }
    }

    if(!Masquerade(Word, UsedLetters).contains("_"))
    {
    System.out.println("Congratulations! You won!");
    System.out.println("The word was "+Word+".");
    System.out.println("Care to play again? Type y for yes, and n for no.");
    String Response=Maryann.nextLine();

    if (Response.equals("y"))
    {
    Miss=0;
    UsedLetters="";
    getRandomWord();
    }
    else if (Response.equals("n"))
    {
    break;
    }
    else
    {
    System.out.println("Care to play again? Type y for yes, and n for no.");
    }
    }
    }
    }
    }
    /code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hangman Program for CP1

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

    it just uses the same word from before.
    Where does it get the word from? How can it get a different word next time the game is played?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Hangman Help
    By devindadude in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 25th, 2013, 09:24 PM
  2. Java Hangman program! Need help reading an ar
    By mmagyar14 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2013, 03:18 AM
  3. Hangman program
    By psgtyler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 12:58 AM
  4. Need help with my hangman program!
    By mbae in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 29th, 2012, 07:24 PM
  5. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM

Tags for this Thread