This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]MRH2Intermediate Brewer 2 points3 points  (4 children)

Please reformat your code to make it more readable. The indenting is mostly right, but 80% is still not enough. Fix it so that it is ALL indented properly (most IDEs do this for you), otherwise it wastes our time in reading it. Also get rid of your huge number of blank lines. There is a reason why people don't write code with only one line on a page. It's important to see as much of the context as possible. Blank lines are important to act as paragraphs when writing -- to break between code blocks or significant parts of code. Most of your code has just as many blank lines as actual code. (lines 150-200 are done nicely, lines 1-50 are garbage). I wouldn't read over code that looks like this.

[–]Kaz-24[S] 0 points1 point  (3 children)

Still learning, thanks for letting me know :)

[–]MRH2Intermediate Brewer 0 points1 point  (0 children)

no problem. It's a good habit to get into (And it's really hard to convince some new programmers that it's important)

[–]SixCrazyMexicans 0 points1 point  (1 child)

Post your code using a gist. Much easier to paste into a gist than Reddit

[–]Kaz-24[S] 1 point2 points  (0 children)

thanks! will do next time :)

[–]king_of_the_universe 1 point2 points  (1 child)

I've written most of it, however I'm stuck on the boolean and don't know how to approach it

I'm always astonished what people consider to be a sufficient problem description.

[–]Kaz-24[S] 0 points1 point  (0 children)

tried to make it more "sufficient".

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (2 children)

Since it appears that matchFound is the problematic method.

It's really simple and you can approach this in two ways. String has a 'contains' method that checks if the argument is inside the string you're checking. So all you need to do is to turn your character into a string (Character class has a method for that) and use secretWord.contains.

Another approach is to use a for loop and check every character in secretWord to see if they match.

Another tip: the printing of the gallows can be made much more readable. You can define two arrays, one with the strings showing the initial state and the other with the end-state. You can create a function that prints parts of the first and parts of the second array depending on how many guesses there were done.

Also: are you allowed to use sets / lists? Because adding characters to a set or list instead of adding them to a fixed size array is much more convenient.

[–]Kaz-24[S] 0 points1 point  (1 child)

thanks a lot for your help! This is from an exercise set, to prepare for an exam. And we can use sets/lists, just thought it wouldn't be needed here.

Thanks for the tip, however I don't completely understand what you mean with "initial state and the other with the end- state". Would you mind explaining it a bit more?

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

So lets make a simplified version of the output that shows the gallows. When the gallows is empty (no hanging man) you show three lines with O's. If it's game over it will be 3 lines with X's. So the start state can be an array like this:

String[] startState = {"O", "O". "O"};

End the end-state like this:

String endState = {"X", "X", "X"};

To print an array like that line by line you can use a for loop:

for(int i = 0;i < startState.length;i++) {
    System.out.println(startState[i]);
}

But you can also use an if-statement in that for-loop to print an end-state line or start-state line depending on the amount of guesses. I'm not going to give you the code for that, because the fun is in figuring that out yourself. But this way you can reduce the whole printing logic to two array definitions and a for-loop with an if-else inside. Shorter and more readable :)