mardi 21 août 2018

Android "IF" and "Else" with arraylist

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.

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.

 [1]: https://i.stack.imgur.com/5H1Eh.jpg

Aucun commentaire:

Enregistrer un commentaire