简体   繁体   中英

Using an array in Random Number Guessing Game

i'd like to implement an achievement system to my random number guessing game but i failed to construct a valid logic for it. i'd like it to have an achievement system to detect a 2 or 3 consecutive CORRECT answers.

whenever there is a correct answer, cons+1(cons is the variable im using for the "consecutive" functionality of my achievement system.

otherwise, cons-1 and life-1.

here's my code below... i know for sure that the error in the logic is because the cons--; and the life--; is in the loop for SCANNING THE ARRAY for a match(the array is the one im using to store the 10 two-digit random numbers that will be the basis of the "correctness" of each answer. My purpose for adding the LOOP is that it will scan the array for a possible match.

NOTE: Thank you advance for your help!

here's my code:

public void cmpans()
{
    String txget;
    txget=gametext.getText();
    String pars;
    int ans;
    pars=gametext.getText();
    ans=Integer.parseInt(pars);

    for(int i=0; i<10; i++)  //this is my "Array Scanner" Loop
    {

        if(ans==arr[i])
        {
            userscore=userscore+10;
            lbscore.setText("Score: "+userscore);
            ck[i].setSelected(true);
            arr[i]=0000;
            cons++;
            gametext.setText("");
            lblives.setText("life: "+life);
            lbcons.setText("cons: "+cons);
        }
        else if(ans!=arr[i])
        {
            cons--;   //this is the cons and life im talking about
            life--;
            if(cons==2)
            {
                lbachieve.setText("You're a GOOD GUESSER!");
                userscore=userscore+20;
            }
            else if(cons==4)
            {
                lbachieve.setText("You're an AMAZING GUESSER!!");
                userscore=userscore+50;
            }
        }
    }gametext.setText("");

The first problem is, as you guessed, that you increment or decrement cons (and life ) for each match or mismatch in the array. You need to move that out of the loop, to do it only once per guessing round. Otherwise the game can never be won, because for any 10 different numbers and any guess, 9 of them will surely differ from the guess, thus you end up with a cons of -9 and possibly negative life (depending on how many lives you initially give) after the very first guess. Eg

boolean guessMatches = false;

for(int i=0; i<10 && !guessMatches; i++)  //this is my "Array Scanner" Loop
{
    guessMatches = (ans==arr[i]);
}
if(guessMatches)
{
    userscore=userscore+10;
    lbscore.setText("Score: "+userscore);
    ck[i].setSelected(true);
    arr[i]=0000;
    cons++;
    gametext.setText("");
    lblives.setText("life: "+life);
    lbcons.setText("cons: "+cons);
    if(cons==2)
    {
        lbachieve.setText("You're a GOOD GUESSER!");
        userscore=userscore+20;
    }
    else if(cons==4)
    {
        lbachieve.setText("You're an AMAZING GUESSER!!");
        userscore=userscore+50;
    }
}
else if(ans!=arr[i])
{
    cons = 0;   //this is the cons and life im talking about
    life--;
}

Note also that after a guess is wrong, you should set cons to 0, rather than just decrement it. Otherwise you aren't actually counting consecutive matches. I modified the code sample above accordingly.

Furthermore, as @pcalcao noted, you can make finding matches much simpler by using an appropriate collection (a Set ) instead of an array.

我这样做的方法是创建一个数组并将其设置为“ 123456789”,然后将其改组,然后可以使用for循环遍历此已改组的数组,并根据需要提取尽可能多的数字以创建新字符串和唯一一样长,即4位5位唯一的随机数

I'd suggest you use a flag instead--this way, you don't get any duplicate ++ or -- operations. That is,

boolean success = false;

for(int i=0; i<10; i++)
{
    if(ans == arr[i])
    {
        ...
        success = true;
    }
}

if(success)
{
    cons++;
}
else
{
    cons = 0;
}

I believe your logic to be quite deeply flawed, if what I understood of your description is correct.

My assumption: The user gives his guess, and you see if that guess exists in the array.

First of all, you're decreasing the player score for each number that isn't equal to his guess, meaning, even if he does get one right, he'll have 9 wrong. Move that logic out of the for loop, just use a variable inside to check if he guessed or not, and then do your increasing and decreasing outside (once he has guessed, break out of the for).

Secondly, I don't think you want to decrease cons after a wrong guess... if you want consecutive, each wrong guess should set cons to 0 again (he has to start over).

Thirdly: You can simplify your logic greatly if you use a structure more suited for checking if a given value exists in that structure, like a Set, for instance:

Set<Integer> randomValues = new HashSet<Integer>();

Fill that set up, and then your logic would be reduced to:

if(randomValues.contains(ans)){
//do stuff
} else {
//do other stuff
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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