Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字母位置,为猜字游戏想出一个聪明的方法_Java - Fatal编程技术网

Java 字母位置,为猜字游戏想出一个聪明的方法

Java 字母位置,为猜字游戏想出一个聪明的方法,java,Java,我正在学习Java,在此过程中我正在编写一个猜字游戏。 现在我的问题是: 假设要猜测的单词是“行话”。假设“oodle”是你建议的一个词。 在处理猜测时,应输出以下内容: 正确字母:无 错误的位置字母:第一个位置为“o”,第四个位置为“l” 请注意,第二个“o”不应被称为位置错误的字母,因为在此之前我们已经报告了一个“o”。因此,如果您输入“ooooo”,只有最后一个字母应该高亮显示,因为它位于正确的位置,如果您输入“ooooz”,只有第一个字母应该高亮显示,而不是其他的“o” 我尝试过一些解决

我正在学习Java,在此过程中我正在编写一个猜字游戏。 现在我的问题是:

假设要猜测的单词是“行话”。假设“oodle”是你建议的一个词。 在处理猜测时,应输出以下内容: 正确字母:无 错误的位置字母:第一个位置为“o”,第四个位置为“l” 请注意,第二个“o”不应被称为位置错误的字母,因为在此之前我们已经报告了一个“o”。因此,如果您输入“ooooo”,只有最后一个字母应该高亮显示,因为它位于正确的位置,如果您输入“ooooz”,只有第一个字母应该高亮显示,而不是其他的“o”

我尝试过一些解决办法,但似乎都失败了。我知道有一种聪明/不那么复杂的方法可以做到这一点。有人能帮我吗

守则:

// / Indicates whether a letter has been accounted for
// / while highlighting letters in the guess that are not
// / in the correct position
private Boolean[][] marked = new Boolean[WordLength][5];

// / Holds which all letters have been solved so far
private Boolean[][] solved = new Boolean[WordLength][6];


public void CheckLetters() {

    for (int j = 0; j < currentAttempt; j++) {
        tempWord = list.get(j); //The guessed words

        for (int i = 0; i < WordLength; i++) {

            if (tempWord.charAt(i) == CurrentPuzzleWord.charAt(i)) {
                solved[i][j] = true; //CurrentPuzzleWord is the string with the hidden word

            } else if (CurrentPuzzleWord.indexOf(tempWord.charAt(i)) != -1) {
                marked[i][j] = true;
            }
        }
    }
///指示信件是否已记账
///在猜测中突出显示不正确的字母时
///在正确的位置
私有布尔值[][]标记=新布尔值[WordLength][5];
///保留到目前为止已解决的所有信件
私有布尔值[][]已解决=新布尔值[WordLength][6];
公开作废支票{
对于(int j=0;j
因此,您需要进行多次检查

String oracle = "lingo";
String input = "oodle";
String modOracle = "";
// ArrayList for noting the matched elements
ArrayList<Integer> match = new ArrayList<Integer>();
// ArrayList for the correct letters with wrong position
ArrayList<Integer> close = new ArrayList<Integer>();
// Length of the Strings of interest
int length = oracle.length;
字符串oracle=“lingo”; 字符串输入=“oodle”; 字符串modOracle=“”; //用于记录匹配元素的ArrayList ArrayList match=新的ArrayList(); //ArrayList用于位置错误的正确字母 ArrayList close=新建ArrayList(); //感兴趣字符串的长度 int length=oracle.length; 显然,您要检查的第一件事是是否有匹配且位置正确的字母。因此,取oracle字符串和用户输入字符串,逐个字符进行比较,注意正确的字母

// may need to check that oracle and input are same length if this isn't enforced.
for (int i = 0; i < length; i++) {
    if (input.substring(i,i+1).equals(oracle.substring(i,i+1))) {
        // there is a match of letter and position
        match.add(i);
    }
    else
        modOracle = modOracle + oracle.substring(i,i+1);
}
//如果不强制执行,可能需要检查oracle和input的长度是否相同。
for(int i=0;i
然后,您需要对正确但位置错误的字母进行第二次检查。为此,请首先从运行的上一次检查中取出正确的字母。然后,对于输入字符串中与oracle字符串中的字母匹配的每个字母,记下匹配项,并将其从其余检查中删除。继续此操作直到整个输入字符串都被检查过了

for (int i = 0; i < length; i++) {
    if (match.contains(i))
        continue;

    // String to match
    String toMatch = input.substring(i,i+1);

    for (int j = 0; j < modOracle.length; j++) {
        if (toMatch.equals(modOracle.substring(j,j+1))) {
            close.add(i);
            // then remove this letter from modOracle
            // need small utility method for this.
            break;
        }
    }
}
for(int i=0;i
将两个检查的结果合并并输出给用户


我不确定您想如何向用户显示结果,但现在您有了arraylist
match
,它对应于oracle/input中完全正确的位置,还有arraylist
close
,它对应于oracle/input中的位置,使字母出现在某个位置,但不在该位置

+1,说不出为什么有人扣了分。感谢你的帮助!我要试试这个!