Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Hangman (http://www.programmingforums.org/showthread.php?t=14080)

sbob60 Oct 1st, 2007 7:09 PM

Hangman
 
Hi,

Before I begin, I am not asking anyone to do my homework for me, I know that is against forum rules. I just need a little help on where to start.

I have a CS assignment, and we have to make a hangman program. The basic output should be (if the word is "computer"):
:

_ _ _ _ _ _ _ _    // # of blanks = word.length()

Enter guess: // just a single char

/*if correct*/

c _ _ _ _ _ _ _    // increases variable for number of correct guesses (numCorrect++)

Enter guess:

/*if wrong*/
 
    O // Hangman's head
_ _ _ _ _ _ _ _    // increases variable for number of wrong guesses (numWrong++)

Enter guess:

// and so on


Our teacher is not allowing us to use string buffers, and I'm not really sure what to do. I get the basic idea of how to change the hangman, how to determine the winner, and such but I'm not sure what I'm supposed to start with when I display the blanks and how to update them (the game part).

The list of words is imported from a word.txt file. So all the words come from an arraylist.

Any tips would be greatly appreciated.

Thanks.

DaWei Oct 1st, 2007 9:57 PM

Write some code (we won't write it for you). We'll help you fix it. This is a common homework question, seen a dozen times a year.

sbob60 Oct 2nd, 2007 12:34 AM

I know this is horrifically bad code, but I'm a noob.

:

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class test
{
        private ArrayList <String> masterwords,list;
        private ArrayList <String> used;
        private String input, current;
        String blanks;
       
        public test()
        {
                masterwords.add("rsgh");
                masterwords.add("rsthh");
                masterwords.add("rstlfgfh");
                masterwords.add("rsgh");
                masterwords.add("rsthh");
                masterwords.add("rstlfgfh");
        }
       
       
       
        public ArrayList getList(int x)
        {
                if(x == 1)
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()<4 && getCommon(masterwords.get(y))>=2)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }
                }
               
                else if(x == 2)
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()>=4 && masterwords.get(y).length()<8 && getCommon(masterwords.get(y))>=3)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }
                }
               
                else
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()>=8)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }       
                }
                return list;
        }
       
        public int getCommon(String c)
        {
                int count = 0;
                       
                for(int x = 0; x <= c.length(); x++)
                {
                        if(c.charAt(x) == 'r' || c.charAt(x) == 's' || c.charAt(x) == 't' || c.charAt(x) == 'l' || c.charAt(x) == 'n' || c.charAt(x) == 'e' || c.charAt(x) == 'R' || c.charAt(x) == 'S' || c.charAt(x) == 'T' || c.charAt(x) == 'L' || c.charAt(x) == 'N' || c.charAt(x) == 'E')
                          count++;
                }
               
                return count;
        }
       
        public String getWord(int x)
        {
                ArrayList <String> lst = getList(x);
                Random rand = new Random(lst.size());
                int random = rand.nextInt();
                current = lst.get(random);
                return current;
        }
       
        public String getInput(String x)
        {
                input = x;
                used.add(input);
                return input;
        }
       
        public String getCur()
        {
                return current;
        }
       
        public boolean repeat(String x)
        {
                boolean a = false;

               
                for(int y=0; y<=used.size(); y++)
                {
                        if(getInput(x).equals(used.get(y)))
                        {
                                a = true;
                        }
                }
               
                return a;
        }
       
        public void makeBlanks(String s)
        {
                blanks = "";
                for(int x=1; x<s.length(); x++)
                {
                        blanks += "_ ";
                }
        }
       
        public String getBlanks()
        {
                return blanks;
        }
       
        public void updateBlanks(String s)
        {
                char sc = s.charAt(0);
                for(int x = 0; x<=getBlanks().length(); x++)
                {
                        if(getCur().charAt(x)==sc)
                        {
                                blanks.substring(x,x).replace('_',sc);
                        }
                }               
        }       
   
   

       

}


when i try to test this, i get a null pointer exception.

andro Oct 2nd, 2007 12:47 AM

Sure you do. In getList(), you use the variable "list" without ever initializing it. And that's probably not the only variable you do so with. I don't see "master" initialized anywhere either.

