Question

I recently started to programm in delphi.

Now, I've got an exercice.

I have to save a top score in a text file. I've got this function:

{Voeg topscore toe aan het goede bestand.}
function addTopScore(sudokuNumber : Integer; naam : String; tijd : Integer; fouten : Integer):boolean;
var
  buffer : TStringList;
  aantal, aantal2, aantal3, i, i2, i3 : Integer;
  scoreArray : array of TArray;
  sameScoreArray : array of TArray;
  plaatsScoreArray : array of Integer;
  inputS1 : String;
  zelfde, stopChecking : boolean;
  insertBefore : Integer;
  outputSL : TStringList;
  outputNR : Integer;
begin
  {Haal de topscores op}
  buffer := TStringList.Create;
  buffer.LoadFromFile('scorelijst/lijst' + IntToStr(sudokuNumber));
  {Initialiseer variabelen}
  aantal  := 0;
  aantal3 := 0;
  aantal  := (buffer.Count - 1);
  zelfde := False;
  result := True;
  {Vul de score array (TStringList to Array)}
  for i := 0 to aantal do
  begin
    SetLength(scoreArray, i + 1);
    inputS1 := buffer[i];
    scoreArray[i] := Unit2.explode(',', inputS1, 0);
  end;

  {Controleer waar hij moet worden ingevoerd}
  insertBefore := 0;
  stopChecking := False;
  for i2 := 0 to aantal do
  begin
    {Als er al een punt is gevonden, hoeft niet meer gecontroleerd te worden}
    if(stopChecking = False) then
    begin
      {Als er een score van dezelfde persoon beter is, moet het result false zijn}
      if (StrToInt(scoreArray[i2][1]) < fouten) AND (scoreArray[i2][1] = naam) then
      begin
        result := False;
      end;

      {Als het aantal fouten, hetzelfde is als de huidige waarde, sla de positie op}
      if ( StrToInt(scoreArray[i2][1]) = fouten) then
      begin
        {aantal zelfde waarden + 1}
        aantal3 := aantal3 + 1;

        {Geef de arrays de goede lengte}
        SetLength(sameScoreArray, aantal3);
        SetLength(plaatsScoreArray, aantal3);

        {Vul de arrays}
        sameScoreArray[(aantal3 - 1)] := scoreArray[i2];
        plaatsScoreArray[(aantal3 - 1)] := i2;

        {Er is een zelfde waarde gevonden.}
        zelfde := True;
      end;

      {Als het aantal fouten groter is, dan de nieuwe}
      if ( (StrToInt(scoreArray[i2][1]) > fouten ) = True)  then
      begin
        {Stop de for loop checking}
        stopChecking := True;

        {Als er geen zelfde waarde is gevonden, moet hij voor deze i2 worden ingevoerd}
        if (zelfde = False) then
          insertBefore := i2;
      end;
    end;
  end;

  outputSL := TStringList.Create;
  if (insertBefore > 0) then
  begin
    outputNR := 0;
    for i3 := 0 to aantal do
    begin
      if ( i3 = insertBefore ) then
      begin
        outputSL[outputNR] := naam + ',' + IntToStr(fouten) + ',' + IntToStr(tijd);
        outputNR := outputNR + 1;
      end;

      outputSL[outputNR] := scoreArray[i3][0] + ',' + scoreArray[i3][1] + ',' + scoreArray[i3][2];
      outputNR := outputNR + 1;
    end;
  end
  else if (zelfde = True) then
  begin
    //Not finished.
  end;

  outputSL.SaveToFile('scorelijst/lijst' + IntToStr(sudokuNumber));
end;

The input (scorelijst/lijst1):

test,2,10
test,3,11

Now, I want to sort the new top score, on the right place in the text file. But, the output is empty... What am I doing wrong?

Comments are in dutch, sorry for that.

TArray = array of string;

Was it helpful?

Solution

Does Delphi 6 have TStringList.CustomSort? If so, create your own sorting routine and use that to sort the StringList.

OTHER TIPS

You should simply let the TStringList sort itself. i.e.:

outputSL.Sorted := true;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top