editing a file/arrays from a file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

editing a file/arrays from a file

fosseyfossey Posts: 6Member
Hi guys,
I need some help in code,
If u are say, making a game, right, and someone gets a high score (saved as ‘score’); and it is in 2nd place out of 3, how can you shift down the other scores. I am using arrays of records to store name and score
(like ‘person[count].name;
person[count].scr (for score);’

im saving the files in high.txt

what im doing is reading the values from file, when ‘score>person[count].scr then begin’
and I don’t know how to make the scores below that one shift down and in turn, the score 3 will be deleted.

So if the score is 1st then ‘
person[1].name:= person[2].name;
and person[1].scr := and person[2].scr;
e.t.c e.t.c …..

is there an easier way that incorporates counts and so on…
thanx!

Comments

  • weedsmokaweedsmoka Posts: 34Member
    : Hi guys,
    : I need some help in code,
    : If u are say, making a game, right, and someone gets a high score (saved as ‘score’); and it is in 2nd place out of 3, how can you shift down the other scores. I am using arrays of records to store name and score
    : (like ‘person[count].name;
    : person[count].scr (for score);’
    :
    : im saving the files in high.txt
    :
    : what im doing is reading the values from file, when ‘score>person[count].scr then begin’
    : and I don’t know how to make the scores below that one shift down and in turn, the score 3 will be deleted.
    :
    : So if the score is 1st then ‘
    : person[1].name:= person[2].name;
    : and person[1].scr := and person[2].scr;
    : e.t.c e.t.c …..
    :
    : is there an easier way that incorporates counts and so on…
    : thanx!
    :
    :


    i did not quite understand the problem but here's how i would take care of scores in a game:

    const
    MaxScores=*whatever*;

    type
    TScore = record
    score:word;
    name:string;
    end;

    ScoreArray = array[1..MaxScores] of TScore;

    var
    Scores:ScoreArray;
    ScoreFile:file of ScoreArray;
    i:word;


    begin
    {In the initialization section of the game you
    load the score table from the file assuming it exists}

    assign(ScoreFile,'score.dat');
    reset(ScoreFile);
    read(ScoreFile,Scores);
    close(ScoreFile);

    {Now your Scores array is loaded}

    ....

    {Now someone got a hiscore so we'll make room for him in the
    table. The Position variable will represent the new hiscore's
    wanted position in the table}

    {Shift the array}
    for i:= MaxScores downto (Position+1) do Scores[i]:=Scores[i-1]

    {Now put the hiscore into the table}
    a[Position].Score:=NewHiScore;
    a[Position].Name:=PlayersName;

    {Repeat this everytime you have a hiscore}
    ....

    {Before quitting save the score table back to the file}

    Assign(ScoreFile,'score.dat');
    rewrite(f);
    write(f,Scores);
    close(f);
    {now your score table is on the disk}
    end.

    Actually you can save the scoretable any time you like - for example
    when a new hiscore is added.

    hope that helps.
    didnt compile this code, just typed it in.




  • sweeneysweeney Posts: 117Member
    : : Hi guys,
    : : I need some help in code,
    : : If u are say, making a game, right, and someone gets a high score (saved as ‘score’); and it is in 2nd place out of 3, how can you shift down the other scores. I am using arrays of records to store name and score
    : : (like ‘person[count].name;
    : : person[count].scr (for score);’
    : :
    : : im saving the files in high.txt
    : :
    : : what im doing is reading the values from file, when ‘score>person[count].scr then begin’
    : : and I don’t know how to make the scores below that one shift down and in turn, the score 3 will be deleted.
    : :
    : : So if the score is 1st then ‘
    : : person[1].name:= person[2].name;
    : : and person[1].scr := and person[2].scr;
    : : e.t.c e.t.c …..
    : :
    : : is there an easier way that incorporates counts and so on…
    : : thanx!
    : :
    : :
    :
    :
    : i did not quite understand the problem but here's how i would take care of scores in a game:
    :
    : const
    : MaxScores=*whatever*;
    :
    : type
    : TScore = record
    : score:word;
    : name:string;
    : end;
    :
    : ScoreArray = array[1..MaxScores] of TScore;
    :
    : var
    : Scores:ScoreArray;
    : ScoreFile:file of ScoreArray;
    : i:word;
    :
    :
    : begin
    : {In the initialization section of the game you
    : load the score table from the file assuming it exists}
    :
    : assign(ScoreFile,'score.dat');
    : reset(ScoreFile);
    : read(ScoreFile,Scores);
    : close(ScoreFile);
    :
    : {Now your Scores array is loaded}
    :
    : ....
    :
    : {Now someone got a hiscore so we'll make room for him in the
    : table. The Position variable will represent the new hiscore's
    : wanted position in the table}
    :
    : {Shift the array}
    : for i:= MaxScores downto (Position+1) do Scores[i]:=Scores[i-1]
    :
    : {Now put the hiscore into the table}
    : a[Position].Score:=NewHiScore;
    : a[Position].Name:=PlayersName;
    :
    : {Repeat this everytime you have a hiscore}
    : ....
    :
    : {Before quitting save the score table back to the file}
    :
    : Assign(ScoreFile,'score.dat');
    : rewrite(f);
    : write(f,Scores);
    : close(f);
    : {now your score table is on the disk}
    : end.
    :
    : Actually you can save the scoretable any time you like - for example
    : when a new hiscore is added.
    :
    : hope that helps.
    : didnt compile this code, just typed it in.
    :
    :
    :
    :
    :
    hi Can someone help me on the same thing! ok I have most of it working the only things not working are,

    assign(scorefile, 'score.dat');
    reset(scorefile);
    read(scorefile, 'scores'); { this is the part where its not working}
    close(scorefile);

    a[postion].score:=NewHighScore;
    a[postion.name:=Playersname;
    this is also not working! can anyone help please! grate to know!

    sweeney
  • Phat NatPhat Nat Posts: 757Member
    : Hi guys,
    : I need some help in code,
    : If u are say, making a game, right, and someone gets a high score (saved as ‘score’); and it is in 2nd place out of 3, how can you shift down the other scores. I am using arrays of records to store name and score
    : (like ‘person[count].name;
    : person[count].scr (for score);’
    :
    : im saving the files in high.txt
    :
    : what im doing is reading the values from file, when ‘score>person[count].scr then begin’
    : and I don’t know how to make the scores below that one shift down and in turn, the score 3 will be deleted.
    :
    : So if the score is 1st then ‘
    : person[1].name:= person[2].name;
    : and person[1].scr := and person[2].scr;
    : e.t.c e.t.c …..
    :
    : is there an easier way that incorporates counts and so on…
    : thanx!

    you are on the right track. But instead of typing them out each time, use a loop such as:
    [code]
    { If they get 2nd place then Start = 2 }

    { The first # will always be 1 less than the # of high score }
    { positions because the last place doesn't get saved. }
    For X := 2 downto Start Do
    Begin
    person[Start+1].Name := person[Start].Name;
    person[Start+1].Scr := person[Start].Scr;
    ...
    End;
    [/code]

    Now even this is not the greatest. We can make this even shorter like this:
    [code]
    For X := 2 downto Start Do
    person[Start+1] := person[Start]; { Copy the whole record }
    [/code]

    Easy as pie.... Mmmm... Pie....
    Yeah... well, hope this helps.

    Phat Nat
Sign In or Register to comment.