java:刽子手游戏重复字母

时间:2012-09-30 19:59:54

标签: java string stringbuffer chars

我有一个刽子手项目的逻辑问题,它需要用户写一封信,并搜索该字母是否包含在密码中。问题是我编程的方式,如果用户在秘密词中猜到了多次出现的字母。它只会通过并表示所有。这不是我想要的,我只希望它一次更新正确猜到的一个字母的状态。

我尝试了一些不同的东西,比如在status(guessCh,之后设置一个休息点,但迭代器只会转到字母匹配的第一个匹配位置并停在那里。

对此有什么简单的解决方法吗?

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().  
        }

    }

}

名词

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;
}

3 个答案:

答案 0 :(得分:2)

您可以使用比较方法中的状态变量来测试先前的解决方案。

    if (guessCh == secretCh && status.charAt(i) != secretCh)
    {
        status(guessCh, i);
        break;
    }

答案 1 :(得分:1)

从我能阅读(并理解)中,您面临的基本问题是由比较方法中的for循环引起的。

(注意,我的示例区分大小写,您需要考虑到这一点)

我可以提出两种基本方法......

第一种是,将所有事件与一次检查相匹配......

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;
}

哪会产生:

That guess is correct.
__i___________
Th_s is a test
That guess is correct.
__i__i________
Th_s _s a test

或者,替换第一次出现的猜测(根据我所理解的是你所追求的)

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;
    }
}

哪会产生这个......

That guess is correct.
__i___________
Th_s is a test

答案 2 :(得分:0)

我猜这是某种功课 - 但无论如何,为什么不使用indexOf