sbob60 Oct 2nd, 2007 3:33 PM

That wasn't the problem I was having because I hadn't tested that part yet. I fixed how the words are gathered, and I know the arraylist for the input is working fine. I am trying to test the "getBlanks" method but it is just outputting "null".

this is test.java:
:

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

class test
{
        private input in;
        private ArrayList <String> masterwords = new ArrayList();
        private ArrayList <String> list = new ArrayList();
        private ArrayList <String> used;
        private String input, current;
        String blanks;       
        private Scanner file;
        private Random rand;
       
       
        public test()throws IOException
        {
                in = new input("words.txt");
                masterwords = in.getMW();
        }

       
        public ArrayList getList(int x)
        {
                if(x == 1)
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()<4 && getCommon(masterwords.get(y))>=2)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }
                }
               
                else if(x == 2)
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()>=4 && masterwords.get(y).length()<8 && getCommon(masterwords.get(y))>=3)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }
                }
               
                else
                {
                        for(int y=0; y<=masterwords.size(); y++)
                        {
                                if(masterwords.get(y).length()>=8)
                                {
                                        list.add(masterwords.get(y));
                                }
                        }       
                }
                return list;
        }
       
        public int getCommon(String c)
        {
                int count = 0;
                       
                for(int x = 0; x <= c.length(); x++)
                {
                        if(c.charAt(x) == 'r' || c.charAt(x) == 's' || c.charAt(x) == 't' || c.charAt(x) == 'l' || c.charAt(x) == 'n' || c.charAt(x) == 'e' || c.charAt(x) == 'R' || c.charAt(x) == 'S' || c.charAt(x) == 'T' || c.charAt(x) == 'L' || c.charAt(x) == 'N' || c.charAt(x) == 'E')
                                count++;
                }
               
                return count;
        }
       
        public String getWord(int x)
        {
                ArrayList <String> lst = getList(x);
                Random rand = new Random(lst.size());
                int random = rand.nextInt();
                current = lst.get(random);
                return current;
        }
       
        public String getInput(String x)
        {
                input = x;
                used.add(input);
                return input;
        }
       
        public String getCur()
        {
                return current;
        }
       
        public boolean repeat(String x)
        {
                boolean a = false;

               
                for(int y=0; y<=used.size(); y++)
                {
                        if(getInput(x).equals(used.get(y)))
                        {
                                a = true;
                        }
                }
               
                return a;
        }
       
        public void makeBlanks(String s)
        {
                blanks = "";
                for(int x=1; x<s.length(); x++)
                {
                        blanks += "_ ";
                }
        }
       
        public String getBlanks()
        {
                return blanks;
        }
       
        public void updateBlanks(String s)
        {
                char sc = s.charAt(0);
                for(int x = 0; x<=getBlanks().length(); x++)
                {
                        if(getCur().charAt(x)==sc)
                        {
                                blanks.substring(x,x).replace('_',sc);
                        }
                }               
        }       
   
   

       

}


this is input.java
:


import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class input {
        private Scanner file;
        private Random rand;
        private ArrayList<String>masterwords;
       
        public input(String newFile)throws IOException
        {
                file = new Scanner(new File(newFile));
                rand = new Random();
                masterwords = new ArrayList<String>()
                while(file.hasNext())
                {
                        String nextWord = file.nextLine();
                        masterwords.add(nextWord);
                }//while
               
               
        }//input
       
        public ArrayList getMW()
        {
                return masterwords;
        }
   
}


I am really lost. :confused:

sbob60 Oct 2nd, 2007 4:10 PM

fixed the "null" thing but I'm getting the null pointer. Where am i supposed to initialize it?

andro Oct 2nd, 2007 6:35 PM

I don't see "ArrayList<String> used" initialized anywhere, do you?

sbob60 Oct 2nd, 2007 7:38 PM

Well, ya, i fixed that. But the null pointer is coming in the "getList" method

:

for(int y=0; y<=masterwords.size(); y++)
{
        if(masterwords.get(y).length()<4 && getCommon(masterwords.get(y))>=2)
        {
                list.add(masterwords.get(y));
        }
}


thats the section im testing, and it gives me an error in the if statement. I'm not sure whats going on.


All times are GMT -5. The time now is 3:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC