Results 1 to 2 of 2

Thread: Hangman Game

  1. #1
    getshum is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Hangman Game

    So right now I am working on a very simple hangman game for class. Right now, the program runs great but I have run into a few problems... I know it is a few lines of code here and there but I don't know how to do it!

    The problem is:
    1. I don't know how to replace the "-" with letters they guessed right
    2. If I enter a correct letter over and over the game will eventually end saying I have won.
    3. I have no idea how to store the wrong guesses.. I want to store them and order them alphabetically and then print them out after every guess. What kind of storing process should I use?

    Code and hints are VERY appreciated!

    package Project3;
    import java.util.*;

    public class project3
    {

    int errors;
    int gameover;
    int counter;

    public project3(String name)
    {

    Dictionary dictionary = new Dictionary();

    String chsnWrd = dictionary.getRandomWord();

    //This makes the amount of characters in chsnWrd and makes it a number
    int lineSize = chsnWrd.length();
    System.out.println(lineSize);

    char blankLine [];

    //this makes a new char array = to the length of the randomly chosen word
    blankLine = new char[lineSize];
    for(int i=0;i<lineSize;i++)
    {blankLine[i] = ' ';}

    drawFigure();

    for(int underlineCounter = 0; underlineCounter < lineSize; underlineCounter++)
    {
    System.out.print("_ ");
    }

    System.out.println("\n\nTry and Guess the Word. Good Luck!\n");
    Scanner guessScan = new Scanner (System.in);


    int correct = 0;

    while(errors < 6)
    {
    String guess = guessScan.next();
    char guessChar = guess.charAt(0);
    boolean checkMe = false;
    System.out.println();
    for(int charCounter = 0; charCounter < lineSize; charCounter++)
    {

    if(guessChar == chsnWrd.charAt(charCounter))

    {
    System.out.println("Correct!\n");
    correct++;
    blankLine[charCounter] = guessChar;
    checkMe = true;
    }
    }

    if(!checkMe)
    {
    System.out.println("Incorrect...Try Again");
    errors++;

    }

    drawFigure();

    for(int i=0; i<lineSize; i++)
    {
    System.out.print(blankLine[i]);
    }

    System.out.println();


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

    if(correct == lineSize)
    {
    System.out.println("\nYou Won!");
    break;
    }
    }
    }

    void drawFigure ()
    {
    if(errors == 6)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("|- + -");
    System.out.println("| / \\ ");
    System.out.println();
    }

    if(errors == 5)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("|- + -");
    System.out.println("| / ");
    System.out.println();

    }

    if(errors == 4)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("|- + -");
    System.out.println("| ");
    System.out.println();

    }

    if(errors == 3)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("|- + ");
    System.out.println("| ");
    System.out.println();
    }

    if(errors == 2)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("| + ");
    System.out.println("| ");
    System.out.println();
    }
    if(errors == 1)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| 0");
    System.out.println("| ");
    System.out.println("| ");
    System.out.println();
    }
    if(errors == 0)
    {
    System.out.println("+-+");
    System.out.println("| |");
    System.out.println("| ");
    System.out.println("| ");
    System.out.println("| ");
    System.out.println();
    }
    }
    }

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    2.
    Java Code:
    for(int charCounter = 0; charCounter < lineSize; charCounter++)
    {
    [COLOR="Red"] if(guessChar != blankLine[charCounter])
    {[/COLOR]
       if(guessChar == chsnWrd.charAt(charCounter))
       {
           System.out.println("Correct!\n");
           correct++;
           blankLine[charCounter] = guessChar;
           checkMe = true;
       }
    [COLOR="Red"]}
    else 
    System.out.println("You have already guessed that letter");
    [/COLOR]
    }
    
    if(!checkMe)
    {
    System.out.println("Incorrect...Try Again");
    errors++;
    
    }
    3. you should use an array and add each incorrect guess to it. sort it
    Java Code:
    myArray.sort()
    to order the letters alphabetically
    Last edited by mr_guy; 02-26-2011 at 05:12 AM.

Similar Threads

  1. Hangman Game // HELP //
    By K-Scale in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 01:01 AM
  2. Hangman Game Help Please
    By 9tjh in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 04:19 AM
  3. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 03:24 PM
  4. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  5. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02: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
  •