5 Replies - 2081 Views - Last Post: 28 June 2011 - 02:37 PM Rate Topic: -----

#1 ramlapam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-June 11

Hangman problem

Posted 28 June 2011 - 12:39 PM

Hey everyone,

I'm fairly new to Java, and I'm taking an intro course this summer. I have a exercise that requires me to create a class to play a game of Hangman. The exact question is as follows:

Quote

Consider a class that could be used to play a game of hangman. The class has the
following attributes:
a. The secret word
b. The disguised word, in which each unknown letter in the secret word is
replaced with a question mark (?). For example, if the secret word is
abracadabra and the letters a, b and e have been guessed, the disguised
word would be ab?a?a?a?ab?a.
c. The number of guesses made.
d. The number of incorrect guesses.
It will have the following methods:
makeGuess© guesses that character c is in the word.
getDisguisedWord returns a String containing correctly guessed letters in
the correct positions and unknown letters replaced with ?.
getSecretWord returns the secret word.
getGuessCount returns the number of guesses made.
isFound returns true if the hidden word has been discovered.
Appropriate constructor and get and set methods.


I'm aware how to make most of the classes and the demo file to test the program, but I'm not sure how to go about implementing a word to guess letters of, a string that displays ?s and how to change question marks into characters based on a user's input.

If anyone could guide me in the right direction I would be forever grateful!

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Hangman problem

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Hangman problem

Posted 28 June 2011 - 01:15 PM

You should have two fields, one being a String with the secret word, and the other being a StringBuilder where it starts with being ?'s.
When the user guess a character, you find all of the indexes of this character in the secret word, and change the indexes in your StringBuilder

The StringBuilder has a method setCharAt(int index, char ch) :)

This post has been edited by CasiOo: 28 June 2011 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 ramlapam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-June 11

Re: Hangman problem

Posted 28 June 2011 - 01:20 PM

View PostCasiOo, on 28 June 2011 - 01:15 PM, said:

You should have two fields, one being a String with the secret word, and the other being a StringBuilder where it starts with being ?'s.
When the user guess a character, you find all of the indexes of this character in the secret word, and change the indexes in your StringBuilder

The StringBuilder has a method setCharAt(int index, char ch) :)


Thanks for the response!

I have another question actually: I've never used StringBuilder before, should this be done in a separate class with the setCharAt method inside it?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Hangman problem

Posted 28 June 2011 - 01:23 PM

View Postramlapam, on 28 June 2011 - 01:20 PM, said:

View PostCasiOo, on 28 June 2011 - 01:15 PM, said:

You should have two fields, one being a String with the secret word, and the other being a StringBuilder where it starts with being ?'s.
When the user guess a character, you find all of the indexes of this character in the secret word, and change the indexes in your StringBuilder

The StringBuilder has a method setCharAt(int index, char ch) :)


Thanks for the response!

I have another question actually: I've never used StringBuilder before, should this be done in a separate class with the setCharAt method inside it?

Thanks!


StringBuilder is a class in Java SE, it is not something you have to make.
It should be in the same class, no need to extend StringBuilder (just like you won't extend String either) :) . The difference between a String and a StringBuilder is that StringBuilder is mutable and a String is immutable, which means you can't edit in a String, but in a StringBuilder you can.
Was This Post Helpful? 0
  • +
  • -

#5 ramlapam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-June 11

Re: Hangman problem

Posted 28 June 2011 - 02:27 PM

Thanks again for the response!

I think I'm just about ready to finalize my code, I just have but one more question.

How do I search for the indexes of a character in a StringBuilder and then take the values and place them in my setCharAt? I tried searching on google, but I'm not really sure how to word my query.


Thanks!!
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Hangman problem

Posted 28 June 2011 - 02:37 PM

View Postramlapam, on 28 June 2011 - 02:27 PM, said:

Thanks again for the response!

I think I'm just about ready to finalize my code, I just have but one more question.

How do I search for the indexes of a character in a StringBuilder and then take the values and place them in my setCharAt? I tried searching on google, but I'm not really sure how to word my query.


Thanks!!


You should be looking in the Java API under StringBuilder and String.

First you make a loop and which every occurrence of the character, and then you replace in your result/whatever you wanna name it.

private String secretWord;
private StringBuilder quessedWord;

//pre: secretWord.length == quessedWord.length
public void guess( char character ) {
	for (int i=0; i<secretWord.length; i++) //Loop through all of the characters
		if ( secretWord.charAt( i ) == character ) //Check if they are the same
			quessedWord.setCharAt( i, character ); //Replace the ?'s with the character
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1