0

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("");

4 Answers 4

1

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. E.g.

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.

1
  • thanks for the quick reply! i was already thinking about that, but im still at a loss on where im supposed to put these pieces of codes so that it will only increment or decrement once every guessing round.
    – cryzone
    Mar 8, 2012 at 18:02
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.

3
  • Thank you mr.pcalcao, but the player score works like this: whenever the user makes a guess, the userscore+10. And it works, i think, just as i expected it to work(not setting aside your answer though). And im sorry for the wrong logic. 'coz im just a 2nd year student and this is my 1st take at java =). And i don't know how to use the "Set<Integer> randomValues = new HashSet<Integer>(); ". can it also be like an array that will store 10 two-digit numbers and how can i check each element if the user's guess matches it?
    – cryzone
    Mar 8, 2012 at 18:16
  • Yes, I understand that, the big problem was just increasing and decreasing logic inside the for. Like other answers have hinted too, that needs to get off the for loop. If your array can have the same value more then once, then a Set won't work (a Set doesn't store duplicate values). In that case you'll need to count how many times the user was right inside the loop, and then apply the game logic outside.
    – pcalcao
    Mar 8, 2012 at 18:21
  • Wow. yes thanks for that. i completely missed out on that one. erm... can you teach me how to use the Set... i think that's a really good idea.
    – cryzone
    Mar 8, 2012 at 18:27
0

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;
}
4
  • @mr.pcalcao, can you please teach me how to implement the Set to my system?
    – cryzone
    Mar 9, 2012 at 0:44
  • If you just want a usable Set for storing a couple numbers? Simply use HashSet: docs.oracle.com/javase/1.5.0/docs/api/java/util/HashSet.html
    – donnyton
    Mar 9, 2012 at 10:26
  • @donnyton_i want to use set like a single dimension array that can store 10 uniquely random numbers that depends on a certain range(for example 1-20). how can i implement this?
    – cryzone
    Mar 9, 2012 at 16:28
  • I've already linked you to the API, so you should look there and learn about the Set class itself before just asking us for the code to do it. Otherwise, that sounds like something for a new question.
    – donnyton
    Mar 10, 2012 at 9:08
0

The way I did this, was to make an array and instatiate it to "123456789" then shuffle that array, then you can go through this now shuffled array with a for loop and pull out as many numbers as you need to make a new string as long and as unique ie 4 digit 5 digit unique random number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.