Reputation: 2002
I´ve got a logic problem with a hangman project, it takes an letter from the user and searches if that letter is contained within the secret word. The problem is the way I´ve programmed it, if there are several occurrences of the letter the user guessed within the secret word. it will just go through and denote them all. Which is not what I want, I only want it to update the status of correctly guessed letter one at a time.
I tried some different stuff like setting a break after status(guessCh,
but then the iterator will just go to the first occurrence where the letters match and stop there.
any simple fix to this?
private void compare(String str)
{
guessCh = str.charAt(0);
char secretCh = '0';
for (int i = i2; i < secretWord.length(); i++) // Cuts the secret word into individual chars to process.
{
secretCh = secretWord.charAt(i);
// Compare the two strings.
if (guessCh == secretCh)
{
status(guessCh, i); // Sends the letter & placement to status().
}
}
}
n
private String status(char guessCh, int placement)
{
/* Update and return status. */
if (guessCh >='A' && guessCh <= 'Z')
{
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement,guessCh).toString();
println("That guess is correct.");
canvas.displayWord(status);
return status;
}
return status;
}
Upvotes: 0
Views: 1365
Reputation: 347324
From what I can read (and understand), the basic problem you're facing is caused by the for
loop in the compare method.
(Note, my examples are case sensitive, you will need to take that into account)
There are two basic approaches I can suggest...
The first is, match ALL occurrences with a single check...
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
// This is a copy of the secret word, this ensures that
// we always have a copy of the original.
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
while (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// We want to remove the "guess" from our check string
// so it doesn't cause a false positive in the future
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
Which would produce:
That guess is correct.
__i___________
Th_s is a test
That guess is correct.
__i__i________
Th_s _s a test
or, replace the first occurrence of the guess (which from what I understand is what you're after)
public class TestStringCompare {
public static void main(String[] args) {
new TestStringCompare();
}
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
if (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// Some where here you need to remove the "guess" character
// to ensure that it doesn't get repeated...
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
}
Which would produce this...
That guess is correct.
__i___________
Th_s is a test
Upvotes: 1
Reputation: 10250
You could test for a prior solution using your status variable from inside your compare method.
if (guessCh == secretCh && status.charAt(i) != secretCh)
{
status(guessCh, i);
break;
}
Upvotes: 2
Reputation: 25929
I am guessing this is homework of some sort - but anyway, why not use indexOf
Upvotes: 0