lundi 31 octobre 2016

Score not increasing after new random array variable is read (Java) - Android Studio

There's a game with 9 colored squared. A random one of those colors is displayed on the screen, in one of those 9 colors (e.g: 'orange' in green). The user scores a point when they tap the colored square which corresponds with the word, ignoring the color of the word (e.g if 'orange' in any color and orange square is tapped, one point is added to score). All these color strings are stored in an array (colorString[]). When the score reaches 10, I introduce new values to the colorString array in the onClick method of each button. The values are ciphered versions of each color string. The issue is, even when the right color boxed is tapped for the ciphered value, the score goes no higher than 10. The newer array values are not working. All is explained in the code below:

int score = 0;
Random colStr = new Random();
int decider = colStr.nextInt(9);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game3);


    final Button greenButton;
    greenButton = (Button) findViewById(R.id.greenButton);

    final Button yellowButton;
    yellowButton = (Button) findViewById(R.id.yellowButton);

    final Button blueButton;
    blueButton = (Button) findViewById(R.id.blueButton);

    final Button blackButton;
    blackButton = (Button) findViewById(R.id.blackButton);

    final Button orangeButton;
    orangeButton = (Button) findViewById(R.id.orangeButton);

    final Button brownButton;
    brownButton = (Button) findViewById(R.id.brownButton);

    final Button whiteButton;
    whiteButton = (Button) findViewById(R.id.whiteButton);

    final Button purpleButton;
    purpleButton = (Button) findViewById(R.id.purpleButton);

    final Button redButton;
    redButton = (Button) findViewById(R.id.redButton);


