I was coding a segment where two characters play a dice game. They have a book which contains questions corresponding to dice numbers, and they must answer the question they get.
BUT
A question can obviously not be repeated
(I haven’t written the *set donex true command in case there is a better way)
I’m not really sure what dice you’re using or how you’re combining them, so for this example I just used two 6-sided dice.
*comment Initialize temporary flags for each question (2 through 12)
*temp q2 true
*temp q3 true
*temp q4 true
*temp q5 true
*temp q6 true
*temp q7 true
*temp q8 true
*temp q9 true
*temp q10 true
*temp q11 true
*temp q12 true
*comment Dice values and control variables
*temp die1 0
*temp die2 0
*temp total 0
*temp turns 0
*label game_loop
*comment Roll two 6-sided dice
*rand die1 1 6
*rand die2 1 6
*comment Calculate the total of the two dice
*set total (die1 + die2)
*comment Show the dice results to the player
You rolled a ${die1} and a ${die2}, for a total of ${total}.
*comment Check if the corresponding question is still available; if so, mark it used and go to that question
*if (total = 2) and q2
*set q2 false
*set question_asked true
*goto question2
*elseif (total = 3) and q3
*set q3 false
*set question_asked true
*goto question3
*elseif (total = 4) and q4
*set q4 false
*set question_asked true
*goto question4
*elseif (total = 5) and q5
*set q5 false
*set question_asked true
*goto question5
*elseif (total = 6) and q6
*set q6 false
*set question_asked true
*goto question6
*elseif (total = 7) and q7
*set q7 false
*set question_asked true
*goto question7
*elseif (total = 8) and q8
*set q8 false
*set question_asked true
*goto question8
*elseif (total = 9) and q9
*set q9 false
*set question_asked true
*goto question9
*elseif (total = 10) and q10
*set q10 false
*set question_asked true
*goto question10
*elseif (total = 11) and q11
*set q11 false
*set question_asked true
*goto question11
*elseif (total = 12) and q12
*set q12 false
*set question_asked true
*goto question12
*else
*comment If the question was already used, reroll
That question was already asked, so you reroll.
*goto game_loop
*comment Question labels follow; each asks a different question, then proceeds to end_turn
*label question2
"Question 2: What is your earliest memory?"
*goto end_turn
*label question3
"Question 3: What’s a talent you wish you had?"
*goto end_turn
*label question4
"Question 4: If you could travel anywhere, where would you go?"
*goto end_turn
*label question5
"Question 5: What’s your favorite time of day?"
*goto end_turn
*label question6
"Question 6: Have you ever had a supernatural experience?"
*goto end_turn
*label question7
"Question 7: What’s a book or movie that changed your perspective?"
*goto end_turn
*label question8
"Question 8: Describe a perfect weekend."
*goto end_turn
*label question9
"Question 9: What’s a habit you’d like to break?"
*goto end_turn
*label question10
"Question 10: Who do you admire most?"
*goto end_turn
*label question11
"Question 11: What’s something you’ve always wanted to try?"
*goto end_turn
*label question12
"Question 12: What would you do if you won a million dollars?"
*goto end_turn
*label end_turn
*comment Increment the turn counter
*set turns +1
*comment End game after 11 unique questions (maximum possible with 2d6)
*if (turns = 11)
All questions have been asked. The game is over!
*finish
*else
*comment Otherwise, continue to the next turn
*page_break Next Turn
*goto game_loop
The first dice stands for set number, and second for question number. Suppose first dice rolls 2 and second rolls 6, so you have the 6th question of the 2nd set (because each set has a theme).
I get more combinations this way (36) instead of just 12.
The dice are supposed to select a set number and a question number. (it first checks if the question has already been used or not, and rerolls if it has been)
Suppose you roll first die, it gives 5
You enter the fifth set
And then you roll second die, it gives 4
The 4th question is now selected (and eliminated).
Well the process will be the same, more or less, just with 36 choices. You could have it set up so that only that second die gets rerolled if there are open options in the set still though, I suppose.
*create dice1 0
*create dice2 0
*create index 0
*create questions_asked 0
*create_array question_done 36 false
Welcome to the Dice Game!
*label redice
*if (questions_asked = 36)
"All questions have been answered! Thanks for playing."
*finish
Press to roll the dice.
*choice
#Roll dice
*rand dice1 1 6
*rand dice2 1 6
You rolled a ${dice1} and a ${dice2}.
*set index ((dice1 - 1) * 6) + dice2
*if question_done[index]
You've already answered question ${index}. Rolling again...
*goto redice
*else
*set question_done[index] true
*set questions_asked +1
*goto question_select
*label question_select
*gosub_scene questions question_${index}
*goto redice
and another scene containing the questions
*label question_1
Question 1: What is your earliest memory?
*return
*label question_2
Question 2: What's a talent you wish you had?
*return
*label question_3
Question 3: What is your favorite book and why?
*return
*label question_4
Question 4: What was your dream job as a kid?
*return
Create an array to save the results. The array has 66 slots, but you’ll use only 36. That’s okay.
Use the string concatenation syntax to “add” the two rolls together. You’re not adding them numerically, just aglutinating them, so 1 & 1 is 11 and not 2.
Use array notation to access that slot in the array: done[dice1 & dice2].
I would suggest that instead of rerolling, to go to the next available option. Otherwise, the more questions they make the more likely to get stuck in a rerolling loop. The last question for example as 1 in 36 chances (less than 3%) of coming out.
Hm… Could we not force it to keep rerolling till it has an option? If we remove the “We have done this. Reroll.” and redirect it to just before it rolls the dice, will it not keep repeating till it encounters a number that is not done? At least that’s how I think it should work.
They’re actually not. It’s just syntax sugar coating to create multiple variables with a prefix. But there’s no way to manipulate arrays natively or any type of property like “length”.
If you’re interested, in CSLIB there’s a module for working with ChoiceScript’s pseudo-arrays.
That’s insane! I was just trying to figure out how to do some string manipulation last month and had to do a weird workaround. I’ll definitely be digging deeper into that library!
I believe the solution was “it isn’t worth my time to reinvent the wheel just for a little one-off bit of narrative, so I’m going to figure out an easier way to accomplish something similar” haha
If I do not add the underscore in the variable names in the *if statements below line 18, it gives an error saying no such variable exists, but if I do add it, it doesn’t recognise it in the statement on line 11. If I don’t add it in line 11 and add it in the statements below 18, it starts repeating because the done being set to true isn’t the same as the done being checked in the *ifs below line 18 (???)