Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Basic Hangman Game

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Post Basic Hangman Game

    Hello again fellow programmers! :)

    As the title implies, I've coded a basic hangman game. It works as I want it to, well almost. I have used "random" numbers (I know that computers can't produce completely random numbers), but they're not enough random for me, because the game always chooses the same word! Which makes it quite a boring game, don't you think? Do you guys know how to code the program to choose words "randomly", or at least more randomly than before? If you do and would like to share that piece of knowledge with me, I would be very grateful! :)

    Thanks in advance!

    Java Code:
    import java.util.Scanner;
    import java.util.Vector;
    import java.util.Arrays;
    import java.util.Random;
    
    public class hej{
    	public static void main(String[] args){
    		Scanner hej = new Scanner(System.in);
    		Random generator = new Random(1000);
    		Vector<String> ord = new Vector<String>();
    		ord.addAll(Arrays.asList("EXCELLENT", "DIFFERENT", "PRESSURE"));
    		int wordSelector = generator.nextInt(3)+1, guesses = 0, maxGuesses = 7;
    		String word = ord.get(wordSelector), hiddenWord = "", guessedLetters = "";
    		char currentGuess;
    		for(int x=0;x<word.length();x++){
    			hiddenWord += "-";
    		}
    		boolean running = true;
    		System.out.print("-= Hangman =-\n\n");
    		while(running){
    			System.out.print("Word: " + hiddenWord + "\nAmount of guesses left: " + (maxGuesses-guesses) + " out of " + maxGuesses + "\nGuessed letters: " + guessedLetters + "\n\nEnter a letter: ");
    			currentGuess = hej.next().charAt(0);
    			if(currentGuess >= 97 && currentGuess <= 122)
    				currentGuess -= 32;
    			guessedLetters += currentGuess;
    			if(word.indexOf(currentGuess) == -1){
    				System.out.println("Your guess was incorrect!");
    				guesses++;
    			}else{
    				System.out.println("Your guess was correct!");
    				char replacer1[] = word.toCharArray(), replacer2[] = hiddenWord.toCharArray();
    				for(int x=0;x<word.length();x++){
    					if(currentGuess == replacer1[x]){
    						replacer2[x] = currentGuess;
    					}
    				}
    				word = new String(replacer1); hiddenWord = new String(replacer2);
    				word.trim(); hiddenWord.trim();
    			}
    			if(hiddenWord.equals(word) || guesses == maxGuesses){
    				running = false;
    			}
    		}
    		if(hiddenWord.equals(word))
    			System.out.print("Congratulations! You guessed the right word!");
    		else if(guesses == maxGuesses)
    			System.out.print("You lost due to too many incorrect guesses!");
    	}
    }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: Basic Hangman Game

    If you are concerned about having enough random words, you can probably find a dictionary file of some sort (a .txt file) that has a single word on each line.
    Once you have that text file, and you know how many words it contains, you could generate a random number bound between 1 and that size(let's call that random number r).
    Then, write a method that retrieves the word at line r in the file, and use that as your hangman word:

    Java Code:
    private String getWordFromFileAtLine(int lineNumber) {
      //file reader code.....
    }
    If the file is sufficiently large, you would have a good chance of not repeating words.
    That is how I would approach it.
    Last edited by sehudson; 09-11-2013 at 04:34 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Basic Hangman Game

    Quote Originally Posted by Zelaine View Post
    ... the game always chooses the same word!
    Java Code:
    		Random generator = new Random(1000);
    That's what you get when you use a seeded Random. Use the other Random constructor.

    db
    Huskies likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Basic Hangman Game

    Random generator = new Random(1000);

    You always use the same seed. Use System.currentTimeMillis() instead. Then you will find out that this
    int wordSelector = generator.nextInt(3)+1
    will cause an exception
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Basic Hangman Game

    Java Code:
    generator.nextInt(3)+1
    That generates a random number from 1-4, while you want one that ranges from 0 to [size of word list - 1].

    because the game always chooses the same word
    I find that hard to believe. And its easy to disprove with a little test program:

    Java Code:
    import java.util.*;
    
    public class Test2
    {
       
       public static void main(String[] args)
       {
         Random generator = new Random(1000);
         List<String> ord = Arrays.asList("EXCELLENT", "DIFFERENT", "PRESSURE");
    	 
    	 for(int i = 0; i < 30; i++){
    	   int wordSelector = generator.nextInt(ord.size());
    	   System.out.println("RANDOM WORD: " + ord.get(wordSelector));
    	 }
       }
    }
    Output:

    XML Code:
    RANDOM WORD: DIFFERENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: EXCELLENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: EXCELLENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: DIFFERENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: PRESSURE
    RANDOM WORD: EXCELLENT
    RANDOM WORD: EXCELLENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: EXCELLENT
    RANDOM WORD: EXCELLENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: DIFFERENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: DIFFERENT
    RANDOM WORD: EXCELLENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: PRESSURE
    RANDOM WORD: DIFFERENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: PRESSURE
    RANDOM WORD: DIFFERENT
    RANDOM WORD: PRESSURE
    RANDOM WORD: EXCELLENT
    RANDOM WORD: PRESSURE
    See? Not always the same, just lots of repetition, which is not entirely hard to understand since you have only three words. Here is a suggestion: change your application so it won't allow the same word twice in a row.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Basic Hangman Game

    sehudson, good idea, however I don't have the knowledge of reading in data from files yet. Though when I have, I will definitely test it out. Thanks for the tip :)
    DarrylBurke, thanks for the tip, I'll check it out :)
    gimbal2, I expanded my game so that it had a few more words, but the program still chose the same word the five times I tested it. Even if I closed my IDE and opened it again and ran the program again, it still chose the same word over and over again. So I don't know how you got that output.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Basic Hangman Game

    I even gave you the code to try for yourself. If that produces different results on your computer, that would be incredibly strange. But you got some suggestions to improve that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

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