Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 27th, 2006, 9:38 AM   #1
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
please i need some help

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
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

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 ELHEK; May 27th, 2006 at 9:58 AM.
ELHEK is offline   Reply With Quote
Old May 27th, 2006, 9:53 AM   #2
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
it would help if you wrapped the code around [code*] [/code*] tags (without the *'s)

tumbleTetris is offline   Reply With Quote
Old May 27th, 2006, 9:57 AM   #3
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
lol sorry bout that
ELHEK is offline   Reply With Quote
Old May 27th, 2006, 10:05 AM   #4
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
I think you missed my point on two levels...

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);
    }
    
    
}

and

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!");
        
        
    }
}
indentations help make the code that much easier to read + you should look into gettin g an IDE that does that for ya
tumbleTetris is offline   Reply With Quote
Old May 27th, 2006, 10:10 AM   #5
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
well I just ran your code though a quick test.

I can tell you for sure the problem is in GuessWord.

As for the details of the problem... well... I can suggest that you should change those for loops to while loops/do while loops, also instead of using word.length(), you should look into creating a new int of word.length(), that is:
int wordLength = word.length();

I don't have much more experience than you with these things, though I think that may help with your null pointer exception problem

also, your logic here doesn't seem to make sense:
for ( int i = 0; i <= word.length(); i++) {
            
            if ( letterguessed != word.charAt(i) ) {
                count++;
            }
            
            if ( count == word.length() ) {
                numberOfMistakes++;
                wronglyGuessLetters += (char)letterguessed;
            }
so basically its if the letter guessed does not equal the letter at charAt(i) you increase the count, and this keeps on happening until count == word.length().

What if letterguessed does equal word.charAt(i)? It should all be handled within the same loop.

While loop, would be nice (just a preferance I have for them, not particularly fond of for loops)...
tumbleTetris is offline   Reply With Quote
Old May 27th, 2006, 10:16 PM   #6
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
ok ive adjusted my code, just note that i made copys of the original files and renamed the filenames and classes.

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....

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
ELHEK is offline   Reply With Quote
Old May 27th, 2006, 11:35 PM   #7
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
well I dunno.

I think it would help if you start commenting your code, while you do that you'll get a better grip of what exactly it is doing and it may (should) help you find whats wrong.

You do need to comment the code in the assigment anyhow.
tumbleTetris is offline   Reply With Quote
Old May 28th, 2006, 12:50 AM   #8
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 595
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
let me take a look and get back to you in a little while(could be minutes or hours possibly days but i'll try and help)
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old May 28th, 2006, 1:01 AM   #9
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
well it better be, for ELHEK's sake, within the day as the assigment is due tomorrow
tumbleTetris is offline   Reply With Quote
Old May 28th, 2006, 2:35 AM   #10
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 595
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
i tried everything i could think of... even re writing the program and nothing worked... sorry
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:27 AM.

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