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 Game

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman Game

    Hi i´m started to program a hangman game i Java not javascript i ecplice but now i´m stuck can anybode help me with the code so i can move forword just take the code and program it så it works PLEASE i cant anymore



    import java.util.*;



    public class GameLogic {
    private boolean isStarted;
    private int numberOfGuesses;
    private int maxGuesses;
    private List<String> guesses;
    private String currentWord;

    public void start() {
    isStarted = true;
    numberOfGuesses = 0;
    maxGuesses = 7;
    guesses = new ArrayList<>();
    currentWord = "Hello";

    }

    public void quit() {
    isStarted = false;

    }

    public void guess(String guess) {
    // gissa på ett ord.
    guesses.add(guess);
    {
    if (guess == currentWord)
    System.out.println(" you win!" );

    while (guess != currentWord)
    numberOfGuesses = numberOfGuesses +1;


    if (numberOfGuesses > maxGuesses)
    System.out.println("You lose" + currentWord);
    quit();

    }
    // koll om "guess" finns i (contains) order "currentWord".

    // Om sådant inte är fallet,
    // öka värdert på numberOfGuesses.

    //if (numberOfGuesses > maxGuesses){
    // quit();
    }



    public boolean isStarted() {
    return isStarted;

    }

    public void setStarted(boolean isStarted) {
    this.isStarted = isStarted;
    }

    public int getNumberOfGuesses() {
    return numberOfGuesses;

    }

    public void setNumberOfGuesses(int numberOfGuesses) {
    this.numberOfGuesses = numberOfGuesses;
    }

    public int getMaxGuesses() {
    return maxGuesses;

    }


    public void setMaxGuesses(int maxGuesses) {
    this.maxGuesses = maxGuesses;
    }

    public List<String> getGuesses() {
    return guesses;
    }

    public void setGuesses(List<String> guesses) {
    this.guesses = guesses;
    }

    public String getCurrentWord() {
    return currentWord;
    }

    public void setCurrentWord(String currentWord) {
    this.currentWord = currentWord;
    }


    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Hangman Game

    Quote Originally Posted by JohanMartin View Post
    can anybode help me with the code
    The first and most evident issue: strings content must be compared with equals method ... not with == .

    I can also see { } which are useless (e.g. the { after guesses.add(guess); ) even if this doesn't cause a compilation error.

    I suggest you to always use { } for if/for, etc... even if the body is a single statement.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Hangman Game

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    Don't give up. We can't do it for you, but we'll help you. Describe why you're stuck, ask specific questions about the results you're seeing and would like to change. If you're getting errors, post those too.

Similar Threads

  1. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 12:50 PM
  2. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  3. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  4. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  5. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM

Tags for this Thread