Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 1st, 2007, 6:09 PM   #1
sbob60
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 sbob60 is on a distinguished road
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.
sbob60 is offline   Reply With Quote
Old Oct 1st, 2007, 8:57 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 1st, 2007, 11:34 PM   #3
sbob60
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 sbob60 is on a distinguished road
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.
sbob60 is offline   Reply With Quote
Old Oct 1st, 2007, 11:47 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 311
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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.
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Oct 2nd, 2007, 2:33 PM   #5
sbob60
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 sbob60 is on a distinguished road
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.

Last edited by sbob60; Oct 2nd, 2007 at 2:58 PM.
sbob60 is offline   Reply With Quote
Old Oct 2nd, 2007, 3:10 PM   #6
sbob60
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 sbob60 is on a distinguished road
fixed the "null" thing but I'm getting the null pointer. Where am i supposed to initialize it?
sbob60 is offline   Reply With Quote
Old Oct 2nd, 2007, 5:35 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 311
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
I don't see "ArrayList<String> used" initialized anywhere, do you?
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Oct 2nd, 2007, 6:38 PM   #8
sbob60
Newbie
 
Join Date: Oct 2007
Posts: 6
Rep Power: 0 sbob60 is on a distinguished road
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.
sbob60 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob trying to code hangman :( commodore Python 12 May 17th, 2006 2:26 PM
[Ruby] Hangman Jessehk Show Off Your Open Source Projects 0 Mar 16th, 2006 9:55 PM
[Python] Simple Hangman game Jessehk Show Off Your Open Source Projects 5 Jan 24th, 2006 8:36 AM
Pseudocode Hass Java 6 Nov 22nd, 2005 1:18 PM
hangman problem ending too soon thebignood C 10 Nov 10th, 2005 6:52 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:55 PM.

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