0

We are trying to use this array of integers in other methods. Setting the final shuffled Array to a global variable has become next to impossible. We have set other variable as global. The goal here is to have a new int [] fix array every time a button is clicked. We have been able to generate a random int [] ar but can not utilize the array in other methods. So our questions after making the random int [] ar how can we use it in the onClickBtnOne method? Code with comments below

public class MainActivity extends AppCompatActivity {

Button btn1,btn2,btn3,btn4,btn5,btn6;
String T1,T2,T3,T4,T5,T6;

int test[] = new int[7];
int count = 0;
int v1,v2,v3,v4,v5,v6;
int[] fix = {3,2,1,4,6,5};
// Trying to not use above values


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

    btn1 = findViewById(R.id.btn1);
    btn2 = findViewById(R.id.btn2);
    btn3 = findViewById(R.id.btn3);
    btn4 = findViewById(R.id.btn4);
    btn5 = findViewById(R.id.btn5);
    btn6 = findViewById(R.id.btn6);

    main(null);
}
// end onCeate

public static void main(String args[]) {
    int [] fix = {1,2,3,4,5,6};

    shuffleArray(fix);
    // Want to USE this fix shuffleArray
    //==================================
    for (int i = 0; i < fix.length; i++) {
        System.out.print(fix[i] + ",");
    }
    System.out.println();
}
// Implementing Fisher–Yates shuffle
static void shuffleArray(int [] ar) {

    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = ThreadLocalRandom.current();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        int a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}

public void onClickBtnOne(View view){
    btn1.setBackgroundColor(getColor(R.color.color_Red));
    btn1.setEnabled(false);
    count = count + 1;
    v1 = count;
    test[v1] = count;

    if(fix[0] == test[v1]){
        // Need a global fix[] here
        // =========================
        T1 = "true";
        if(T1.matches("true")){
            btn1.setBackgroundColor(getColor(R.color.color_Yellow));
        }
    }else {
        T1 = "false";
    }
}
James_Duh
  • 1,321
  • 11
  • 31
  • may be? `shuffleArray(fix);` [This answer might help](https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – Dipak Poudel Dec 15 '17 at 05:26
  • @DipakPoudel That is the post where I stole the shuffle code from it just does what our code does it shuffles but trying to use the shuffled array is what is driving us crazy We are a 8th grade computer club kind of new at this array concept – James_Duh Dec 15 '17 at 05:32

1 Answers1

1

The array you are trying to use does not have an add method you need to put the values in from another variable like this ar[i] = a; So if you use this type of Array declaration List value = new ArrayList<>(); where you declared the other global variable life will be much easier. Modified code below

This will do the shuffle NOTICE value.clear() without this the List will grow each time it is initialized

    public void shf(View view){
    value.clear();
    for (int i = 1; i <= 6; i++) {
        value.add(i);
    }
    Collections.shuffle(value);
}

And here is your test method call value.get(index) Arrays are ZERO based

    public void on1(View view){

    btn1.setBackgroundColor(getColor(R.color.color_Red));
    btn1.setEnabled(false);

    if(value.get(0) == 1){
        T1 = "true";
        if(T1.matches("true")){
            btn1.setBackgroundColor(getColor(R.color.color_Yellow));
        }
    }else {
        T1 = "false";
    }
}
Vector
  • 3,066
  • 5
  • 27
  • 54
  • So our count Clicks will not work in the test method Need to rethink the test method – James_Duh Dec 15 '17 at 07:12
  • @James_Duh The chances of the value in the List Array matching the button clicks are about zero and the chance of matching the button value are even less out of 20 test they only matched twice Advice let the user enter a value and test that you know that the values are only 1 to 6 so the factorial of 6 is 720 still steep odds ? – Vector Dec 15 '17 at 07:19