final Button loseStarter3;

    loseStarter3 = (Button) findViewById(R.id.Starter3);
    loseStarter3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            infoG3.setVisibility(View.GONE);
            loseStarter3.setVisibility(View.GONE);
            final TextView word = (TextView) findViewById(R.id.word);
            word.setVisibility(View.VISIBLE);
            greenButton.setVisibility(View.VISIBLE);
            purpleButton.setVisibility(View.VISIBLE);
            blueButton.setVisibility(View.VISIBLE);
            blackButton.setVisibility(View.VISIBLE);
            redButton.setVisibility(View.VISIBLE);
            whiteButton.setVisibility(View.VISIBLE);
            brownButton.setVisibility(View.VISIBLE);
            orangeButton.setVisibility(View.VISIBLE);
            yellowButton.setVisibility(View.VISIBLE);

                final String[] colorString = new String[9];
                colorString[0] = "yellow";
                colorString[1] = "red";
                colorString[2] = "green";
                colorString[3] = "black";
                colorString[4] = "white";
                colorString[5] = "purple";
                colorString[6] = "blue";
                colorString[7] = "brown";
                colorString[8] = "orange";
                word.setText(colorString[decider]);

                int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
                word.setTextColor(randomAndroidColor);

                yellowButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (word.getText() == colorString[0] || word.getText() == colorString[9] || word.getText() == colorString[10] || word.getText() == colorString[11]) {
                            score++;
                        }
                        Random colStr = new Random();
                        if (score<=9) {
                            int decider = colStr.nextInt(9);
                            final String[] colorString = new String[9];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            word.setText(colorString[decider]);
                        }
                        if (score>9) {
                            int decider = colStr.nextInt(27)+9;
                            final String[] colorString = new String[36];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            colorString[9] = "weyoll";
                            colorString[10] = "loyelw";
                            colorString[11] = "oelwyl";
                            colorString[12] = "erd";
                            colorString[13] = "der";
                            colorString[14] = "edr";
                            colorString[15] = "enrge";
                            colorString[16] = "regne";
                            colorString[17] = "nerge";
                            colorString[18] = "lcbka";
                            colorString[19] = "alkcb";
                            colorString[20] = "cbakl";
                            colorString[21] = "ihewt";
                            colorString[22] = "thewi";
                            colorString[23] = "ewthi";
                            colorString[24] = "relppu";
                            colorString[25] = "ulrpep";
                            colorString[26] = "leprpu";
                            colorString[27] = "ebul";
                            colorString[28] = "lbeu";
                            colorString[29] = "ulbe";
                            colorString[30] = "rbwno";
                            colorString[31] = "wobnr";
                            colorString[32] = "onwrb";
                            colorString[33] = "agonre";
                            colorString[34] = "negrao";
                            colorString[35] = "greaon";
                            word.setText(colorString[decider]);
                        }

                        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
                        word.setTextColor(randomAndroidColor);
                    }
                });

                redButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (word.getText() == colorString[1]) {
                            score++;
                        }
                        Random colStr = new Random();
                        if (score<=9) {
                            int decider = colStr.nextInt(9);
                            final String[] colorString = new String[9];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            word.setText(colorString[decider]);
                        }
                        if (score>9) {
                            int decider = colStr.nextInt(27) + 9;
                            final String[] colorString = new String[36];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            colorString[9] = "weyoll";
                            colorString[10] = "loyelw";
                            colorString[11] = "oelwyl";
                            colorString[12] = "erd";
                            colorString[13] = "der";
                            colorString[14] = "edr";
                            colorString[15] = "enrge";
                            colorString[16] = "regne";
                            colorString[17] = "nerge";
                            colorString[18] = "lcbka";
                            colorString[19] = "alkcb";
                            colorString[20] = "cbakl";
                            colorString[21] = "ihewt";
                            colorString[22] = "thewi";
                            colorString[23] = "ewthi";
                            colorString[24] = "relppu";
                            colorString[25] = "ulrpep";
                            colorString[26] = "leprpu";
                            colorString[27] = "ebul";
                            colorString[28] = "lbeu";
                            colorString[29] = "ulbe";
                            colorString[30] = "rbwno";
                            colorString[31] = "wobnr";
                            colorString[32] = "onwrb";
                            colorString[33] = "agonre";
                            colorString[34] = "negrao";
                            colorString[35] = "greaon";
                            word.setText(colorString[decider]);
                        }

                        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
                        word.setTextColor(randomAndroidColor);
                    }
                });

                greenButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (word.getText() == colorString[2]) {
                            score++;
                        }
                        Random colStr = new Random();
                        if (score<=9) {
                            int decider = colStr.nextInt(9);
                            final String[] colorString = new String[9];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            word.setText(colorString[decider]);
                        }
                        if (score>9) {
                            int decider = colStr.nextInt(27) + 9;
                            final String[] colorString = new String[36];
                            colorString[0] = "yellow";
                            colorString[1] = "red";
                            colorString[2] = "green";
                            colorString[3] = "black";
                            colorString[4] = "white";
                            colorString[5] = "purple";
                            colorString[6] = "blue";
                            colorString[7] = "brown";
                            colorString[8] = "orange";
                            colorString[9] = "weyoll";
                            colorString[10] = "loyelw";
                            colorString[11] = "oelwyl";
                            colorString[12] = "erd";
                            colorString[13] = "der";
                            colorString[14] = "edr";
                            colorString[15] = "enrge";
                            colorString[16] = "regne";
                            colorString[17] = "nerge";
                            colorString[18] = "lcbka";
                            colorString[19] = "alkcb";
                            colorString[20] = "cbakl";
                            colorString[21] = "ihewt";
                            colorString[22] = "thewi";
                            colorString[23] = "ewthi";
                            colorString[24] = "relppu";
                            colorString[25] = "ulrpep";
                            colorString[26] = "leprpu";
                            colorString[27] = "ebul";
                            colorString[28] = "lbeu";
                            colorString[29] = "ulbe";
                            colorString[30] = "rbwno";
                            colorString[31] = "wobnr";
                            colorString[32] = "onwrb";
                            colorString[33] = "agonre";
                            colorString[34] = "negrao";
                            colorString[35] = "greaon";
                            word.setText(colorString[decider]);
                        }

                        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
                        word.setTextColor(randomAndroidColor);
                    }
                });

                /*etc... for other buttons, same contents but different color name*/

I AM AWARE THAT I HAVE ONLY USED || OTHER ARRAY VALUES FOR YELLOW, THIS IS JUST AN EXAMPLE. I have also tried using individual if statements below the colorString[0] one to add scores when == to the other values, and else if. Neither worked, they just crashed the app. I've also tried different button, not that is should make a difference. I tried changing the order of where the if statement is, only to fail again. I've spent a while trying to resolve this, but have unfortunately not been able to resolve this.

Would appreciate it if someone could provide me with a fix, to ensure these other array values are accepted. If there is something I haven't made clear, all is shown in the code I have posted. Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire