开发者

Stuck on my Java homework -- Hangman game with StringBuilder -- help?

开发者 https://www.devze.com 2023-01-25 14:08 出处:网络
*NOTE: I AM NOT ASKING YOU TO DO MY HOMEWORK. I am just stuck. I am 开发者_Python百科designing a Hangman class right now. Apparently, we need three StringBuilders (a) one to display hyphens: \"------

*NOTE: I AM NOT ASKING YOU TO DO MY HOMEWORK. I am just stuck.

I am 开发者_Python百科designing a Hangman class right now. Apparently, we need three StringBuilders (a) one to display hyphens: "--------" the length of the word, b) one to display correct letters guessed: "--a--e---", and finally c) another one that is essentially the opposite of b (guessed letters replaced by hyphens and unguessed letters revealed). The purpose of c) is to see if there are any matches during guessing.

My biggest problem is I can't find many practical StringBuilder examples on Google, namely my biggest issue is where can/should I instantiate the three StringBuilders in the Hangman class?

thanks...


I'm guessing here that you have a Hangman class which works as a model that does three things (relevant for this) which is:

  • Gives you a string with one - for each character in the word to guess
  • Gives you a string which shows the correctly guessed characters in the right position
  • Gives you a string which shows which characters have been used

These are all dependent on the state of the model which would be

  • the word
  • characters guessed

Based on that I'd say that you should have three methods that return Strings and in each of those methods you create a new StringBuilder instance. Building a string is separate from the state just to make it clear why I disagree with Computerish.

StringBuilder is a more efficient way to build up strings then just using concatenation, but it is easy to use. You start with creating an instance of it.

StringBuilder builder = new StringBuilder();

Then you build up the String by appending Strings or chars (or other things):

builder.append('-');
builder.append('w');

When you are done you construct a String instance from the StringBuilder:

String string = builder.toString();

and you end up with "-w" which is a rather boring example.


A short example:

StringBuilder helloWorldBuilder = new StringBuilder();
helloWorldBuilder.append("Hello");
helloWorldBuilder.append(" ");
helloWorldBuilder.append("World");
helloWorldBuilder.append("!");
String helloWorld = helloWorldBuilder.toString();

And yes, you can create as many StringBuilder objects as you like (assuming you have an unlimited heap, but practically spoken: "as many as you like" which is bigger then 3 :-) )


The purpose of a StringBuilder is to build a string right before it is consumed. They should be created fresh each time (this is good practice for the tougher problems that lie ahead). So instead of thinking about the stringbuilder class, think instead of how to build the string you need. Assuming you are storing the correct guesses in a char array, you can have three methods that build the string as needed. In one of those cases you shouldn't need a StringBuilder at all.

Your ToBlankFormat method could look like this:

public String toBlankFormat()
{
    char[] format = new char[answer.length()];
    Arrays.fill(format, '-');
    return new String(format);
}

Essentially, to use a StringBuilder all you do is append strings, characters, etc. and then call the ToString() method. An example would look like this:

public String example()
{
    StringBuilder builder = new StringBuilder("Hello"); // initial value

    if (addSpaces)
    {
        builder.append(" ");
    }

    builder.append("World");
    return builder.ToString();
}


The three StringBuilders should be instance variables of your class. You can instantiate them in your class constructor or you can instantiate them when you choose the word that is being guessed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号