| | |
desperate plee for help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 4
Reputation:
Solved Threads: 0
Hey there guys I'm new to java OOP and im not very good at it at all. I have a uni assignment due in soon which i mostly completed but I'am stuck on this questions which ask us to make a guessword game. this is the problem
Problem 3 – The Guess-a-Word Game (15% implementation, 10%
design, code, layout and comments)
The guess-a-word game, as the name suggests, is a game of guessing words. You will be playing
this game with the computer in charge of the game. Suppose the word you have to guess is:
PROGRAMMING
The computer would start by displaying the word with each letter replaced by an asterisk:
***********
Number of mistakes = 0
Wrongly guessed letters = “�
Letters available = “ABCDEFGHIJKLMNOPQRSTUVWXYZ�
That display means that so far you have not made any mistakes and the set of wrongly guessed
letters is empty.
You are then asked to play a letter. Suppose you play letter “M�. The computer would display:
******MM***
Number of mistakes = 0
Wrongly guessed letters = “�
Letters available = “ABCDEFGHIJKLNOPQRSTUVWXYZ�
Next suppose you play letter “E� which is not one of the letters of the word. Then the computer
would display:
******MM***
Number of mistakes = 1
Wrongly guessed letters = “E�
Letters available = “ABCDFGHIJKLNOPQRSTUVWXYZ�
And so on until you successfully guess all the letters in the word, or until you make nine
mistakes.
You are required to do the following:
• First, design and create a class called GuessWord to represent a game. (If you freeze the
game at a point in time and observe its state, then what do you see? What would constitute
the state of the game? How would the game change its state?)
• Second, provide a text-based interface program called GuessWordTest that will allow the
user to play the game once. The text-based interface program should use the class you create
in step 1. The interface will ask for a word to guess (that is entered at the keyboard), then
the dialog between the user and the program is similar to that described above.
And here is the coding i have done so far
Then my text based interface is
defined class (GuessWord) compiles with no error, so i'am no sure wat is wrong PLEASE ANYBODY HELP THIS NOOB OUT, i appreciate anyone who even bothered read my problem
Problem 3 – The Guess-a-Word Game (15% implementation, 10%
design, code, layout and comments)
The guess-a-word game, as the name suggests, is a game of guessing words. You will be playing
this game with the computer in charge of the game. Suppose the word you have to guess is:
PROGRAMMING
The computer would start by displaying the word with each letter replaced by an asterisk:
***********
Number of mistakes = 0
Wrongly guessed letters = “�
Letters available = “ABCDEFGHIJKLMNOPQRSTUVWXYZ�
That display means that so far you have not made any mistakes and the set of wrongly guessed
letters is empty.
You are then asked to play a letter. Suppose you play letter “M�. The computer would display:
******MM***
Number of mistakes = 0
Wrongly guessed letters = “�
Letters available = “ABCDEFGHIJKLNOPQRSTUVWXYZ�
Next suppose you play letter “E� which is not one of the letters of the word. Then the computer
would display:
******MM***
Number of mistakes = 1
Wrongly guessed letters = “E�
Letters available = “ABCDFGHIJKLNOPQRSTUVWXYZ�
And so on until you successfully guess all the letters in the word, or until you make nine
mistakes.
You are required to do the following:
• First, design and create a class called GuessWord to represent a game. (If you freeze the
game at a point in time and observe its state, then what do you see? What would constitute
the state of the game? How would the game change its state?)
• Second, provide a text-based interface program called GuessWordTest that will allow the
user to play the game once. The text-based interface program should use the class you create
in step 1. The interface will ask for a word to guess (that is entered at the keyboard), then
the dialog between the user and the program is similar to that described above.
And here is the coding i have done so far
Java Syntax (Toggle Plain Text)
public class GuessWord { private String word; private char letterguessed; private int numberOfMistakes; private char wronglyGuessLetters; private String[] hiddenWord = new String[word.length()]; private char[] lettersAvalible = new char[26]; public GuessWord(String word, char letterguessed) { this.letterguessed = letterguessed; this.word = word; numberOfMistakes = 0; wronglyGuessLetters = (char)0; for( int i = 0; i < word.length(); i++) { hiddenWord[i] = "*"; } for( int j = 0; j < lettersAvalible.length; j++ ) { lettersAvalible[j] = (char)('A' + j); } } public void displayWord() { hiddenWord = new String[word.length()]; for ( int i = 0; i < hiddenWord.length; i++) { if ( letterguessed != word.charAt(i) ) { hiddenWord[i] = "*"; } else { hiddenWord[i] += (char)letterguessed; } } } public void checkGuess(int count) { for ( int i = 0; i <= word.length(); i++) { if ( letterguessed != word.charAt(i) ) { count++; } if ( count == word.length() ) { numberOfMistakes++; wronglyGuessLetters += (char)letterguessed; } } if( numberOfMistakes == 9 ) { System.out.println("\nGAME OVER"); } } public void lettersLeft() { for ( int i = 0; i < lettersAvalible.length; i++ ) { if ( letterguessed == lettersAvalible[i] ) { lettersAvalible[i] = (char)' '; } } } public String toString() { return( hiddenWord + "\nNumber of mistakes = " + numberOfMistakes + "\nWrongly guessed letters = " + wronglyGuessLetters + "\nLetters available = " + lettersAvalible); } }
Then my text based interface is
Java Syntax (Toggle Plain Text)
import java.util.*; public class GuessWordTest { public static void main(String[] args) { System.out.println("Please insert a word to be guessed"); Scanner keyboard = new Scanner(System.in); int i = 0; String word = keyboard.nextLine(); System.out.println("please guess a letter"); char letterguessed = (char)keyboard.nextLine().charAt(0); GuessWord test = new GuessWord(word, letterguessed); do { test.displayWord(); test.checkGuess(0); test.lettersLeft(); test.toString(); i++; System.out.println("Guess another letter"); }while( word != "programing"); System.out.print("GAME OVER, thanks for using!"); } }
defined class (GuessWord) compiles with no error, so i'am no sure wat is wrong PLEASE ANYBODY HELP THIS NOOB OUT, i appreciate anyone who even bothered read my problem
OK, as i was bored i had a quick look.
In GuessWord.java you have :
This is a instance variable which is created as the constructor executes but BEFORE any parameters have been initialised, so 'word' does not exist here. [null exception]
Then later you have :
If you have a String with 4 characters, the indexes to these characters start at 0 (so 0...3). [out-of-bounds exception]
These are two fatal errors.
Then in GuessWordTest you have :
Ehh, it would be nice if i could actually enter another letter.
Hope this points you in the right direction.
In GuessWord.java you have :
Java Syntax (Toggle Plain Text)
private String[] hiddenWord = new String[word.length()];
This is a instance variable which is created as the constructor executes but BEFORE any parameters have been initialised, so 'word' does not exist here. [null exception]
Then later you have :
Java Syntax (Toggle Plain Text)
for ( int i = 0; i <= word.length(); i++) { if ( letterguessed != word.charAt(i) ) { count++; } }
If you have a String with 4 characters, the indexes to these characters start at 0 (so 0...3). [out-of-bounds exception]
These are two fatal errors.
Then in GuessWordTest you have :
Java Syntax (Toggle Plain Text)
do { test.displayWord(); test.checkGuess(0); test.lettersLeft(); test.toString(); i++; System.out.println("Guess another letter"); } while( word != "programing");
Ehh, it would be nice if i could actually enter another letter.
Hope this points you in the right direction.
:?: Sometimes i wonder if i'm on the right planet :!:
•
•
Join Date: May 2006
Posts: 4
Reputation:
Solved Threads: 0
ok ive adjusted my code, just note that i made copys of the original files and renamed the filenames and classes.
And the other....
Im still having some NullPointerException errors with hiddenWord and within the second code im having trouble with the condition in my while loop comparing an char array with a String word, and ideas how to go about this? thx again
Java Syntax (Toggle Plain Text)
public class lele { private String word; private char letterguessed; private int numberOfMistakes; private char wronglyGuessLetters; private String[] hiddenWord; private char[] lettersAvalible = new char[26]; public lele(String word, char letterguessed, String[] hiddenWord) { this.letterguessed = letterguessed; this.word = word; numberOfMistakes = 0; wronglyGuessLetters = (char)0; hiddenWord = new String[word.length()]; for( int j = 0; j < lettersAvalible.length; j++ ) { lettersAvalible[j] = (char)('A' + j); } } public void displayWord() { for ( int i = 0; i < hiddenWord.length; i++) { if ( letterguessed != word.charAt(i) ) { hiddenWord[i] = "*"; } else { hiddenWord[i] += letterguessed; } } } public void checkGuess(int count) { for ( int i = 0; i < word.length(); i++) { if ( letterguessed != word.charAt(i) ) { count++; } if ( count == word.length() ) { numberOfMistakes++; wronglyGuessLetters += (char)letterguessed; } } if( numberOfMistakes == 9 ) { System.out.println("\nGAME OVER"); } } public void lettersLeft() { for ( int i = 0; i < lettersAvalible.length; i++ ) { if ( letterguessed == lettersAvalible[i] ) { lettersAvalible[i] = (char)' '; } } } public String toString() { return( hiddenWord + "\nNumber of mistakes = " + numberOfMistakes + "\nWrongly guessed letters = " + wronglyGuessLetters + "\nLetters available = " + lettersAvalible); } }
Java Syntax (Toggle Plain Text)
import java.util.*; public class lala { public static void main(String[] args) { System.out.println("Please insert a word to be guessed"); Scanner keyboard = new Scanner(System.in); String word = keyboard.nextLine(); System.out.println("please guess a letter"); char letterguessed = keyboard.next().charAt(0); int e = word.length(); int counter = 0; String[] hiddenWord = new String[e]; for(int z = 0; z < hiddenWord.length; z++) { hiddenWord[z] = "*"; } lele test = new lele(word, letterguessed, hiddenWord); do { test.displayWord(); test.checkGuess(0); test.lettersLeft(); test.toString(); letterguessed = keyboard.next().charAt(0); System.out.println("Guess another letter"); counter++; }while( hiddenWord[counter] != word); System.out.print("GAME OVER, thanks for using!"); } }
OK, lets see. Starting from the main method you input a word and a letter and then for some reason create a array of strings, word.length strings long, and each string is set to "*".
I expect you want an array of char with each char set to '*' for display purposes.
In the lele class you declare a hiddenword instance variable and also use hiddenword as a parameter. Makes it very tricky to follow which variable you are using.
for checking chars in a char array to a string it would look someting like :
[code]
String word = "String To Test";
char[] characters = new char[4];
characters[0] = 'a';
characters[1] = 'i';
characters[2] = 'e';
characters[3] = 'o';
for (int count = 0; count < characters.size; count++)
I expect you want an array of char with each char set to '*' for display purposes.
In the lele class you declare a hiddenword instance variable and also use hiddenword as a parameter. Makes it very tricky to follow which variable you are using.
for checking chars in a char array to a string it would look someting like :
[code]
String word = "String To Test";
char[] characters = new char[4];
characters[0] = 'a';
characters[1] = 'i';
characters[2] = 'e';
characters[3] = 'o';
for (int count = 0; count < characters.size; count++)
:?: Sometimes i wonder if i'm on the right planet :!:
Oops, wrong key, we continue :
Crude but works.
Have a try again.
Good luck.
Java Syntax (Toggle Plain Text)
for (int count = 0; count < characters.length; count++) { for (int c2 = 0; c2 < word.length(); c2++) { if (word.charAt(c2) == characters[count]) { System.out.println("Found"); } } }
Crude but works.
Have a try again.
Good luck.
:?: Sometimes i wonder if i'm on the right planet :!:
![]() |
Other Threads in the Java Forum
- Previous Thread: Debug code. Any thoughts?
- Next Thread: Java help
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






