Jump to content

counter counting correct replies


testgenord1
 Share

You are about to reply to a thread that has been inactive for 1143 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi again!
I've got a script counting correct replies from players in a quiz.

With each correct reply the name of the player is shouted to a counter (the script below).
The counter puts the names of the players on a list.
When a player has reached a certain number of correct replies (here 3),
he / she has won.

The script seems to work so far.
The only two things I could not figure out is:

1. When I want to be shown all entries on the list by llList2String, only one entry is shown, but not the others (llGetListLength shows more entries, though.).
2. I try to delete the name of the winning player in the end. But instead all player names are deleted.

Do you see how to change these two things?
Any help will be appreciated.

 // The main function was taken from "count occurrences in list; by Dora Gustafson, Studio Dora// v1.1 inline code"
 
list lpoints;
list lnames; 
integer ListenHandle;

default
{
    state_entry()
    {
        integer ListenHandle = llListen(-1121,"",NULL_KEY,"");
    }
    listen(integer channel,string name,key id,string message)
    {
        if(message) // The message is the name of the player.
        {
            lpoints = lpoints + [message]; // The name of a player with a correct answer is put on the list.
            lnames = [message]; // The name of the player
            integer i = 0;
            integer j = 0;
            integer k = llListFindList( lpoints, lnames); // The name of the player is searched for on the list.
            integer m = llGetListLength( lpoints );
            while ( k >= 0 && i < m )
            {
                i += k+1;
                k = llListFindList( llList2List( lpoints, i, -1), lnames);
                ++j;
                if(j == 3) // If the player has given 3 correct replies ...
                {
                    string swinner = llList2String(lnames,0);
                    llOwnerSay(swinner + " has won!");
                    llOwnerSay("list length: " + m); // <-- This shows the number of all player names on the list.
                    integer a;
                    for(a=0;a<m;++a)
                    {
                        llOwnerSay(llList2String(lpoints,a)); // <-- This only shows one player name, but not the others.
                        integer index = llListFindList(lpoints,swinner);
                        lpoints = llDeleteSubList(lpoints,index,index); // <-- This deletes all player names on the list.
                        llOwnerSay("new list length: " + llGetListLength(lpoints));
                    }
                }
            }
        }
    }
}
Edited by testgenord1
Link to comment
Share on other sites

25 minutes ago, testgenord1 said:

integer index = llListFindList(lpoints,swinner);

should be  

integer index = llListFindList(lpoints,[swinner]);

because the function is supposed to be looking for a sublist  within the target list.  I'm surprised that you don't get a compilation error when you write it as you did.

Now, I think the problem you're having is related to that. I suspect that if you put in a diagnostic llOwnerSay("k = " + (string) k);  statement, you will discover that k = -1.  Since that's the case, your last for loop will keep removing the last element in lpoints every time it loops around. (Actually, that's going to happen any time that index == llGetListLength(lpoints) - 1, even if you fix the definition of index )

There are other odd things in the script (like the unnecessary if(message) test in the listen event) , but I think that's the immediate issue.

Edited by Rolig Loon
typos, of course
  • Like 1
Link to comment
Share on other sites

Thank you very much for your quick reply, Rolig Loon!
I've learned a lot from it.

"K" produced the value 0 for me when I checked.
But it's true, there probably is a connection between removing the last element of the list and the complete removal loop.
The script certainly isn't the best.
In case you or anyone else have more ideas, you're welcome to comment further.

Thank you very much for your help, once again!

Link to comment
Share on other sites

a way to avoid coding a search loop to get the count of a word in a list is:

default
{
    state_entry()
    {
        list words = ["apple", "banana", "carrot", "apple", "banana", "apple"];

        string search = "apple";

        integer count = llGetListLength(words)
            - llGetListLength(llParseString2List(llDumpList2String(words, " "), [" ", search], []));
         // - length of the list excluding the search word
         
         llOwnerSay(search + " = " + (string)count);
    }
}

 

edit add for completeness

we can remove all occurrences of a word in a list with the same method

list words = llParseString2List(llDumpList2String(words, " "), [" ", search], []);

 

Edited by Mollymews
  • Like 3
Link to comment
Share on other sites

Thanks a million, Mollymews!
This script works really beautifully.
I couldn't have done it without your help.
I'm posting the updated quiz below. Maybe someone has some use for it later.
I've probably still left some flaws in the script, so everybody still feel free to comment further.
Again, thank you so much for your help!🙂

list lpoints;
list lnames;
integer length;
integer ListenHandle;
integer inumber = 1; //number of expected correct answers
string sname;

default
{
    state_entry()
    {
        ListenHandle = llListen(-1121,"",NULL_KEY,"");
    }
    listen(integer channel,string name,key id,string message)
    {
        llSensorRepeat("","",AGENT_BY_LEGACY_NAME,10.0,PI,10.0);
        lpoints = lpoints + [message]; // The key of a player with a correct answer is put on the list.
        lnames = [message]; // The key of the player
        length = llGetListLength(lpoints);
        string search = message;
        integer count = length - llGetListLength(llParseString2List(llDumpList2String(lpoints, " "), [" ", search], []));
        if(count == inumber)
        {
            sname = llKey2Name(search);
            llRegionSayTo(search,0,"\n \n \nCongratulations!\n \n" + sname + " has won the game!\n \n \n \n \n");
            lpoints = llParseString2List(llDumpList2String(lpoints, " "), [" ", search], []);
        }
    }
    no_sensor()
    {
        //llOwnerSay("No avatar around any more.");
        llResetScript();
    }
}

 

Edited by testgenord1
  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1143 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...