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

Thread: Hangman Game

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman Game

    Alright, I have a hangman game due thursday at midnight and I've decided to start from scratch for various reasons.

    So what I want to do is this. I want to have a text file with one word per line, I want to go in and count the number of words, and then create an array sized as needed and store the words in it.

    The reason I want to do this is because, in the future instead of having to go in and modify code, someone could just attach a new text file and be done. If they wanted to use a bigger word bank, it wouldn't require modifying the code.

    After that I want to randomly pick a word and create the hangman interface. I know we are suppose to use panels and I am not entirely sure how to go about that logic. I think the easiest way so far would be to just break the word into individual letters and generate 2 sets of Jpanels. One to be the hangman itself And then the other set to be the word. The word's Jpanels would be created based on the number of letters and for every time guessed right.. the panel would change from - to whatever letter. Although I'm still debating on how best to program that part. If guessed wrong +1 to a counter and reveal a Jpanel of hangman. If counter reaches 5 "game over"

    The problem I'm having is actually more towards the beginning.

    I am trying to think of how best to get the number of lines.

    package Hangman;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.File;
     
    public class WordBank 
    	{		
    		public static int wordCount()
    		{
    			int x = 0;
    		       try {
    		           File words = new File("words.txt");
    		           FileReader reader = new FileReader(words);
    		           BufferedReader in = new BufferedReader(reader);
    		           String string;
    		           while ((string = in.readLine()) != null) {
    		             x++;
    		           }
    		           in.close();
    		         } catch (IOException e) {
    		           e.printStackTrace();
    		         }
    			return x;
    		}
     
    	}

    Is what I have... the only problem is it seems like its counter productive to repeat almost the exact same process again, just to add the words in the second time.

    Is there a better way to count the number of words in the text document?


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Hangman Game

    Instead of counting the words, creating an array, and then reading the words again, why not use an ArrayList: ArrayList (Java Platform SE 6)

    This way, you can create an ArrayList (since you don't need to know the size when you create it), and just add the words as you read them. This will avoid needing to read the file twice.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman Game

    *Edit*

    Ok I got it working but, I have to attach <Object> in the creation of the arraylist..

    will post again if more help is needed.

    Thank you.
    Last edited by Snow_Fox; November 29th, 2010 at 02:02 PM.

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Hangman Game

    You should really type the ArrayList with String and not Object, since you are populating it with strings

    ArrayList<String> list = new ArrayList<String>();

  5. The Following User Says Thank You to DavidFongs For This Useful Post:

    JavaPF (November 30th, 2010)

Similar Threads

  1. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  2. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM
  3. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  4. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM