-1

I am developing a game on Android studio.

the game is 12 blocks and 12 symbols (in this case "0" zeros). When you press the new game button, the first level is initiated. there are 12 levels in total. (curLevel). For a few seconds, one symbol will be displayed randomly on the screen, and disappear. (according to each level the number of inputs increases 1, for example, Level 1 : 1 symbol has to be guessed, Level 2: 2 symbols has to be guessed) until level 12.

Until this point, it is working with no problem.

Now I need to solve my code to receive the user's input, check if matches with the right block previews showed. If yes move to the next level(repeating the cycle of clearing the blocks, show random symbols, clearing again, waiting for the user's input) until level 12. If not show Fail message and Display new Game button again.

I created this Arraylist but it's not working properly, Right now the condition it's always negative(even if the right block its pressed) the "Fail! Try Again!" message it is displaying.

I ran the debugger and showed: List tempList = new ArrayList<> ();

tempList:size 12
for (int i = 0; i < curLevel; i++) { curLevel size:13
tempList.add ( buttons.get ( i ) ); buttons size:12
if (tempList.contains ( v.getTag () )) { tempList size 12
Its not matching the condition. –

here it is the values:

static int MAXLEVELS = 12;
        static String SUCCESS_SYMBOL = "0";
curLevel = 1;
        curGuess = 0;

Any suggestion to make it work ? or even make this code tidier?

private void buttonLogic(View v) {
        List<Button> tempList = new ArrayList<> ();
        for (int i = 0; i <= curLevel; i++) {
            if (i!= MAXLEVELS)
                tempList.add ( buttons.get ( i ) );
            else
                break;
        }
        if (tempList.contains ( v.getTag () )) {
            curGuess++;
            checkWin ();
        } else {
            lostGame ();
        }
    }

    private void lostGame () {
        Toast.makeText(this, "Fail! Try Again", Toast.LENGTH_SHORT).show();
        disableButtons();
        b_new.setVisibility(View.VISIBLE);
    }

        private void checkWin() {
            if (curGuess == curLevel) {
                disableButtons ();
                if (curLevel == 12) {
                    Toast.makeText ( this, "Congratulations! You Won", Toast.LENGTH_SHORT ).show ();
                    b_new.setVisibility ( View.VISIBLE );
                } else {
                    curGuess = 0;
                    curLevel++;
                    generateButtons ( curLevel );
                }
            }
        }

There is also an image from the previous code. Cheers.

Here is the image

  • debug your code, check what the tempList contains when you try to verify the contents, and check what exactly v.getTag() contains when you enter that value in the list – Stultuske Aug 22 '18 at 06:47
  • It's hard to tell especially since we don't know what `v.getTag()` will return or what `tempList` will actually contain. If this is your actual code then `tempList.contains ( v.getTag () )` should work if the data is set up correct (which we don't know). It would be best to step through your code with a debugger and especially check the contents of `tempList` and what you get from `v.getTag()` - then check what the `equals()` method of both objects is doing. – Thomas Aug 22 '18 at 06:47
  • I ran the debugger and showed: List – Wesley Souza Aug 22 '18 at 07:03
  • `if (tempList.contains ( v.getTag () ))` change this to `if (tempList.contains ( v))` – Krishna Sharma Aug 22 '18 at 07:09
  • Please do never provide more infos via comments. Always edit and update your question instead! – GhostCat Aug 22 '18 at 07:39

2 Answers2

0

use ArrayList<Button> tempList = new ArrayList<>(); instead, because else you'll lack methods (despite casting the variable from ArrayList to List does not cause any complaints).

the if condition might be (i+1) < MAXLEVELS, because i is 0 based and curLevel is 1 based.

the most simple might be to init curLevel = 0 and only display it as curLevel + 1... else you'll always have to consider that +1 / -1 offset in the business logic, which I'd consider as "useless complexity"... so that the game would start at level 0, while level 1 is being displayed on the GUI.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I was under the impression that generally when you create a collection object, you want to refer to that object with a variable of the corresponding collection interface type. In this case, using List – superPhreshHackerKid Aug 22 '18 at 08:37
0

Looks like you are trying to find any match in available buttons for the button on which click event has been performed.

To do that I would recommend you to find exact match by using viewId, you can change your method like below.

private void buttonLogic(View v) {
    boolean hasAnyMatch = false;
    // Assumption is curLevel will always be less than MAXLEVELS
    for (int i = 0; i <= curLevel; i++) {
        if (v.getId() == buttons.get(i).getId()) {
            hasAnyMatch = true;
            break;
        }
    }
    if (hasAnyMatch) {
        curGuess++;
        checkWin ();
    } else {
        lostGame ();
    }
}
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23