Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
editing a file/arrays from a file Posted by fossey on 22 Aug 2002 at 3:36 AM
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!

Report
Re: editing a file/arrays from a file Posted by weedsmoka on 22 Aug 2002 at 10:09 AM
: 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.




Report
Re: editing a file/arrays from a file Posted by sweeney on 11 Mar 2003 at 3:44 PM
: : 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
Report
Re: editing a file/arrays from a file Posted by Phat Nat on 11 Mar 2003 at 5:27 PM
: 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:
   { 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;


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


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

Phat Nat



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.