Jump to content

Issue with Array and For, Next loop


nitekram
 Share

Recommended Posts

  • Moderators

nitekram,

Sorry about that! ;) I was being badgered to take "she-who-must-be-obeyed" for yet more Xmas shooping.

Here is the code:

#include <GUIConstantsEx.au3>
#include <Array.au3>

; Declare these as Global so all functions can see them
Global $sIniFile = "Players.ini"
Global $aDetails[1][4], $iNumPlayers

; Create a GUI
$hGUI = GUICreate("Test", 500, 500)

$hRank = GUICtrlCreateButton("Rank", 10, 10, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hRank
            GUICtrlSetState($hRank, $GUI_DISABLE)
            Rank()
            GUICtrlSetState($hRank, $GUI_ENABLE)
    EndSwitch

WEnd

Func Rank()

    $hLabel = GUICtrlCreateLabel("Reading ini file", 30, 50, 200, 20)

    ; Load ini section names
    Local $aPlayers = IniReadSectionNames('Players.ini')

    ; Determine number of players
    $iNumPlayers = $aPlayers[0] - 1
    If $iNumPlayers < 11 Then
        GUICtrlDelete($hLabel)
        MsgBox(0, "", "The minimum number of players is 11" & @CRLF & "There are currently only " & $iNumPlayers & @CRLF & "Sorry!")
        Return
    Endif

    ; Remove element total and CONFIG section
    _ArrayDelete($aPlayers, 0)
    _ArrayDelete($aPlayers, 0)

    ; Create array to hold details
    Dim $aDetails[$iNumPlayers][4]
    For $i = 0 To $iNumPlayers - 1
        $aDetails[$i][0] = IniRead($sIniFile, $aPlayers[$i], "ID", 0)
        $aDetails[$i][1] = Number(IniRead($sIniFile, $aPlayers[$i], "RANK", 0))
        $aDetails[$i][2] = Number(IniRead($sIniFile, $aPlayers[$i], "TOTALRANK", 0))
        $aDetails[$i][3] = Number(IniRead($sIniFile, $aPlayers[$i], "MADE", 0))
    Next

    ; Check for duplicate IDs in the Details array
    $aIDs = _ArrayUnique($aDetails, 1)

    ; Check that the unique array has as many elements as the Details array - if not we have duplicate IDs
    If UBound($aDetails) <> UBound($aIDs) - 1 Then
        ; Run through arrays to find errors
        ; Initialise duplicate list
        $sDuplicates = ""
        ; For each unique ID
        For $i = 1 To $aIDs[0]
            $iCount = 0
            ; Find wheer it appears in the ini file
            For $j = 0 To $iNumPlayers - 1
                If $aDetails[$j][0] = $aIDs[$i] Then
                    ; Found an instance
                    $iCount += 1
                    Select
                        ; Store location of first instance
                        Case $iCount = 1
                            $iCurrID = $j
                        ; Found duplicate so add both to duplicate list
                        Case $iCount = 2
                            $sDuplicates &= $iCurrID & ", " & $j
                        ; Found yet another so add to duplicate list
                        Case $iCount > 2
                            $sDuplicates &= ", " & $j
                    EndSelect
                EndIf
            Next
            ; Format duplicate list
            If $iCount > 1 Then $sDuplicates &= @CRLF
        Next
        ; Display duplicate list
        GUICtrlDelete($hLabel)
        MsgBox(0, "Duplicate IDs", "The following Player sections have duplicate ID numbers" & @CRLF & $sDuplicates & @CRLF & _
                "Please fix the ini file before trying again!")
        ; Over to you to fix it!!!!
        Return
    EndIf

    GUICtrlCreateLabel(Chr(0x50), 10, 50, 10, 10)
        GUICtrlSetFont(-1, 9, 600, 4, "Wingdings 2")

    GUICtrlCreateLabel("Sorting teams and setting ranks", 30, 80, 200, 20)

    ; Sort array on TOTALRANK so we get the players in that order
    _ArraySort($aDetails, 1, 0, 0, 2)

    ; Read TOTALRANK of top team
    $iStartIndex = 0
    ;  Then, starting at the top, work our way down the array
    ; and for each section of teams with equal TOTALRANK
    ; sort on TOTAL
    Do
        ; This is the TOTALRANK we are looking for
        $nCurrentTOTALRANK = $aDetails[$iStartIndex][2]
        ; Pass the index of the first team with this TOTALRANK score to the sorting function
        $iStartIndex = Sort_Equal_TOTALRANK($iStartIndex, $nCurrentTOTALRANK)
        ; The function has returned the index of the team with the next TOTALRANK
        ; So we can go round the loop again until we reach the bottom

    Until $iStartIndex > $iNumPlayers - 1

    ; Now we have the players ordered on TOTALRANK and MADE
    ; So we need to set the initial percentage rankings

    ; Read the percentages from the ini file
    $sPercent = IniRead($sIniFile, "CONFIG", "PERCENT", 0)
    ; Get rid of the count value
    $sPercent = StringTrimLeft($sPercent, 2)
    ; Split the string into the values
    $aPercent = StringSplit($sPercent, ",")

    ; Create an array to hold the indices of the team starting each rank level
    Local $aRankings[10]
    $iMaxPercent = 0
    $aRankings[0] = 0
    For $i = 1 To 9
        $iMaxPercent += $aPercent[$i]
        $aRankings[$i] = Int($iNumPlayers * $iMaxPercent / 100)
    Next

    ; Adjust to cater for small numbers of players
    For $i = 1 To 9
        If $aRankings[$i] = $aRankings[$i - 1] Then $aRankings[$i] = $aRankings[$i - 1] + 1
    Next

    ; Set the rankings according to these indices
    For $i = 0 to 8
        For $j = $aRankings[$i] To $aRankings[$i + 1] - 1
            $aDetails[$j][1] = 11 - $i
        Next
    Next

    ; Now we need to go through and adjust the rankings if TOTALRANK values are equal
    ; We need to start looking at the last team in each rank level

    For $i = 1 To 8
        ; Last of the ranking teams is one above start of next rank
        $iStart = $aRankings[$i] - 1
        ; These are the values for that team
        $iRank = $aDetails[$iStart][1]
        $iCurrTOTALRANK = $aDetails[$iStart][2]
        ; Run down the array until the TOTALRANK values are no longer the same
        For $j = $iStart + 1 To $iNumPlayers - 1
            If $aDetails[$j][2] = $iCurrTOTALRANK Then
                ; Increase the rank if the values are the same
                $aDetails[$j][1] = $iRank
            Else
                ; Move on to the next rank junction
                ExitLoop
            EndIf
        Next
    Next

    GUICtrlCreateLabel(Chr(0x50), 10, 80, 10, 10)
        GUICtrlSetFont(-1, 9, 600, 4, "Wingdings 2")

    GUICtrlCreateLabel("Writing new ranks into ini file", 30, 110, 200, 20)

    ; Now we put the adjusted rank values into the ini file
    For $i = 0 To $iNumPlayers - 1
        $sCurrRank = IniRead($sIniFile, "Player" & $aDetails[$i][0], "RANK", 0)
        If $sCurrRank <> $aDetails[$i][1] Then IniWrite($sIniFile, "Player" & $aDetails[$i][0], "RANK", $aDetails[$i][1])
    Next

    GUICtrlCreateLabel(Chr(0x50), 10, 110, 10, 10)
        GUICtrlSetFont(-1, 9, 600, 4, "Wingdings 2")

    ; And we are all done!

EndFunc

Func Sort_Equal_TOTALRANK($iStart, $nTOTALRANK)

    ; We have the index of the first team with the same TOTALRANK
    ; Move through the array to count players with the same TOTALRANK
    For $i = $iStart To $iNumPlayers - 1
        If $aDetails[$i][2] <> $nTOTALRANK Then ExitLoop
    Next
    ; $i = the index of the first team witha lower TOTALRANK
    ; It is also the first index of the next comparison
    $iNextStart = $i

    ; Set indices of the equal TOTALRANKed players
    $iFirst = $iStart
    $iLast = $i - 1

    ; And sort these players on MADE
    If $iLast > $iFirst Then _ArraySort($aDetails, 1, $iFirst, $iLast, 3)

    ; Return the index for the next search/sort
    Return $iNextStart

EndFunc

Look for the lines immediately after "; Adjust to cater for small numbers of players" to see the fix for that problem.

I hope it now does what you want. :evil:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

That looks like it it going to do the trick...I really am thankful to you.

Have a great New Year and Xmas!

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...