Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: CB.. on Tue 16/03/2004 13:21:12

Title: random display of user imput
Post by: CB.. on Tue 16/03/2004 13:21:12
a neighbour has asked me to make a random "poetry" generator..
so i have a text box that he can type different sentences into..
and he can then store those sentences in a listbox...

he wants to then be able to press a button and have a random sentence displayed by the "game"

i have got it to work but the code is getting a bit clunky as the number of entrys in the listbox increases
is there a short hand way to do this?

///////////////////////////////////this is what i have so far  ///////////get global in 3 is the button////

int notes;
notes = ListBoxGetNumItems(0,6);

if ((GetGlobalInt(3)==01) && (notes == 4)){
int ran=Random(3);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else ListBoxSetSelected(0,6,3);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 5)){
int ran=Random(4);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else ListBoxSetSelected(0,6,4);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 6)){
int ran=Random(5);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else ListBoxSetSelected(0,6,5);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 7)){
int ran=Random(6);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else ListBoxSetSelected(0,6,6);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 8)){
int ran=Random(7);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else ListBoxSetSelected(0,6,7);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 9)){
int ran=Random(8);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else ListBoxSetSelected(0,6,8);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 10)){
int ran=Random(9);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else if (ran==8) ListBoxSetSelected(0,6,8);
else ListBoxSetSelected(0,6,9);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 11)){
int ran=Random(10);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else if (ran==8) ListBoxSetSelected(0,6,8);
else if (ran==9) ListBoxSetSelected(0,6,9);
else ListBoxSetSelected(0,6,10);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 12)){
int ran=Random(11);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else if (ran==8) ListBoxSetSelected(0,6,8);
else if (ran==9) ListBoxSetSelected(0,6,9);
else if (ran==10) ListBoxSetSelected(0,6,10);
else ListBoxSetSelected(0,6,11);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 13)){
int ran=Random(12);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else if (ran==8) ListBoxSetSelected(0,6,8);
else if (ran==9) ListBoxSetSelected(0,6,9);
else if (ran==10) ListBoxSetSelected(0,6,10);
else if (ran==11) ListBoxSetSelected(0,6,11);
else ListBoxSetSelected(0,6,12);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}
else if ((GetGlobalInt(3)==01) && (notes == 14)){
int ran=Random(13);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else if (ran==5) ListBoxSetSelected(0,6,5);
else if (ran==6) ListBoxSetSelected(0,6,6);
else if (ran==7) ListBoxSetSelected(0,6,7);
else if (ran==8) ListBoxSetSelected(0,6,8);
else if (ran==9) ListBoxSetSelected(0,6,9);
else if (ran==10) ListBoxSetSelected(0,6,10);
else if (ran==11) ListBoxSetSelected(0,6,11);
else if (ran==12) ListBoxSetSelected(0,6,12);
else ListBoxSetSelected(0,6,13);
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}


///////it's going to get redicoulous as i get up towards 30 or so listbox entrys
Title: Re:random display of user imput
Post by: Radiant on Tue 16/03/2004 14:12:15
Well, you could replace


int ran=Random(5);
if (ran==0) ListBoxSetSelected(0,6,0);
else if (ran==1) ListBoxSetSelected(0,6,1);
else if (ran==2) ListBoxSetSelected(0,6,2);
else if (ran==3) ListBoxSetSelected(0,6,3);
else if (ran==4) ListBoxSetSelected(0,6,4);
else ListBoxSetSelected(0,6,5);


with

ListBoxSeSelected (0, 6, Random (5));

Title: Re:random display of user imput
Post by: CB.. on Tue 16/03/2004 16:46:41
wow !! nice one ;D could you explain that a little for me?
that certainly looks like a real improvement!!
(just so i can learn a little from it)
Title: Re:random display of user imput
Post by: Radiant on Tue 16/03/2004 17:41:50
Well, there's little to explain. It's just a basic concept of programming languages, and math.

If ran is a variable and is equal to 5, then we can use it in any expression and get the same result as if it were the number 5.
I.e. ran + 1 = 6, ran * 2 = 10 and so forth.
Similarly, SomeFunction (1, 2, 5) could be written as SomeFunction (ran - 4, (ran * 2) / 5, ran). Do the math.

Similarly, your list of

if ((GetGlobalInt(3)==01) && (notes == 4)){
int ran=Random(3);
...
if ((GetGlobalInt(3)==01) && (notes == 5)){
int ran=Random(4);
...
if ((GetGlobalInt(3)==01) && (notes == 6)){
int ran=Random(5);


can be concisely written as

if (GetGlobalInt(3)==01) {
int ran=Random(notes - 1);

Title: Re:random display of user imput
Post by: CB.. on Wed 17/03/2004 03:07:38
double WOW!!
boy have i got a lot to learn!!

many many thanks !!

yikes thats some condensation of a lot of code!!!
amazing stuff!
Title: Re:random display of user imput
Post by: Gilbert on Wed 17/03/2004 03:25:22
Hehe I'm just curious why you put 01 in you rcode instead of 1, that should make no difference.
Title: Re:random display of user imput
Post by: CB.. on Wed 17/03/2004 08:57:50
i do apologise it seemed like the thing to do.. ;D
Title: Re:random display of user imput
Post by: Gilbert on Wed 17/03/2004 09:11:03
Heh no need to apologise, the codes will work anyway, I was just curious. ;)
Title: Re:random display of user imput
Post by: CB.. on Wed 17/03/2004 10:39:01
ok cheers guys..
here we go....

int notes;
notes = ListBoxGetNumItems(0,6);
int ran;
ran = Random(notes - 1);

if (GetGlobalInt(3)==1) {
int ran=Random(notes - 1);
ListBoxSetSelected (0, 6, Random(ran));
string buffer;
ListBoxGetItemText (0,6,ListBoxGetSelected(0,6),buffer);Display ("%s",buffer);SetGlobalInt(3,00);}



i'm happy because ive learnt something ...my neighbour's happy because he can store his poetry and quickly and easily experiment with randomising  the phrases (if thats the correct term for lines of poetry? i dunno..)

excellent  many thanks!!!