HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Word Guessing Game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamewordguessing

Problem

I'm trying to learn Java (albeit rather slowly). I'm in a computer science class at my highschool and I've written this little bit of code for a project. Basically it is a word guessing game where if you get a letter correct it reveals it. If you get a letter correct in the wrong spot it marks it with a +, and if the letter is not present at all it marks it with a *.

I'm wondering if anyone can give me feedback as to how I can improve this code.

Here is the assignment page if I didn't do an adequate job of explaining what the goal was:

public class main {
    private int length;
    private String word;
    private String result;
    private String buffer;
    public main(String aword) {

        result = "";
        word = aword;
        length = word.length();
        buffer = "";

    }
    public String guess(String aguess) {
        for(int i = 0; i < length; i++) {
            buffer = aguess.substring(i, i+1);
            if(word.indexOf(buffer) == i)
                result = result + buffer;
            else if ((word.indexOf(buffer) != -1))
                result = result + "+";
            if ((word.indexOf(buffer) == -1)) 
                result = result + "*";
        }
        return result;

    }
}

Solution

-
There is an error in your code:

if(word.indexOf(buffer) == i)
        result = result + buffer;


Test it with word="aa" and aguess="aa".

-
your class should really have a single member variable, which is 'word'. The member "length" is not needed since it can be deduced by word, and represents a duplication of information (which, in general, is a bad thing). The variables 'result' and 'buffer' should be local to the function which uses them, since they make no sense outside that function.

-
as already said, you should stick with the names provided in the assignment.

Possible rewriting:

public class HiddenWord {
    private String word;

    public HiddenWord(String a_word) {
        word = a_word;
    }

    public String getHint(String a_guess) {
        string result = "";
        for(int i = 0; i < word.length(); i++) {
            if (word[i] == a_guess[i])
                result += word[i];
            else if (word.indexOf(a_guess[i]) != -1)
                result += "+";
            else
                result += "*";
        }
        return result;
    }
}

Code Snippets

if(word.indexOf(buffer) == i)
        result = result + buffer;
public class HiddenWord {
    private String word;

    public HiddenWord(String a_word) {
        word = a_word;
    }

    public String getHint(String a_guess) {
        string result = "";
        for(int i = 0; i < word.length(); i++) {
            if (word[i] == a_guess[i])
                result += word[i];
            else if (word.indexOf(a_guess[i]) != -1)
                result += "+";
            else
                result += "*";
        }
        return result;
    }
}

Context

StackExchange Code Review Q#90328, answer score: 4

Revisions (0)

No revisions yet.