GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-May-2006, 03:41
ELHEK ELHEK is offline
New Member
 
Join Date: May 2006
Posts: 6
ELHEK is on a distinguished road

Any help would be appreciated


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

JAVA Code:
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 Code:
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!");


}
}




My 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
Last edited by LuciWiz : 27-May-2006 at 05:59. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 27-May-2006, 12:58
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Any help would be appreciated


Just because the program compiles fine, doesn't mean that it has no logic flaws...

So, what is the first errors that you are getting? (I already know, but this is to help you work through them [and learn!])
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 27-May-2006, 18:46
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Any help would be appreciated


I'll help a bit more... you are probably getting this after the first letter guess:
Quote:
Exception in thread "main" java.lang.NullPointerException
at GuessWord.<init>(GuessWord.java:7)
at GuessWordTest.main(GuessWordTest.java:16)
this is the java stack dump that can help you pin-point the problem.
Notice inside the () is: ( <filename>.java:<linenumber> )

(your line numbers may be different)
the first "at" after the exception shows that GuessWord failed during 'init' at line 7 in GuessWord.java ... and this was at a point called from GuessWordTest.java, in GuessWordTest.main, at line 16 in the file. [which happens to be in main()]

If you check main at line 16, you are calling a new GuessWord() object, but it failed during initialization.

This should help you resolve errors as they appear, but if not, reply back and include the stack dump(s).
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 27-May-2006, 19:27
ELHEK ELHEK is offline
New Member
 
Join Date: May 2006
Posts: 6
ELHEK is on a distinguished road

Re: Any help would be appreciated


ok ive adjusted my code, just note that i made copys of the original files and renamed the filenames and classes.

JAVA Code:
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);
     }
}

And the other....

JAVA Code:
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!");


   }
}

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
Last edited by cable_guy_67 : 27-May-2006 at 19:34. Reason: Changed [CODE] tags to [JAVA]
  #5  
Old 27-May-2006, 20:42
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Any help would be appreciated


What has occurred is that in lele's constructor, you created hiddenWord (be careful of using common names between classes, by the way) as a new String, but there is nothing in the String! (null)

No need to pass hiddenWord to the constructor of class lele. Move the loop that you have to fill hiddenWord with '*' to the contructor of lele just after where you create the new String for hiddenWord. After moving the loop, remove hiddenWord = new String[e] and the fill-loop from lala.

Start with that and see how it goes.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 27-May-2006 at 21:15.
  #6  
Old 27-May-2006, 20:57
ELHEK ELHEK is offline
New Member
 
Join Date: May 2006
Posts: 6
ELHEK is on a distinguished road

Re: Any help would be appreciated


k thank you dude, i really appreciate any help
  #7  
Old 27-May-2006, 21:17
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Any help would be appreciated


You must have caught me in-between edits, see post #5.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #8  
Old 27-May-2006, 21:24
ELHEK ELHEK is offline
New Member
 
Join Date: May 2006
Posts: 6
ELHEK is on a distinguished road

Re: Any help would be appreciated


OK this is wat ive changed but im still geting errors

JAVA Code:
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;
      for( int i = 0; i < hiddenWord.length; i++)
      {
         if ( letterguessed != word.charAt(i) )
         {
            hiddenWord[i] = "*";
         }
      }
      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);
   }


}

and....

JAVA Code:
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];
      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!");


   }
}


lol this thing is killing me
Last edited by cable_guy_67 : 28-May-2006 at 06:52. Reason: Please use [JAVA] code tags for your java code
  #9  
Old 27-May-2006, 22:09
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,233
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Any help would be appreciated


You didn't get my suggestion quite right, but I meant like this:
constructor changes (after // markers)...
JAVA Code:
public class lele
{
...
   public lele(String word, char letterguessed) // note change
   {
      this.letterguessed = letterguessed;
      this.word = word;
      numberOfMistakes = 0;
      wronglyGuessLetters = (char)0;
      hiddenWord = new String[word.length()]; // and here
      for( int i = 0; i < word.length(); i++)       // and here
      {
         if ( letterguessed != word.charAt(i) )
         {
            hiddenWord[i] = "*";
         }
      }
      for( int j = 0; j < lettersAvalible.length; j++ )
      {
         lettersAvalible[j] = (char)('A' + j);
      }
   }
...
}
and....

changes to main (after // markers)...
JAVA Code:
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);
//delete     int e = word.length(); 
      int counter = 0;
//delete      String[] hiddenWord = new String[e];
      lele test = new lele(word, letterguessed); // delete 3rd argument
      do
      {
         test.displayWord();
         test.checkGuess(0);
         test.lettersLeft();
         test.toString();
         letterguessed = keyboard.next().charAt(0);
         System.out.println("Guess another letter");
         counter++;
      }while( counter != word.length());                // condition change.
      System.out.print("GAME OVER, thanks for using!");
   }
}
Not sure if the condition change belongs just yet, but had to make a change because of the eliminations.

Try that and see how it goes.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #10  
Old 28-May-2006, 00:42
ELHEK ELHEK is offline
New Member
 
Join Date: May 2006
Posts: 6
ELHEK is on a distinguished road

Re: Any help would be appreciated


K ive made all those adjustments but my problem still lies within initialzing the array in the constructor.. i am confused how to do it:S
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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 with a question,any help would be greatly appreciated :) yrostran C Programming Language 7 11-May-2006 02:44
C++ Calculator Problem. Any help much appreciated. SpinDizzy C Programming Language 13 12-Jul-2005 13:35
probably a stupid mistake....lil help appreciated wbsquared03 C++ Forum 9 06-Dec-2004 15:43
Need help with a program if anyone can help it would be appreciated Krc784 C++ Forum 1 03-Nov-2004 20:55
Minor Problem with my program. Any help greatly appreciated. agentxx04 C Programming Language 6 24-Oct-2004 15:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:42.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.