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

Thread: Hangman Help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman Help

    My code is not updating write after i enter a guess can someone help me out i cant think of what would be wrong

    This is my hangman class

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


    public class Hangman {

    private ArrayList<String> pool;
    private ArrayList<Character> lGuess;
    private String secret;
    private StringBuffer hideWord;
    private StringBuffer charLetters;
    private StringBuffer disguise = new StringBuffer();



    public Hangman(){
    pool = new ArrayList<>();
    lGuess = new ArrayList<>();

    pool.add("easy");
    pool.add("hard");
    pool.add("soft");
    pool.add("cheat");
    pool.add("genius");
    pool.add("smart");
    pool.add("dumb");
    pool.add("sport");
    pool.add("football");
    pool.add("basketball");
    pool.add("baseball");
    pool.add("track");
    pool.add("soccer");
    pool.add("computer");
    pool.add("science");
    pool.add("math");
    pool.add("history");
    pool.add("english");
    pool.add("study");
    pool.add("quiz");
    pool.add("test");
    pool.add("television");
    pool.add("picture");
    pool.add("teacher");
    pool.add("student");
    pool.add("couch");
    pool.add("bed");
    pool.add("chair");
    pool.add("backpack");
    pool.add("summer");
    pool.add("graduate");
    lGuess.add('a');
    lGuess.add('b');
    lGuess.add('c');
    lGuess.add('d');
    lGuess.add('e');
    lGuess.add('f');
    lGuess.add('g');
    lGuess.add('h');
    lGuess.add('i');
    lGuess.add('j');
    lGuess.add('k');
    lGuess.add('l');
    lGuess.add('m');
    lGuess.add('n');
    lGuess.add('o');
    lGuess.add('p');
    lGuess.add('q');
    lGuess.add('r');
    lGuess.add('s');
    lGuess.add('t');
    lGuess.add('u');
    lGuess.add('v');
    lGuess.add('w');
    lGuess.add('x');
    lGuess.add('y');
    lGuess.add('z');

    secret = pool.get((int)Math.floor(Math.random()*pool.size() )); // creates the secret word
    }

    public StringBuffer choseWord(String s){ // method that makes the string dashes

    hideWord = new StringBuffer(s.length());
    hideWord.append(s);
    for(int i = 0; i < s.length(); i++){
    disguise.append('-');
    }
    return disguise;

    }

    public StringBuffer letterDict(){ // method that shows the letter guess as a stringbuffer
    charLetters = new StringBuffer(lGuess.size());
    for(int i = 0; i < lGuess.size(); i++){
    charLetters.append(lGuess.get(i));
    }
    return charLetters;
    }

    public void matchLetter(Character c){ // method that should match the guessed letter with hidden word
    for(int i = 0; i < hideWord.length(); i++){
    if(c == hideWord.charAt(i) ){
    disguise.setCharAt(i, c);
    // hideWord = hideWord.append(hideWord.substring(0, i)).append(secret.charAt(i)).append(hideWord.subst ring(i+1,secret.length()));
    }
    }

    }

    public StringBuffer eraseDict(Character c){ // method that is suppose to erase the letter dictionary when the letter is guessed
    for(int i = 0; i < secret.length(); i++){
    if(c == hideWord.charAt(i) && lGuess.contains(c)){
    lGuess.remove(i);
    }
    charLetters = new StringBuffer(lGuess.size());
    for(int j = 0; j < lGuess.size(); j++){
    charLetters.append(lGuess.get(j));
    }
    }
    return charLetters;
    }

    public Boolean gameOver(){ // boolean to say the game is over when word equals secret word
    return secret.contentEquals(disguise.toString());

    }

    public ArrayList<String> getPool() {
    return pool;
    }


    public ArrayList<Character> getlGuess() {
    return lGuess;
    }

    public String getSecret() {
    return secret;
    }

    public StringBuffer getHideWord() {
    return hideWord;
    }
    }

    This is my main class

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

    public class Homework1 {

    public static void main(String[] args) throws IOException{

    Scanner input = new Scanner(System.in);
    boolean valid = false;
    String guess;
    Hangman play = new Hangman();


    System.out.println("****************************** ***");
    System.out.println("*****THE WORD GUESS GAME!!!!*****");
    System.out.println("****************************** ***"+"\n");
    System.out.println("**********Have Fun!!!!************"+"\n");
    System.out.println("The word is displayed for working purposes:");
    System.out.println("Your word is: "+play.getSecret().toString()+"\n");

    while(!valid){
    System.out.println("Word is:"+play.choseWord(play.getSecret())+"\n");
    System.out.println("Unused Letters:"+play.letterDict()+"\n");
    System.out.println("Guess a letter:");
    guess = input.next().toLowerCase();
    Character ch = guess.charAt(0);
    if(guess.length() == 0 || Character.isDigit(ch)){
    System.out.println("Please enter a correct value");
    continue;
    }
    if(guess.length() > 1){
    System.out.println("Too many values, one at a time");
    continue;
    }
    else{
    if(play.getSecret().indexOf(ch) < 0) {
    System.out.println("Wrong guess");
    play.eraseDict(ch);
    }
    else{
    play.matchLetter(ch);
    play.eraseDict(ch);
    }
    if(play.gameOver()){
    System.out.println("You won!!!!");
    System.out.println("The word was:"+play.getSecret());
    valid = true;

    }
    }
    //System.out.print("Would you like to play again, y/n:");
    }
    }
    }


  2. #2
    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 Help

    My code is not updating
    Can you explain what the code does
    and what you want it to do.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Help

    It is like hangman just a word guessing game with unlimited tries until you get the word right. The word is chosen from my arraylist of words and disguised by dashes.

    When i run the program presents the letter in disguise with the dashes. When i enter the guess it updates the word but it adds more dashes to the word. I am having problems with my dictionary that shows to delete the letter that was guessed but keep the dictionary up for next guesses

    <import java.util.*;
    import java.io.*;
     
     
    public class Hangman {
     
    private ArrayList<String> pool;
    private ArrayList<Character> lGuess;
    private String secret;
    private StringBuffer hideWord;
    private StringBuffer charLetters;
    private StringBuffer disguise = new StringBuffer();
     
     
     
    public Hangman(){
    pool = new ArrayList<>();
    lGuess = new ArrayList<>();
     
    pool.add("easy");
    pool.add("hard");
    pool.add("soft");
    pool.add("cheat");
    pool.add("genius");
    pool.add("smart");
    pool.add("dumb");
    pool.add("sport");
    pool.add("football");
    pool.add("basketball");
    pool.add("baseball");
    pool.add("track");
    pool.add("soccer");
    pool.add("computer");
    pool.add("science");
    pool.add("math");
    pool.add("history");
    pool.add("english");
    pool.add("study");
    pool.add("quiz");
    pool.add("test");
    pool.add("television");
    pool.add("picture");
    pool.add("teacher");
    pool.add("student");
    pool.add("couch");
    pool.add("bed");
    pool.add("chair");
    pool.add("backpack");
    pool.add("summer");
    pool.add("graduate");
    lGuess.add('a');
    lGuess.add('b');
    lGuess.add('c');
    lGuess.add('d');
    lGuess.add('e');
    lGuess.add('f');
    lGuess.add('g');
    lGuess.add('h');
    lGuess.add('i');
    lGuess.add('j');
    lGuess.add('k');
    lGuess.add('l');
    lGuess.add('m');
    lGuess.add('n');
    lGuess.add('o');
    lGuess.add('p');
    lGuess.add('q');
    lGuess.add('r');
    lGuess.add('s');
    lGuess.add('t');
    lGuess.add('u');
    lGuess.add('v');
    lGuess.add('w');
    lGuess.add('x');
    lGuess.add('y');
    lGuess.add('z');
     
    secret = pool.get((int)Math.floor(Math.random()*pool.size() )); // creates the secret word 
    }
     
    public StringBuffer choseWord(String s){ // method that makes the string dashes
     
    hideWord = new StringBuffer(s.length());
    hideWord.append(s);
    for(int i = 0; i < s.length(); i++){
    disguise.append('-');
    }
    return disguise;
     
    }
     
    public StringBuffer letterDict(){ // method that shows the letter guess as a stringbuffer
    charLetters = new StringBuffer(lGuess.size());
    for(int i = 0; i < lGuess.size(); i++){
    charLetters.append(lGuess.get(i));
    }
    return charLetters;
    }
     
    public void matchLetter(Character c){ // method that should match the guessed letter with hidden word
    for(int i = 0; i < hideWord.length(); i++){
    if(c == hideWord.charAt(i) ){
    disguise.setCharAt(i, c);
    // hideWord = hideWord.append(hideWord.substring(0, i)).append(secret.charAt(i)).append(hideWord.subst ring(i+1,secret.length()));
    }
    }
     
    }
     
    public StringBuffer eraseDict(Character c){ // method that is suppose to erase the letter dictionary when the letter is guessed
    for(int i = 0; i < secret.length(); i++){
    if(c == hideWord.charAt(i) && lGuess.contains(c)){
    lGuess.remove(i);
    }
    charLetters = new StringBuffer(lGuess.size());
    for(int j = 0; j < lGuess.size(); j++){
    charLetters.append(lGuess.get(j));
    }
    }
    return charLetters;
    }
     
    public Boolean gameOver(){ // boolean to say the game is over when word equals secret word
    return secret.contentEquals(disguise.toString());
     
    }
     
    public ArrayList<String> getPool() {
    return pool;
    }
     
     
    public ArrayList<Character> getlGuess() {
    return lGuess;
    }
     
    public String getSecret() {
    return secret;
    }
     
    public StringBuffer getHideWord() {
    return hideWord;
    }
    }>

    <import java.util.*;
    import java.io.*;
     
    public class Homework1 {
     
    public static void main(String[] args) throws IOException{
     
    Scanner input = new Scanner(System.in);
    boolean valid = false;
    String guess;
    Hangman play = new Hangman();
     
     
    System.out.println("****************************** ***");
    System.out.println("*****THE WORD GUESS GAME!!!!*****");
    System.out.println("****************************** ***"+"\n");
    System.out.println("**********Have Fun!!!!************"+"\n");
    System.out.println("The word is displayed for working purposes:");
    System.out.println("Your word is: "+play.getSecret().toString()+"\n");
     
    while(!valid){
    System.out.println("Word is:"+play.choseWord(play.getSecret())+"\n");
    System.out.println("Unused Letters:"+play.letterDict()+"\n"); 
    System.out.println("Guess a letter:");
    guess = input.next().toLowerCase();
    Character ch = guess.charAt(0);
    if(guess.length() == 0 || Character.isDigit(ch)){
    System.out.println("Please enter a correct value");
    continue;
    }
    if(guess.length() > 1){
    System.out.println("Too many values, one at a time");
    continue;
    }
    else{
    if(play.getSecret().indexOf(ch) < 0) {
    System.out.println("Wrong guess");
    play.eraseDict(ch);
    }
    else{
    play.matchLetter(ch);
    play.eraseDict(ch);
    }
    if(play.gameOver()){
    System.out.println("You won!!!!");
    System.out.println("The word was:"+play.getSecret());
    valid = true;
     
    }
    } 
    //System.out.print("Would you like to play again, y/n:");
    } 
    } 
    }>

  4. #4
    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 Help

    it adds more dashes to the word.
    Where does the code do that? Why does it add any more dashes after it first creates the disguise variable?

    What's wrong with the formatting of the code now? All the statements start in the first column.
    Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Help

    I dont know if you have tried to compile it or not but it happens when i compile the code. After i enter in my first guess it adds more dashes to word even though it stays the same, thus not allowing my gameOver method to not work correctly

  6. #6
    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 Help

    it adds more dashes to word
    Where does the code do that? Why does it add any more dashes after it first creates the disguise variable with all the needed dashes?

    What's wrong with the formatting of the code now? All the statements start in the first column.
    Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Help

    What's wrong with the formatting of the code now? All the statements start in the first column.
    Nested statements should be indented.
    The nested statements are indented it is just the way the code was copied into thread.

    Where does the code do that? Why does it add any more dashes after it first creates the disguise variable with all the needed dashes?
    Im guessing it does this in the loop some how when i do the guesses one by one, im just not understanding why it is my self. I have seen other examples and it does not do that,. Do you think because I am using StringBuffers instead of regular strings

  8. #8
    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 Help

    The nested statements are indented it is just the way the code was copied into thread.
    Look at the code in post#3 and tell me which statements are indented.

    Im guessing it does this
    Who wrote this code? The author should know where the code adds '-' to the StringBuffer.
    Use the editor's find/search function to find the - character to see where it is added.

    The problem is the logic of the code, not the classes:StringBuffer vs String.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Help

    i wrote the code and the method for the String buffer is this
    <public StringBuffer choseWord(String s){ // method that makes the string dashes
     
    hideWord = new StringBuffer(s.length());
    hideWord.append(s);
    for(int i = 0; i < s.length(); i++){
    disguise.append('-');
    }
    return disguise;
     
    }>

    and this is how the indents will go
    <else{
               if(play.getSecret().indexOf(ch) < 0) {
               System.out.println("Wrong guess");
               play.eraseDict(ch);
               }
           else{
           play.matchLetter(ch);
           play.eraseDict(ch);
          }
    if(play.gameOver()){
    System.out.println("You won!!!!");
    System.out.println("The word was:"+play.getSecret());
    valid = true;
     
    }
    } >

  10. #10
    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 Help

    this is how the indents will go
    Look at post#3. I don't see them.

    Let me repeat my earlier questions:
    Why does the code add any more dashes after it first creates the disguise variable with all the needed dashes?
    Isn't doing once enough? Why call the method more than once?

    Perhaps the logic needs to be reworked to separate out what should be done once and what is to be done every time a new letter is guessed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Help

    i think i see what you are getting at because have the recall of my hidden word in the while loop so it goes everytime

Similar Threads

  1. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  2. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  3. Java Hangman!
    By JavaManNoob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 11:17 PM
  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