Array quiz game help[SOLVED]

Discussion in 'Programming' started by Mr Awesome, Mar 15, 2019.

1. Mr AwesomeMember

Joined:
Dec 14, 2017
Posts:
80
Hello

I wanted to choose a random string (of which there are three) stored in an array to appear in one of three speech bubbles (obj_quiz_answer_A,obj_quiz_answer_B,obj_quiz_answer_C,) then, the next bubble will choose one of the remaining strings and the final bubble will have the last string. Without covering every stipulation, could anyone explain an easier way to do this?

This is the code I have so far in the draw event:
Code:
```if question=1{//Switch
draw_set_color(c_purple);
draw_set_font(fnt_score);
draw_text(obj_quiz_speech_bubble.x,obj_quiz_speech_bubble.y,quizarray[0,0])
draw_set_color(c_red);
draw_set_color(c_orange);
draw_set_color(c_green);
}
```

2. CloseRangeMember

Joined:
Jul 2, 2016
Posts:
693
well my suggestion is to keep it how you have it now, but randomize the order of the array instead.
Alternatively you can store a second array about what order to draw in.
So maybe have this code in the create event.
Code:
```var total_n = 3; // how many 'bubbles' you have
for(var i=0; i<total_n; i++) { // loop through all items
quizarray_order[i] = 0; // initalize the item in the array to some number
var set = false;
while(!set) { // keep doing all this until we find some number that hasn't been used yet
n = irandom(total_n); // set it to a random number
set = true;
for(var j=0; j<i; j++) // check back through the list to see if that number has already been used
if(quizarray_order[j] == n) set = false; // if the number is already in use, make 'set' false so we can try a new number
}
quizarray_order[i] = n; // once out of the loop, we know the number is available, so set it.
}```
that should in theory work.
That is a simple code that will make an array of 3 items and set each array index
Code:
```if question=1{//Switch
draw_set_color(c_purple);
draw_set_font(fnt_score);
draw_text(obj_quiz_speech_bubble.x,obj_quiz_speech_bubble.y,quizarray[0,0])
draw_set_color(c_red);
draw_set_color(c_orange);
draw_set_color(c_green);
}```

Mr Awesome likes this.
3. Mr AwesomeMember

Joined:
Dec 14, 2017
Posts:
80
That seems to work, although I'm not sure it is randomizing the positions as they all keep appearing in the same order (tested it four times).

Would this be an easy way to implement a second random display of a dfferent set of three answers when the first draw text command is also different [1,1]?

4. CloseRangeMember

Joined:
Jul 2, 2016
Posts:
693
do you call randomize() at the start of the game?
if you don't do that you'll never see a change between reruns of the game

EDIT:
you can take the code I have, and make it 2 dimensional and then just have to process repeat in another for loop to have a seperate random occurance for each set of choices

Mr Awesome likes this.
5. Mr AwesomeMember

Joined:
Dec 14, 2017
Posts:
80
No I haven't been calling that, althnough If you close GMS then reopen it to run the game, will the order also still be the same? Anyway thanks for your help, I've got enough to tinker with and make this work!