desperate plee for help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 4
Reputation: ELHEK is an unknown quantity at this point 
Solved Threads: 0
ELHEK ELHEK is offline Offline
Newbie Poster

desperate plee for help

 
0
  #1
May 27th, 2006
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
  1. public class GuessWord
  2. {
  3. private String word;
  4. private char letterguessed;
  5. private int numberOfMistakes;
  6. private char wronglyGuessLetters;
  7. private String[] hiddenWord = new String[word.length()];
  8. private char[] lettersAvalible = new char[26];
  9.  
  10.  
  11. public GuessWord(String word, char letterguessed)
  12.  
  13. {
  14. this.letterguessed = letterguessed;
  15. this.word = word;
  16. numberOfMistakes = 0;
  17. wronglyGuessLetters = (char)0;
  18. for( int i = 0; i < word.length(); i++)
  19. {
  20. hiddenWord[i] = "*";
  21. }
  22. for( int j = 0; j < lettersAvalible.length; j++ )
  23. {
  24. lettersAvalible[j] = (char)('A' + j);
  25. }
  26. }
  27.  
  28. public void displayWord()
  29.  
  30. {
  31. hiddenWord = new String[word.length()];
  32. for ( int i = 0; i < hiddenWord.length; i++)
  33. {
  34. if ( letterguessed != word.charAt(i) )
  35. {
  36. hiddenWord[i] = "*";
  37. }
  38. else
  39. {
  40. hiddenWord[i] += (char)letterguessed;
  41. }
  42.  
  43. }
  44. }
  45.  
  46. public void checkGuess(int count)
  47.  
  48. {
  49. for ( int i = 0; i <= word.length(); i++)
  50. {
  51.  
  52. if ( letterguessed != word.charAt(i) )
  53. {
  54. count++;
  55. }
  56.  
  57. if ( count == word.length() )
  58. {
  59. numberOfMistakes++;
  60. wronglyGuessLetters += (char)letterguessed;
  61. }
  62.  
  63. }
  64. if( numberOfMistakes == 9 )
  65. {
  66. System.out.println("\nGAME OVER");
  67. }
  68.  
  69. }
  70.  
  71. public void lettersLeft()
  72.  
  73. {
  74. for ( int i = 0; i < lettersAvalible.length; i++ )
  75. {
  76. if ( letterguessed == lettersAvalible[i] )
  77. {
  78. lettersAvalible[i] = (char)' ';
  79. }
  80.  
  81. }
  82. }
  83.  
  84.  
  85. public String toString()
  86.  
  87. {
  88. return( hiddenWord + "\nNumber of mistakes = " + numberOfMistakes + "\nWrongly guessed letters = "
  89. + wronglyGuessLetters + "\nLetters available = " + lettersAvalible);
  90. }
  91.  
  92.  
  93. }


Then my text based interface is

  1. import java.util.*;
  2. public class GuessWordTest
  3. {
  4. public static void main(String[] args)
  5. {
  6. System.out.println("Please insert a word to be guessed");
  7. Scanner keyboard = new Scanner(System.in);
  8. int i = 0;
  9. String word = keyboard.nextLine();
  10. System.out.println("please guess a letter");
  11. char letterguessed = (char)keyboard.nextLine().charAt(0);
  12. GuessWord test = new GuessWord(word, letterguessed);
  13. do
  14. {
  15. test.displayWord();
  16. test.checkGuess(0);
  17. test.lettersLeft();
  18. test.toString();
  19. i++;
  20. System.out.println("Guess another letter");
  21. }while( word != "programing");
  22. System.out.print("GAME OVER, thanks for using!");
  23.  
  24.  
  25. }
  26. }


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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: desperate plee for help

 
0
  #2
May 27th, 2006
OK, as i was bored i had a quick look.

In GuessWord.java you have :
  1. 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 :
  1. for ( int i = 0; i <= word.length(); i++)
  2. {
  3. if ( letterguessed != word.charAt(i) )
  4. {
  5. count++;
  6. }
  7. }

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 :
  1. do
  2. {
  3. test.displayWord();
  4. test.checkGuess(0);
  5. test.lettersLeft();
  6. test.toString();
  7. i++;
  8. System.out.println("Guess another letter");
  9. } 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 :!:
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: ELHEK is an unknown quantity at this point 
Solved Threads: 0
ELHEK ELHEK is offline Offline
Newbie Poster

Re: desperate plee for help

 
0
  #3
May 27th, 2006
K, thanks for the reply mate and help i appreciate it
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: ELHEK is an unknown quantity at this point 
Solved Threads: 0
ELHEK ELHEK is offline Offline
Newbie Poster

