the following code is suppose to
1) "initiallize" a typed array under GenerateScores() (just filling it with -1 values for now - not the issue).
2) generate a random score, # of seconds it took the achieve the score, and add a date (using Timer() vs date for now to try and sort the issue) as a Type (ScoreData).
3) then CheckScore() to see if it makes the Top Ten list (i'll deal with return of any new rank later)
// Project: SortUDScores
// Created: 2020-03-21
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "SortUDScores" )
SetWindowSize( 1080, 720, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1080, 720 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
GLOBAL Version$ = "032120"
NewHighScore as Integer
NewHighScore = -1
TYPE ScoreData
Shots as Integer
Seconds as Integer
Date as String
ENDTYPE
GLOBAL Scores as ScoreData [9]
GLOBAL Low = 5
GLOBAL High = 10
GenerateScores(Low,High)
GLOBAL LastScore$ as String
TotalGens = 0
StartTimer# = Timer()
do
`Print("Start: " + STR(StartTimer#))
`Print( "LastScore: " + LastScore$ )
`If GetRawKeyPressed(32) = 1 `[SPACE] = Generate New Score
If TotalGens < 100
ThisScore = Random(Low ,10)
ThisSeconds = Random(Low ,10)
NewHighScore = CheckScore(ThisScore,ThisSeconds, STR(Timer())) `change to Date
TotalGens = TotalGens + 1
else
if GetRawKeyPressed(13) = 1
GenerateScores(5,10)
TotalGens = 0
endif
Endif
For x = 0 to 9
ThisScore$ = STR(Scores[x].Shots)
ThisSeconds$ = STR(Scores[x].Seconds) + "''"
ThisDate$ = Scores[x].Date
Print(STR(x) + " = " + ThisScore$ + " in " + ThisSeconds$ + " @ " + ThisDate$)
Next x
`Print("LastKey: " + STR(GetRawLastKey()))
`Print("Length: " + STR(Scores.Length))
`Print(TotalGens)
`Print("LastScoreAdded: " + STR(NewHireScore))
Sync()
loop
Function GenerateScores(Low,High)
For x = 0 to 9
ThisScore = -1 `Random(Low,High)
ThisSeconds = -1
Scores[x].Shots = ThisScore
Scores[x].Seconds = ThisSeconds
Scores[x].Date = STR(Timer())
next x
EndFunction
Function CheckScore(ThisScore,ThisSeconds,ThisDate$)
NewScore = ThisScore : NewSeconds = ThisSeconds : NewDate$ = ThisDate$ `hold to find index
Result = -1
`InsertIndex = -1
AddScore as ScoreData
AddScore.Shots = ThisScore
AddScore.Seconds = ThisSeconds
AddScore.Date = ThisDate$
Scores.Insert(AddScore)
for x = Scores.Length to 1 step -1
IndexScore = Scores[x].Shots
IndexSeconds = Scores[x].Seconds
IndexDate$ = Scores[x].Date
AboveScore = Scores[x-1].Shots
AboveSeconds = Scores[x-1].Seconds
AboveDate$ = Scores[x-1].Date
If AboveScore = -1 or IndexScore < AboveScore or IndexScore = AboveScore and IndexSeconds <= AboveSeconds `then swap
HoldScore = IndexScore
HoldSeconds = IndexSeconds
HoldDate$ = IndexDate$
Scores[x].Shots = AboveScore
Scores[x].Seconds = AboveSeconds
Scores[x].Date = AboveDate$
Scores[x-1].Shots = HoldScore
Scores[x-1].Seconds = HoldSeconds
Scores[x-1].Date = HoldDate$
Endif
next x
Scores.Remove()
EndFunction Result
the desired sorting results aren't quite right where, if a score is equal to one already in the list, the seconds are checked. if the seconds are <= to that score and seconds, i want it to go on top. IE, as it climbs up from the bottom, it should be swapped.
since the scores are timestamped, for any new score and seconds that are duplicates, the most-recent one should rank above the other.
ie, the bottom 4 (which are identical in score and seconds) should be sorted newest to old (9, 7, 6, 8) but aren't:
neither are ranks 3-5, while ranks 0-2 are... something's amiss.
meanwhile, i'm attempting a manual swap using HoldScore, HoldSeconds, HoldDate$ where Scores.Swap(x,x-1) didn't seem to be swapping all values of the type?
any guidance is appreciated