4 Replies Latest reply on Apr 27, 2012 9:57 AM by Mister MUX

    Linear List

    IEatSandvich

      This is my script to sort a list of scores and consequently the names, hits, and bullets fired accordingly when a game is finished, however it does not. (updatescores custom handler is called at the end of the game). list[7] is where the resultant score is initially stored. Any help please?

       

      on startmovie

        gvName = "Player"

        if gvNameList = void then

          gvNameList = ["Player Name", "Player Name", "Player Name", "Player Name", "Player Name", "Player Name", "Player Name"]

          gvScoreList = [12, 4, 3, 2, 1, 0, 0]

          gvHitsList = [0, 0, 0, 0, 0, 0, 0]

          gvBulletsList = [0, 0, 0, 0, 0, 0, 0]

        end if

      end

       

       

      on updatescores

        gvNameList[7] = (gvName)

        gvScoreList[7] = (gvScore)

        gvHitsList[7] = (gvHits)

        gvBulletsList[7] = (gvTotalBullets)

       

        if gvScoreList[7] > gvScoreList[5] then

          gvScoreList.setAt(6, gvScoreList[5])

          gvNameList.setAt(6, gvNameList[5])

          gvBulletsList.setAt(6, gvBulletsList[5])

          gvHitsList.setAt(6, gvHitsList[5])

        else if gvScoreList[7] > gvScoreList[4] then

          gvScoreList.setAt(5, gvScoreList[4])

          gvNameList.setAt(5, gvNameList[4])

          gvBulletsList.setAt(5, gvBulletsList[4])

          gvHitsList.setAt(5, gvHitsList[4])

        else if gvScoreList[7] > gvScoreList[3] then

          gvScoreList.setAt(4, gvScoreList[3])

          gvNameList.setAt(4, gvNameList[3])

          gvBulletsList.setAt(4, gvBulletsList[3])

          gvHitsList.setAt(4, gvHitsList[3])

        else if gvScoreList[7] > gvScoreList[2] then

          gvScoreList.setAt(3, gvScoreList[2])

          gvNameList.setAt(3, gvNameList[2])

          gvBulletsList.setAt(3, gvBulletsList[2])

          gvHitsList.setAt(3, gvHitsList[2])

        else if gvScoreList[7] > gvScoreList[1] then

          gvScoreList.setAt(2, gvScoreList[1])

          gvNameList.setAt(2, gvNameList[1])

          gvBulletsList.setAt(2, gvBulletsList[1])

          gvHitsList.setAt(2, gvHitsList[1])

          gvScoreList.setAt(1, gvScoreList[7])

          gvNameList.setAt(1, gvNameList[7])

          gvBulletsList.setAt(1, gvBulletsList[7])

          gvHitsList.setAt(1, gvHitsList[7])

        end if

      end

        • 1. Re: Linear List
          Mister MUX Level 2

          My suggestion would be to organize your data a bit differently, which will make sorting the data much easier. Rather than 4 seperate linears lists, I would use a single linear list. The elements of that list would be a property list that contains all of the data (name, score, bullets & hits) for a single player. For example if there was only one player it wold look like this:

           

          [[#name: "Player A", #score: 12, #hits: 0, #bullets: 0]]

           

          If there were two players it would look like this:

           

          [[#name: "Player A", #score: 12, #hits: 0, #bullets: 0], [#name: "Player B", #score: 2, #hits: 6, #bullets: 0]]

           

          And so on...

           

          So here is a startMovie script that builds a sample data set

           

          ------------------------------------------------------------------------------------

          global gvGameData

           

          on startMovie

            -- build sample data set

            gvGameData = []

            gvGameData.add([#name:"Player A", #score:8, #hits:0, #bullets:0])

            gvGameData.add([#name:"Player B", #score:4, #hits:0, #bullets:0])

            gvGameData.add([#name:"Player C", #score:3, #hits:0, #bullets:0])

            gvGameData.add([#name:"Player D", #score:9, #hits:3, #bullets:0])

            gvGameData.add([#name:"Player E", #score:9, #hits:2, #bullets:13])

            gvGameData.add([#name:"Player F", #score:9, #hits:2, #bullets:6])

            gvGameData.add([#name:"Player G", #score:12, #hits:0, #bullets:0])

          end

          -------------------------------------------------------------------------------------

           

          Now you can make a handler that sorts the data. This one uses a standard "Bubble Sort" (Google it for more info). It also allows you to sort the list by mulitple properties. For example you can sort by score, and if two scores are equal, it can sort by hits. If two scores and hits are equal, it can sort by bullets. Here is the sort handler.

           

          ----------------------------------------------------------------------------------------

          on sortGameData gameData, sortByPropertyList

            repeat while sortByPropertyList.count > 0

              sortProperty = sortByPropertyList[sortByPropertyList.count]

              sortByPropertyList.deleteAt(sortByPropertyList.count)

              repeat with x = 1 to gameData.count

                repeat with y = 1 to gameData.count - 1

                  if gameData[y][sortProperty] < gameData[y+1][sortProperty] then

                    temp = gameData[y+1]

                    gameData[y+1] = gameData[y]

                    gameData[y] = temp

                  end if

                end repeat

              end repeat

            end repeat

          end

          ----------------------------------------------------------------------------------------

           

          Here is an example usage:

           

          Presorted list

           

          [[#name: "Player A", #score: 8, #hits: 0, #bullets: 0], [#name: "Player B", #score: 4, #hits: 0, #bullets: 0], [#name: "Player C", #score: 3, #hits: 0, #bullets: 0], [#name: "Player D", #score: 9, #hits: 3, #bullets: 0], [#name: "Player E", #score: 9, #hits: 2, #bullets: 13], [#name: "Player F", #score: 9, #hits: 2, #bullets: 6], [#name: "Player G", #score: 12, #hits: 0, #bullets: 0]]

           

          Call the sort handler:

           

          sortGameData(gvGameData,[#score,#hits,#bullets])

           

          And now the list looks like this:

           

          [[#name: "Player G", #score: 12, #hits: 0, #bullets: 0], [#name: "Player D", #score: 9, #hits: 3, #bullets: 0], [#name: "Player E", #score: 9, #hits: 2, #bullets: 13], [#name: "Player F", #score: 9, #hits: 2, #bullets: 6], [#name: "Player A", #score: 8, #hits: 0, #bullets: 0], [#name: "Player B", #score: 4, #hits: 0, #bullets: 0], [#name: "Player C", #score: 3, #hits: 0, #bullets: 0]]

           

          Notice how it used multiple properties to sort where players tied on score, or hits, etc? Now you will probably want to turn this data into a nicely formatted string so that you can display it in a text member. Here is a handler that might work for you.

           

          --------------------------------------------------------------------------------

          on getDisplayString gameData, propertyList

            displayString = ""

            repeat with playerData in gameData

              repeat with prop in propertyList

                displayString = displayString & playerData[prop] & tab

              end repeat

              displayString = displayString.char[1..displayString.char.count - 1]

              displayString = displayString & return

            end repeat

            displayString = displayString.char[1..displayString.char.count - 1]

            return displayString

          end

          --------------------------------------------------------------------------------

           

          Example Usage:

           

          member("someTextMember").text = getDisplayString(gvGameData, [#name, #score, #hits, #bullets])

          1 person found this helpful
          • 2. Re: Linear List
            Mister MUX Level 2

            Also, with this data structure you don't want to update data by specifying an index. Obviously the player at element 7 wont necessarily be the same after sorting. Assuming player names are unique, a handler such as this can be used to update a player's data. It returns true if the update suceeded, or false if an invalid player name was passed.

             

            -----------------------------------------------------------------------------

            on updatePlayerData gameData, playerName, dataList

              playerIndex = 0

              repeat with x = 1 to gameData.count

                if gameData[x].name = playerName then

                  playerIndex = x

                  exit repeat

                end if

              end repeat

              if playerIndex = 0 then return false -- player name not found

              repeat with x = 1 to dataList.count

                gameData[playerIndex][dataList.getPropAt(x)] = dataList[x]

              end repeat

              return true -- player data was updated

            end

            -----------------------------------------------------------------------------

             

            Example usage:

             

            errorCode = updatePlayerData(gvGameData, "Player F", [#bullets:5,#score:99])

            1 person found this helpful
            • 3. Re: Linear List
              IEatSandvich Level 1

              Thanks, Mux. I'll see if i can get this to work, and let you know how it goes.

              • 4. Re: Linear List
                Mister MUX Level 2

                Well if you really want to stick with your original design, you can still use a bubble sort routine. You just have to manipulate each of the four lists individually.

                 

                 

                on updatescores

                  gvNameList[7] = (gvName)

                  gvScoreList[7] = (gvScore)

                  gvHitsList[7] = (gvHits)

                  gvBulletsList[7] = (gvTotalBullets)

                 

                  -- bubble sort

                  repeat with x = 1 to gvScoreList.count

                    repeat with y = 1 to gvScoreList.count - 1

                      if gvScoreList[y] < gvScoreList[y+1] then

                        -- sort the score list

                        temp = gvScoreList[y+1]

                        gvScoreList[y+1] = gvScoreList[y]

                        gvScoreList[y] = temp

                        -- sort the name list

                        temp = gvNameList[y+1]

                        gvNameList[y+1] = gvNameList[y]

                        gvNameList[y] = temp

                        -- sort the hits list

                        temp = gvHitsList[y+1]

                        gvHitsList[y+1] = gvHitsList[y]

                        gvHitsList[y] = temp

                        -- sort the bullets list

                        temp = gvBulletsList[y+1]

                        gvBulletsList[y+1] = gvBulletsList[y]

                        gvBulletsList[y] = temp

                      end if

                    end repeat

                  end repeat

                end