Re: desperate plee for help

 
0
  #4
May 27th, 2006
ok ive adjusted my code, just note that i made copys of the original files and renamed the filenames and classes.
  1. public class lele
  2. {
  3. private String word;
  4. private char letterguessed;
  5. private int numberOfMistakes;
  6. private char wronglyGuessLetters;
  7. private String[] hiddenWord;
  8. private char[] lettersAvalible = new char[26];
  9.  
  10. public lele(String word, char letterguessed, String[] hiddenWord)
  11. {
  12. this.letterguessed = letterguessed;
  13. this.word = word;
  14. numberOfMistakes = 0;
  15. wronglyGuessLetters = (char)0;
  16. hiddenWord = new String[word.length()];
  17. for( int j = 0; j < lettersAvalible.length; j++ )
  18. {
  19. lettersAvalible[j] = (char)('A' + j);
  20. }
  21. }
  22. public void displayWord()
  23. {
  24. for ( int i = 0; i < hiddenWord.length; i++)
  25. {
  26. if ( letterguessed != word.charAt(i) )
  27. {
  28. hiddenWord[i] = "*";
  29. }
  30. else
  31. {
  32. hiddenWord[i] += letterguessed;
  33. }
  34. }
  35. }
  36. public void checkGuess(int count)
  37.  
  38. {
  39. for ( int i = 0; i < word.length(); i++)
  40. {
  41. if ( letterguessed != word.charAt(i) )
  42. {
  43. count++;
  44. }
  45. if ( count == word.length() )
  46. {
  47. numberOfMistakes++;
  48. wronglyGuessLetters += (char)letterguessed;
  49. }
  50. }
  51. if( numberOfMistakes == 9 )
  52. {
  53. System.out.println("\nGAME OVER");
  54. }
  55. }
  56. public void lettersLeft()
  57. {
  58. for ( int i = 0; i < lettersAvalible.length; i++ )
  59. {
  60. if ( letterguessed == lettersAvalible[i] )
  61. {
  62. lettersAvalible[i] = (char)' ';
  63. }
  64. }
  65. }
  66.  
  67. public String toString()
  68. {
  69. return( hiddenWord + "\nNumber of mistakes = " + numberOfMistakes + "\nWrongly guessed letters = "
  70. + wronglyGuessLetters + "\nLetters available = " + lettersAvalible);
  71. }
  72. }
And the other....
  1. import java.util.*;
  2. public class lala
  3. {
  4. public static void main(String[] args)
  5. {
  6. System.out.println("Please insert a word to be guessed");
  7. Scanner keyboard = new Scanner(System.in);
  8. String word = keyboard.nextLine();
  9. System.out.println("please guess a letter");
  10. char letterguessed = keyboard.next().charAt(0);
  11. int e = word.length();
  12. int counter = 0;
  13. String[] hiddenWord = new String[e];
  14. for(int z = 0; z < hiddenWord.length; z++)
  15. {
  16. hiddenWord[z] = "*";
  17. }
  18. lele test = new lele(word, letterguessed, hiddenWord);
  19. do
  20. {
  21. test.displayWord();
  22. test.checkGuess(0);
  23. test.lettersLeft();
  24. test.toString();
  25. letterguessed = keyboard.next().charAt(0);
  26. System.out.println("Guess another letter");
  27. counter++;
  28. }while( hiddenWord[counter] != word);
  29. System.out.print("GAME OVER, thanks for using!");
  30.  
  31. }
  32. }
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: desperate plee for help

 
0
  #5
May 28th, 2006
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++)
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: desperate plee for help

 
0
  #6
May 28th, 2006
This is sooo simple, I can't understand how you're managing to fudge it up.

Hmm?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: desperate plee for help

 
0
  #7
May 28th, 2006
Oops, wrong key, we continue :
  1. for (int count = 0; count < characters.length; count++) {
  2. for (int c2 = 0; c2 < word.length(); c2++) {
  3. if (word.charAt(c2) == characters[count]) {
  4. System.out.println("Found");
  5. }
  6. }
  7. }

Crude but works.

Have a try again.
Good luck.
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: desperate plee for help

 
0
  #8
May 28th, 2006
Were not all guru's. We all had to learn.
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: desperate plee for help

 
0
  #9
May 28th, 2006
>Were not all guru's. We all had to learn.

Lies...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: desperate plee for help

 
0
  #10
May 29th, 2006
Sttt. Don't let them in on the secret. 8-)
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC