![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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 onOur 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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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/ |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
fixed the "null" thing but I'm getting the null pointer. Where am i supposed to initialize it?
|
|
|
|
|
|
#7 |
|
Professional Programmer
|
I don't see "ArrayList<String> used" initialized anywhere, do you?
__________________
http://www.kevinherron.com/ |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2007
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |