首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java GUI MasterMind

Java GUI MasterMind
EN

Stack Overflow用户
提问于 2014-04-25 17:21:42
回答 2查看 559关注 0票数 1

我打算用Java做一个游戏,它是数字智囊团。

现在AI从1到6生成4个数字,然后用户必须猜测它。

现在我有20分钟的GUI界面,我正在为如何更好地处理这一点和布局的使用而苦苦挣扎。

但我的问题是:

如果随机数是1122,我如何将其与猜测进行比较,并根据用户的输入输出值。

例如1122,用户猜测1133,我如何输出RRWW r表示正确的数字和位置,W表示错误?

我没做太长时间的java,所以对我来说还是个新手。

EN

回答 2

Stack Overflow用户

发布于 2014-04-25 17:34:01

您可以先将int转换为一个字符串,然后再转换为char[],然后将每个值进行比较,并将它们放入一个boolean[]数组中,该数组可以判断正确与否,例如:

代码语言:javascript
复制
boolean[] howManyRight(int number, int guess, int length){
    char[] n = String.valueOf(number).toCharArray();
    char[] g = String.valueOf(guess).toCharArray();
    boolean[] rightAnswers = new boolean[length];        

    for(int i = 0; i < length; i++){
        boolean[i] = n[i] == g[i];
    }

    return rightAnswers;
}
票数 0
EN

Stack Overflow用户

发布于 2014-09-10 11:23:08

不知道你对Mastermind有多熟悉,但通常情况下,你的例子的结果就是RR。从技术上讲,完全错误的数字不需要讨论。相反,W可以表示代码中的数字,但在错误的位置。所以如果我们稍微改变你的例子(生成的代码仍然可以是1122,但是来自用户的猜测现在是1234),它将生成一个R W的结果(1个正确,1个在错误的地方)来实现如下所示:

代码语言:javascript
复制
public String compareGuessToCode(String guess,String code) {
    //Variable to store result to show the user
    String result = "";
    //Necessary variables for code comparison
    //These xxxCharCount variables are used to keep track of how many of each possible
    // character are used in the guess and the code.
    int[] guessCharCount = new int[6];
    int[] codeCharCount  = new int[6];
    //These variables are used for bookkeeping during the evaluation process
    int   guessCharVal   = 0;
    int   guessValPos    = 0;
    int   codeCharVal    = 0;
    int   codeValPos     = 0;
    //Variables to keep track of perfectly correct and wrong position characters
    int   r              = 0;
    int   w              = 0;
    //Set all positions in xxxCharCount to 0
    for (int i = 0; i < 6; i++) {
        guessCharCount[i] = 0;
        codeCharCount[i]  = 0;
    }
    //Analyze the code
    for (int i = 0; i < code.length(); i++) {
        codeCharVal = (int) code.charAt(i);
        codeValPos  = codeCharVal - 1;
        codeCharCount[codeValPos] += 1;
    }
    //Analyze the guess
    for (int i = 0; i < guess.length(); i++) {
        guessCharVal = (int) guess.charAt(i);
        guessValPos  = guessCharVal - 1;
        //Compare guess characters to code characters
        if (guess.charAt(i) == code.charAt(i)) {
            //Show the position as correct
            r++;
            //Increment the appropriate slot in the character counter
            guessCharCount[guessValPos]++;
        }
    }
    //We want to check if the any guess characters are in the code, but we also want
    //to ensure that we haven't already accounted for all occurrences of the
    //guessed character in the code.
    for (int i = 0; i < guess.length(); i++) {
        guessCharVal = (int) guess.charAt(i);
        if (code.contains(String.valueOf(guess.charAt(i))) && guessCharCount[guessValPos] < codeCharCount[guessValPos]) {
            //Show the character as present, but in the wrong spot
            w++;
            //Increment the appropriate slot in the character counter
            guessCharCount[guessValPos]++;
        }
    }
    //Build the result string...perfect first...
    for (int i = 1; i <= p; i++) {
        if (result.isEmpty()) {
            result += "P";
        } else {
            result += " P";
        }
    }
    //...wrong spot last
    for (int i = 1; i <= p; i++) {
        if (result.isEmpty()) {
            result += "X";
        } else {
            result += " X";
        }
    }
    return result;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23289064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档