Results 1 to 9 of 9

Thread: Hangman Game

  1. #1
    L23
    L23 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default Hangman Game

    The first step in the assignment is to create a Word class with 3 data members. One of type File that gets set by the constructor and two integers called counter and amountOfWords. The class has three methods:
    getAmountOfWords() that returns the amount of words
    countWords() that counts the how many words are in the File and then sets amountOfWords
    nextWord() that returns the next word,making use of counter to keep track of at which word was it before.

    I have completed the first two method but I am having trouble with the last one.

    This is what I have done so far:
    import java.io.*;
    import java.util.*;

    public class Word
    {
    File myFile;
    int counter=0;
    int amountOfWords;

    public Word(File f)
    {
    myFile=f;
    }

    public int getAmountOfWords()throws FileNotFoundException,IOException
    {
    countWords();
    return amountOfWords;

    }

    public void countWords()throws FileNotFoundException,IOException
    {
    int count=0;
    FileReader fr=new FileReader(myFile);
    BufferedReader br=new BufferedReader(fr);
    String line=br.readLine();
    while(line!=null)
    {
    StringTokenizer st=new StringTokenizer(line);
    while(st.hasMoreTokens())
    {
    String word=st.nextToken();
    count++;


    }
    line=br.readLine();
    }

    amountOfWords=count;

    }




    public String nextWord()throws FileNotFoundException,IOException
    {
    String nextWord="";
    ArrayList list = new ArrayList();
    FileReader fr=new FileReader(myFile);
    BufferedReader br=new BufferedReader(fr);
    String line=br.readLine();
    while(line!=null)
    {
    StringTokenizer st=new StringTokenizer(line);
    while(st.hasMoreTokens())
    {

    list.add(st.nextToken());

    }


    line=br.readLine();
    }

    for(int x=0;x<11;x++)
    {



    }

    return nextWord;
    }

    }



    I am not sure how to use the for loop to move through the array list and display the next word using the counter to track the word it was before Ive tried this in the for loop:

    counter=list.indexOf(nextWord)+1;
    nextWord=list.get(counter).toString();

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Please put in link to other postings of this problem so people don't waste time repeating solutions.

  3. #3
    L23
    L23 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Please could you explain how to do that...

    Thanks.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    It makes more sense to read in all the words only one time and to then access them as needed. Or to read in words until you get to the counter word and return it.
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class WordRx
    {
        File myFile;
        int counter=0;
        int amountOfWords;
        List<String> list;
    
        public WordRx(File f)
        {
            myFile=f;
            // Call to [i]countWords[/i] required for list to be filled.
            list = new ArrayList<String>();
        }
    
        public int getAmountOfWords() throws FileNotFoundException,
                                             IOException
        {
            countWords();
            return amountOfWords;
        }
    
        public void countWords() throws FileNotFoundException,
                                        IOException
        {
            int count=0;
            FileReader fr=new FileReader(myFile);
            BufferedReader br=new BufferedReader(fr);
            String line=br.readLine();
            while(line!=null)
            {
                StringTokenizer st=new StringTokenizer(line);
                while(st.hasMoreTokens())
                {
                    String word=st.nextToken();
                    list.add(word);
                    count++;
                }
                line=br.readLine();
            }
    
            amountOfWords=count;
        }
    
        public String nextWord() throws FileNotFoundException,
                                        IOException
        {
            String nextWord=""; 
            FileReader fr=new FileReader(myFile);
            BufferedReader br=new BufferedReader(fr);
            String line=br.readLine();
            int count = 0;
            while(line!=null)
            {
                StringTokenizer st=new StringTokenizer(line);
                while(st.hasMoreTokens())
                {
                    nextWord = st.nextToken();
                    if(count == counter) {
                        counter++;
                        return nextWord;
                    }
                    count++;
                }
                line=br.readLine();
            }
            return nextWord;
        }
    
        private String getNextWord() {
            //if(counter < list.size())
            return list.get(counter++);
        }
    
        public static void main(String[] args) throws FileNotFoundException,
                                                      IOException {
            String path = "tutorial/dictionary.txt";
            File file = new File(path);
            WordRx test = new WordRx(file);
            int wordCount = test.getAmountOfWords();
            System.out.println("wordCount = " + wordCount);
            for(int i = 0; i < 10; i++) {
                System.out.println(test.nextWord());
            }
            for(int i = 0; i < 10; i++) {
                System.out.println(test.getNextWord());
            }
        }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    To add a link, use the icon above the Message input box.
    Get the link, the press the icon and you'll get a prompt to enter the link in:
    http://aLink.to.nowhere

  6. #6
    Alan-LB's Avatar
    Alan-LB is offline Member
    Join Date
    Jun 2008
    Location
    Junee, NSW, Australia
    Posts
    19
    Rep Power
    0

    Default

    CodeGuru Forums - Programming the Hangman Game

    www(dot)codeguru(dot)com/forum/showthread.php?p=1736603#post1736603

    Alan
    There are 10 types of people - those who understand binary and those who don't!!
    Today is the Beta version of Tomorrow!!

  7. #7
    L23
    L23 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks Alan-LB

    Thank you Hard Wired the code now reads the next word. I have read the words till I got to the counter and then returned it.

  8. #8
    L23
    L23 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    I need to update a players Information. I am able to change the players information by the following method:

    public String updatePlayerInfo(int playerNr,int correct,int gamesPlayed)throws FileNotFoundException,IOException
    {
    String playerInfo="";
    FileReader fr=new FileReader(myFile);
    BufferedReader br=new BufferedReader(fr);
    String line=br.readLine();
    while(line!=null)
    {


    StringTokenizer st=new StringTokenizer(line);
    String num=st.nextToken();
    int playerNum=Integer.parseInt(num);
    String name=st.nextToken();
    String score=st.nextToken();
    int correctGames=Integer.parseInt(score);
    String play=st.nextToken();
    int played=Integer.parseInt(play);


    if(playerNum==playerNr)
    {


    correctGames=correct;
    played=gamesPlayed;

    playerInfo=playerInfo+playerNum+" "+name+" "+correctGames+" "+played;
    playerInfo=playerInfo+"\n";


    }
    line=br.readLine();
    }
    return playerInfo;
    }

    I am having trouble updating the text file with the new information

  9. #9
    L23
    L23 is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM
  3. Battery 1.00 (Game)
    By M77 in forum Java Software
    Replies: 1
    Last Post: 05-12-2008, 09:56 AM
  4. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:16 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 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
  •