0

I am making a quiz game similar to the logo quizzes and im trying to create the input style on which they apps have. They have random mixed up letters and the user has to find the letter which make up the correct answer. So far i have an array list that hold the buttons and the correct answers letters, then i loop through the array lists and assign the buttons text to the letters from the correct answers array list but causing a force stop with no errors. Anyone know why?

ArrayList<Character> answer = new ArrayList<Character>();
ArrayList<Button> buttons = new ArrayList<Button>();

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

    Button btn1 = (Button) findViewById(R.id.button1);
    Button btn2 = (Button) findViewById(R.id.button2);
    Button btn3 = (Button) findViewById(R.id.button3);
    Button btn4 = (Button) findViewById(R.id.button4);
    Button btn5 = (Button) findViewById(R.id.button5);
    Button btn6 = (Button) findViewById(R.id.button6);
    Button btn7 = (Button) findViewById(R.id.button7);
    Button btn8 = (Button) findViewById(R.id.button8);
    Button btn9 = (Button) findViewById(R.id.button9);
    buttons.add(btn1);
    buttons.add(btn2);
    buttons.add(btn3);
    buttons.add(btn4);
    buttons.add(btn5);
    buttons.add(btn6);
    buttons.add(btn7);
    buttons.add(btn8);
    buttons.add(btn9);

    String testAnswer = "Answer";
    //convert to chars and add to arraylist for shuffling
    for(char a : testAnswer.toCharArray()){
        answer.add(a);
    }
    Collections.shuffle(answer);


    //loop over buttons and letters and assign each button with a letter
    for(char a : answer){
        for(Button bb : buttons){
            bb.setText(String.valueOf(a));
        }
    }

}
6
  • 2
    Good luck with your app! So, ... where's your question? Commented Aug 1, 2014 at 16:01
  • Why do you want to set every button's text to only the last answer? (That's what your double for is doing.) Commented Aug 1, 2014 at 16:05
  • Yeah it is, what i want is to set each character from the answer array to an available button from the buttons array Commented Aug 1, 2014 at 16:07
  • Instead of your for (char a : testAnswer.toCharArray()) { answer.add(a); } you should write answer = new ArrayList<Character>(Arrays.asList(testAnswer.toCharArray()));. Commented Aug 1, 2014 at 16:07
  • But that wants me to change the String testAnswer to an ArrayList<Character> Commented Aug 1, 2014 at 16:12

3 Answers 3

1

I can't see why the app force stops from the code you have given (it seems fine), but I discovered another problem:

//loop over buttons and letters and assign each button with a letter
for(char a : answer){
    for(Button bb : buttons){
        bb.setText(String.valueOf(a));
    }
}

This code will not assign a different letter to every button, but assign the same letter to all buttons.
Instead do this:

// omit unnecessary answer.shuffle() (`answer` will be shuffled anyway afterwards)

String allowed = "abcdefghijklmnopqrstuvwxy"; // add all allowed characters here

for (int i = 0; i < answer.size(); i++)
    buttons.get(i).setText("" + answer.get(i));

Random r = new Random();
for (int j = answer.size(); i < buttons.size(); j++)
    buttons.get(j).setText("" + allowed.charAt(r.nextInt(0, allowed.length())); // get a random character from `allowed`

Collection.shuffle(buttons); // shuffle buttons, so that they are in random order

In case you don't want characters to occur more than once you can you remove them from allowed after setting them as a button text.

Sign up to request clarification or add additional context in comments.

3 Comments

They wont have the same size, each quiz question will have 20 buttons and the answer array will fill however many buttons and then the remaining buttons will be filled with other random characters, then the user has to try find the correct letters from the 20 buttons, guess the emoji, logo quiz etc has this feature if you have played any of them
Ok, the code I have given will assign the answer letters to the first i buttons in the list.
Yeah i just tested it works good thank you for the help, would there be a way where i could fill the remaining buttons with other random letters and have the buttons scramble up so the first x buttons arent always the correct answer
0

Well you could do the following:

Button btn;
for(int i = 0; i < answer.size(); i++){
   btn = buttons.get(Math.random() * (buttons.size()+1))
   if(btn.getText == null){
        btn.setText(String.valueOf(answer.get(i)));
   }
}

Then fill the remaining with random letters:

char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();

// char[] alphabet = [a-z] May also work ;)

for(Button b: buttons){
    if(b.getText == null){
        b.setText(String.valueOf(answer.get(Math.random() * (alphabet.size()+1)));
    }
}

Assuming this works, this should put a "answer" letter in a random button, then fills the other buttons with other letters from a-z.

P.S: This assumes that the buttons text is "null" by default, you could/should change this

1 Comment

Doesnt return anything now, i had to cast the buttons.get(Math.random(); to an int but buttons are blank
0
String[] Herifler={"q","ü","e","r","t","y","u","i","o","p","ö","ğ","a","s","d","F","G","H","J","K","L","ı","ə","Z","X","C","V","B","N","M","ç","ş"};

for(in i=0;i<30;i++) {
   buttons.get(i).setText(Herifler[i]);
}

Collections.shuffle(buttons);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.