简体   繁体   English

数组有时无法按预期工作

[英]Array sometimes doesn't work as expected

I've got a game in development for Android. 我有一款Android开发游戏。 I've got most of it up and running, but have just noticed a bug, that I can't always replicate, which (as you know) makes it a nightmare to debug! 我已经完成了大部分工作,但是刚刚注意到一个错误,我不能总是复制,这(如你所知)使它成为调试的噩梦! I'm sure its a logic problem, but I've been staring at it for so long I can't see it. 我确定这是一个逻辑问题,但我一直盯着它看这么久我看不到它。 This is the code that sorts the arrays: 这是对数组进行排序的代码:

    // RANDOMISE UP SOME LETTERS //
    for(int i=0; i<MAX_LETTERS; i++)
    {
        int r = rand.nextInt(25);       

        // SAVE THIS LETTER //
        lettersToUse[i] = letters[r];           
    }       

    // NOW MAKE SURE WE COPY IN OUR WORD //
    for(int i=0; i<nameLen; i++)
    {
        int r = rand.nextInt(MAX_LETTERS);

        Letter tmpLetter = new Letter();
        tmpLetter = letters[lettersInName[i]];                      

        while(in_array(lastElementsUsed, r))
        {
            r = rand.nextInt(MAX_LETTERS);
        }

        lastElementsUsed[i] = r;            

        if(!in_array(lettersToUse, tmpLetter.getLetter()))
        {               
            lettersToUse[r] = tmpLetter;
        }
    }

I've got an array of 18 allowed letters (in the app). 我有一组18个允许的字母(在应用程序中)。 The first loop (obviously) just picks a load of random letters. 第一个循环(显然)只是挑选一堆随机字母。 The second loop, then ensures that at least one letter of the word to be guessed is included in the final 18 letters. 然后,第二个循环确保最后18个字母中包含要猜到的单词的至少一个字母。

The trouble is: Most of the time it works flawlessly. 问题是:大部分时间它完美无缺。 At seemingly random intervals, the final array will be missing a letter from the word to be guessed. 在看似随机的间隔中,最后一个数组将缺少要猜到的单词中的一个字母。 It's not always in the same place in the word either. 它也不总是在同一个地方。

Can anyone see a problem with my logic? 任何人都可以看到我的逻辑问题吗? I'm happy to give more information and code, if I've missed a vital piece of the puzzle! 如果我错过了一个重要的难题,我很乐意提供更多的信息和代码!

TIA. TIA。

This method seems a lot simpler: 这种方法似乎更简单:

for (int i=0; i<(MAX_LETTERS-wordLength); i++)
{
    int r = rand.nextInt(25);       

    // SAVE THIS RANDOM LETTER //
    lettersToUse[i] = letters[r];           
}   
for (int i=MAX_LETTERS-wordLength; i<MAX_LETTERS; i++)
{
    // SAVE THIS LETTER OF THE WORD //
    lettersToUse[i] = lettersInName[r]; //I think that lettersInName is the right variable name to use, hopefully you understand what I'm trying to do here           
}   
lettersToUse.shuffle() //or similar method for whatever array type you're using

Which then ensures you have your word plus the rest of the 18 are filled with random ones, and they've been shuffled 然后,这确保你有你的话加上其余18个充满随机的,他们已经洗牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM