Newbhelp-saving - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Newbhelp-saving

wizarbowizarbo Posts: 29Member
[b][red]This message was edited by Moderator at 2003-3-16 11:45:49[/red][/b][hr]
[b][red]This message was edited by wizarbo at 2003-3-16 9:17:15[/red][/b][hr]
I red the message poasted below on saving and I was lost. I only took a semester long class in school on programming and we spent about a term on pascal so I dont know everything about pascal. we never learned what those "arrays" were and some other things like types. I am making a game and i want to be able to save so I made a sample code so it wouldn't be so long and I included it below. I have it so it would have 3 slots with names in it but this is not necesary. I just thought that might be easy. I was going to make a text save because i think that you would just make it save like this: "name,money,race" and then recall it but if there is an easier way please tell me!!! So if you could help me out and tell me what some of the things are doing I would really apreatiate it.
Thanks for your time!


[code]
uses crt;

var
name,load,race:string;
char_load,money:intager;

procedure sats;
writeln('You are ',name);
writeln(name,' is a ',race);
writeln('Your walet containes $,'money);
begin
clrscr;
write('Do you have a character you can load?(y or n)');
readln(load);
if load='y' then
begin
writeln('Select your character.');
writeln;
writeln('1.',slot1);
writeln('2.',slot2);
writeln('3.',slot3);
writeln;
write('You are:');
readln(char_load);
end;
if load<>'y' then
begin
write('Enter your name:');
readln(name);
write('What race should you be?');
readln(race);
write('How much money do you want?');
readln(money);
end;
writeln('You are now set, to see crap just type "stats"');
writeln('You may also "save" your character');
writeln('And dont forget how you "quit"');
while move<>'quit' do
begin
writeln;
write('Tell me what you want to do');
readln(move);
if move='save' then
save;
if move='sats' then
stats;
end;



end.
[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by wizarbo at 2003-3-16 9:17:15[/red][/b][hr]
    : I red the message poasted below on saving and I was lost. I only took a semester long class in school on programming and we spent about a term on pascal so I dont know everything about pascal. we never learned what those "arrays" were and some other things like types. I am making a game and i want to be able to save so I made a sample code so it wouldn't be so long and I included it below. I have it so it would have 3 slots with names in it but this is not necesary. I just thought that might be easy. I was going to make a text save because i think that you would just make it save like this: "name,money,race" and then recall it but if there is an easier way please tell me!!! So if you could help me out and tell me what some of the things are doing I would really apreatiate it.
    : Thanks for your time!
    :
    :
    : uses crt;
    :
    : var
    : name,load,race:string;
    : char_load,money:intager;
    :
    : procedure sats;
    : writeln('You are ',name);
    : writeln(name,' is a ',race);
    : writeln('Your walet containes $,'money);
    : begin
    : clrscr;
    : write('Do you have a character you can load?(y or n)');
    : readln(load);
    : if load='y' then
    : begin
    : writeln('Select your character.');
    : writeln;
    : writeln('1.',slot1);
    : writeln('2.',slot2);
    : writeln('3.',slot3);
    : writeln;
    : write('You are:');
    : readln(char_load);
    : end;
    : if load<>'y' then
    : begin
    : write('Enter your name:');
    : readln(name);
    : write('What race should you be?');
    : readln(race);
    : write('How much money do you want?');
    : readln(money);
    : end;
    : writeln('You are now set, to see crap just type "stats"');
    : writeln('You may also "save" your character');
    : writeln('And dont forget how you "quit"');
    : while move<>'quit' do
    : begin
    : writeln;
    : write('Tell me what you want to do');
    : readln(move);
    : if move='save' then
    : save;
    : if move='sats' then
    : stats;
    : end;
    :
    :
    :
    : end.
    :
    Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    [code]
    procedure Save(filename: string);
    var
    f: text;
    begin
    Assign(f, filename); { Assign the name to the file variable }
    rewrite(f); { open/create the file to write }
    writeln(f, name); { write a single field to the file }
    writeln(f, race);
    writeln(f, money);
    close(f); { close the file again }
    end;
    [/code]
    The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.

    [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
  • ManningManning Posts: 1,621Member
    : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    :

    Heh I try to fix people's messages when they dont use the stylecodes, but you replied to fast to this one :-)
  • wizarbowizarbo Posts: 29Member
    : Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    : [code]
    : procedure Save(filename: string);
    : var
    : f: text;
    : begin
    : Assign(f, filename); { Assign the name to the file variable }
    : rewrite(f); { open/create the file to write }
    : writeln(f, name); { write a single field to the file }
    : writeln(f, race);
    : writeln(f, money);
    : close(f); { close the file again }
    : end;
    : [/code]
    : The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.
    :
    : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    :
    Thanks it worked great!!! I was wondering if there is a way to show a list of saved files so it looked like this:
    1.John
    2.Mike
    3.Eric
    so that you could select from some files.
    thanks again!
  • zibadianzibadian Posts: 6,349Member
    : : Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    : : [code]
    : : procedure Save(filename: string);
    : : var
    : : f: text;
    : : begin
    : : Assign(f, filename); { Assign the name to the file variable }
    : : rewrite(f); { open/create the file to write }
    : : writeln(f, name); { write a single field to the file }
    : : writeln(f, race);
    : : writeln(f, money);
    : : close(f); { close the file again }
    : : end;
    : : [/code]
    : : The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.
    : :
    : : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    : :
    : Thanks it worked great!!! I was wondering if there is a way to show a list of saved files so it looked like this:
    : 1.John
    : 2.Mike
    : 3.Eric
    : so that you could select from some files.
    : thanks again!
    :
    You could use FindFirst() and FindNext() for that. The best way is to make something like this:
    [code]
    if FindFirst(...)=... then repeat
    ...
    until FindNext(...)=...;
    [/code]
    The pseudocode above is not complete, but given the help files you should be able to complete it.
  • wizarbowizarbo Posts: 29Member
    : : : Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    : : : [code]
    : : : procedure Save(filename: string);
    : : : var
    : : : f: text;
    : : : begin
    : : : Assign(f, filename); { Assign the name to the file variable }
    : : : rewrite(f); { open/create the file to write }
    : : : writeln(f, name); { write a single field to the file }
    : : : writeln(f, race);
    : : : writeln(f, money);
    : : : close(f); { close the file again }
    : : : end;
    : : : [/code]
    : : : The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.
    : : :
    : : : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    : : :
    : : Thanks it worked great!!! I was wondering if there is a way to show a list of saved files so it looked like this:
    : : 1.John
    : : 2.Mike
    : : 3.Eric
    : : so that you could select from some files.
    : : thanks again!
    : :
    : You could use FindFirst() and FindNext() for that. The best way is to make something like this:
    : [code]
    : if FindFirst(...)=... then repeat
    : ...
    : until FindNext(...)=...;
    : [/code]
    : The pseudocode above is not complete, but given the help files you should be able to complete it.
    :
    Dude you are so awsome!!! Thanks for all this help that works great. But i have encounterd another error. I have it so you can pick a guy but you need the password to load him. But if you creat a new guy with the same name you can just save over the other guy. I set it so that if you enter teh same name it makes you re-pick but because it only saves in all caps you must enter it in all caps otherwise it will still save over in all caps even if the name you make is in diffrent caps. If you know how to make it save not in all caps or so that it will read no matter what caps it is that would be helpfull! Thanks again!!!!!!!!!

  • zibadianzibadian Posts: 6,349Member
    : : : : Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    : : : : [code]
    : : : : procedure Save(filename: string);
    : : : : var
    : : : : f: text;
    : : : : begin
    : : : : Assign(f, filename); { Assign the name to the file variable }
    : : : : rewrite(f); { open/create the file to write }
    : : : : writeln(f, name); { write a single field to the file }
    : : : : writeln(f, race);
    : : : : writeln(f, money);
    : : : : close(f); { close the file again }
    : : : : end;
    : : : : [/code]
    : : : : The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.
    : : : :
    : : : : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    : : : :
    : : : Thanks it worked great!!! I was wondering if there is a way to show a list of saved files so it looked like this:
    : : : 1.John
    : : : 2.Mike
    : : : 3.Eric
    : : : so that you could select from some files.
    : : : thanks again!
    : : :
    : : You could use FindFirst() and FindNext() for that. The best way is to make something like this:
    : : [code]
    : : if FindFirst(...)=... then repeat
    : : ...
    : : until FindNext(...)=...;
    : : [/code]
    : : The pseudocode above is not complete, but given the help files you should be able to complete it.
    : :
    : Dude you are so awsome!!! Thanks for all this help that works great. But i have encounterd another error. I have it so you can pick a guy but you need the password to load him. But if you creat a new guy with the same name you can just save over the other guy. I set it so that if you enter teh same name it makes you re-pick but because it only saves in all caps you must enter it in all caps otherwise it will still save over in all caps even if the name you make is in diffrent caps. If you know how to make it save not in all caps or so that it will read no matter what caps it is that would be helpfull! Thanks again!!!!!!!!!
    :
    :
    As far as I know are DOS/WIN filenames case-insensitive, so no matter what the caps the file is treated as the same file. You could use some counter to diverentiate between names. Here is a small example code:
    [code]
    { Append a counter }
    { ================ }
    i := 0;
    while FindFirst(path+filename+IntToStr(i)+ext, faAnyFile, SR) = 0 do
    { if file is found then increase counter }
    inc(i);
    SaveCharacter(path+filename+IntToStr(i)+ext);
    [/code]
    Notes:
    - substitute path, filename, ext and SaveCharacter() for the correct variables.
    - IntToStr() is a function which converts an integer into a string. If you version of Pascal doesn't know it, you can easily create it using the Val() procedure.
    - the loop might need to be rewritten slightly for TP:
    [code]
    FindFirst(...);
    while DosError = 0 do begin
    inc(i);
    FindFirst(...);
    end;
    [/code]
    - In DOS you might need to shorten the filename to 8 characters.
  • wizarbowizarbo Posts: 29Member
    : : : : : Here is a small sample code, which saves your character data 1 field per line. This is much easier to read back than a single line (although that is also possible).
    : : : : : [code]
    : : : : : procedure Save(filename: string);
    : : : : : var
    : : : : : f: text;
    : : : : : begin
    : : : : : Assign(f, filename); { Assign the name to the file variable }
    : : : : : rewrite(f); { open/create the file to write }
    : : : : : writeln(f, name); { write a single field to the file }
    : : : : : writeln(f, race);
    : : : : : writeln(f, money);
    : : : : : close(f); { close the file again }
    : : : : : end;
    : : : : : [/code]
    : : : : : The Load procedure is very similar, but uses the reset() and readln() procedures. See the help files for more info on these procedures.
    : : : : :
    : : : : : [red]Please, when posting code in a message use the stylecodes to improve readability!!!![/red] See below the edit field for more info.
    : : : : :
    : : : : Thanks it worked great!!! I was wondering if there is a way to show a list of saved files so it looked like this:
    : : : : 1.John
    : : : : 2.Mike
    : : : : 3.Eric
    : : : : so that you could select from some files.
    : : : : thanks again!
    : : : :
    : : : You could use FindFirst() and FindNext() for that. The best way is to make something like this:
    : : : [code]
    : : : if FindFirst(...)=... then repeat
    : : : ...
    : : : until FindNext(...)=...;
    : : : [/code]
    : : : The pseudocode above is not complete, but given the help files you should be able to complete it.
    : : :
    : : Dude you are so awsome!!! Thanks for all this help that works great. But i have encounterd another error. I have it so you can pick a guy but you need the password to load him. But if you creat a new guy with the same name you can just save over the other guy. I set it so that if you enter teh same name it makes you re-pick but because it only saves in all caps you must enter it in all caps otherwise it will still save over in all caps even if the name you make is in diffrent caps. If you know how to make it save not in all caps or so that it will read no matter what caps it is that would be helpfull! Thanks again!!!!!!!!!
    : :
    : :
    : As far as I know are DOS/WIN filenames case-insensitive, so no matter what the caps the file is treated as the same file. You could use some counter to diverentiate between names. Here is a small example code:
    : [code]
    : { Append a counter }
    : { ================ }
    : i := 0;
    : while FindFirst(path+filename+IntToStr(i)+ext, faAnyFile, SR) = 0 do
    : { if file is found then increase counter }
    : inc(i);
    : SaveCharacter(path+filename+IntToStr(i)+ext);
    : [/code]
    : Notes:
    : - substitute path, filename, ext and SaveCharacter() for the correct variables.
    : - IntToStr() is a function which converts an integer into a string. If you version of Pascal doesn't know it, you can easily create it using the Val() procedure.
    : - the loop might need to be rewritten slightly for TP:
    : [code]
    : FindFirst(...);
    : while DosError = 0 do begin
    : inc(i);
    : FindFirst(...);
    : end;
    : [/code]
    : - In DOS you might need to shorten the filename to 8 characters.
    :
    Ok i didn't quite undestand what you ment there. Ill show you what i have and mabey you can tell me a better way orsomething but i didn't really understand what you said just there. sorry :-(
    thanks though dude!


    [code]
    write('Do you have a character you can load?(y or n)');
    readln(load);
    if load='y' then
    begin
    1:
    writeln('Select your character(hit "0" to make a new character.');
    writeln;
    FindFirst('*.', Archive, DirInfo);
    load_counter:=0;
    while DosError = 0 do
    begin
    load_counter:=load_counter+1;
    Writeln('1.',DirInfo.Name);
    char_1:=dirinfo.name;
    FindNext(DirInfo);
    if doserror <> 0 then
    goto 2;
    load_counter:=load_counter+1;
    Writeln('2.',DirInfo.Name);
    char_2:=dirinfo.name;
    FindNext(DirInfo);
    if doserror <> 0 then
    goto 2;
    {****I cut some out here, it goes to 10****}
    2:
    end;
    writeln;
    if load_counter=0 then
    begin
    clrscr;
    writeln('!!!NO AVALIABLE CHARACTERS!!!');
    readln;
    goto 3;
    end;
    write('You are:');
    readln(char_load);
    if (char_load>load_counter) or (char_load<-1) then
    begin
    writeln('That is not a valid choice! Please relselect');
    goto 1;
    end;
    if char_load=0 then
    goto 3;
    if char_load=1 then
    name:=char_1;
    if char_load=2 then
    name:=char_2;
    assign(f, name);
    reset(f);
    readln(f, name);
    readln(f, password);
    write('Password:');
    readln(pass_check);
    pass_counter:=0;
    if pass_check<>password then
    begin
    repeat
    begin
    writeln('!!!Incorect!!!');
    writeln;
    write('Password:');
    readln(pass_check);
    pass_counter:=pass_counter+1;
    end;
    until (pass_counter=2) or (password=pass_check);
    if pass_counter=2 then
    goto 1;
    end;
    readln(f, name);
    readln(f, race);
    readln(f, money);
    close(f);
    end;
    if load<>'y' then
    begin
    3:
    clrscr;
    FindFirst('*.', Archive, DirInfo);
    while DosError = 0 do
    begin
    char_1:=dirinfo.name;
    FindNext(DirInfo);
    if doserror <> 0 then
    goto 4;
    char_2:=dirinfo.name;
    FindNext(DirInfo);
    if doserror <> 0 then
    goto 4;
    4:
    end;
    write('Enter your name:');
    readln(name);
    if (name=char_1)or(name=char_2)then
    begin
    writeln('That name is already taken. Please repick.');
    goto 3;
    end;
    [/code]
    WOW!! A lot to handle right. Anyway if you don't type in all caps you can continue on in making the guy with the same but it saves over the other save. And that sucks cuse then your guy could be erased before you beet the game. So if you know how to fix this, or know a better way id apreaciate you telling me!
    thanks again dude!!!!!
  • Phat NatPhat Nat Posts: 757Member
    : Ok i didn't quite undestand what you ment there. Ill show you what i have and mabey you can tell me a better way orsomething but i didn't really understand what you said just there. sorry :-(
    : thanks though dude!
    :
    :
    : [code]
    : write('Do you have a character you can load?(y or n)');
    : readln(load);
    : if load='y' then
    : begin
    : 1:
    : writeln('Select your character(hit "0" to make a new character.');
    : writeln;
    : FindFirst('*.', Archive, DirInfo);
    : load_counter:=0;
    : while DosError = 0 do
    : begin
    : load_counter:=load_counter+1;
    : Writeln('1.',DirInfo.Name);
    : char_1:=dirinfo.name;
    : FindNext(DirInfo);
    : if doserror <> 0 then
    : goto 2;
    : load_counter:=load_counter+1;
    : Writeln('2.',DirInfo.Name);
    : char_2:=dirinfo.name;
    : FindNext(DirInfo);
    : if doserror <> 0 then
    : goto 2;
    : {****I cut some out here, it goes to 10****}
    : 2:
    : end;
    : writeln;
    : if load_counter=0 then
    : begin
    : clrscr;
    : writeln('!!!NO AVALIABLE CHARACTERS!!!');
    : readln;
    : goto 3;
    : end;
    : write('You are:');
    : readln(char_load);
    : if (char_load>load_counter) or (char_load<-1) then
    : begin
    : writeln('That is not a valid choice! Please relselect');
    : goto 1;
    : end;
    : if char_load=0 then
    : goto 3;
    : if char_load=1 then
    : name:=char_1;
    : if char_load=2 then
    : name:=char_2;
    : assign(f, name);
    : reset(f);
    : readln(f, name);
    : readln(f, password);
    : write('Password:');
    : readln(pass_check);
    : pass_counter:=0;
    : if pass_check<>password then
    : begin
    : repeat
    : begin
    : writeln('!!!Incorect!!!');
    : writeln;
    : write('Password:');
    : readln(pass_check);
    : pass_counter:=pass_counter+1;
    : end;
    : until (pass_counter=2) or (password=pass_check);
    : if pass_counter=2 then
    : goto 1;
    : end;
    : readln(f, name);
    : readln(f, race);
    : readln(f, money);
    : close(f);
    : end;
    : if load<>'y' then
    : begin
    : 3:
    : clrscr;
    : FindFirst('*.', Archive, DirInfo);
    : while DosError = 0 do
    : begin
    : char_1:=dirinfo.name;
    : FindNext(DirInfo);
    : if doserror <> 0 then
    : goto 4;
    : char_2:=dirinfo.name;
    : FindNext(DirInfo);
    : if doserror <> 0 then
    : goto 4;
    : 4:
    : end;
    : write('Enter your name:');
    : readln(name);
    : if (name=char_1)or(name=char_2)then
    : begin
    : writeln('That name is already taken. Please repick.');
    : goto 3;
    : end;
    : [/code]
    : WOW!! A lot to handle right. Anyway if you don't type in all caps you can continue on in making the guy with the same but it saves over the other save. And that sucks cuse then your guy could be erased before you beet the game. So if you know how to fix this, or know a better way id apreaciate you telling me!
    : thanks again dude!!!!!
    :

    Just examined your code and I'm going to make a few suggestions that you can take or leave, but will help you out in the long run.
    To start with, to fix your problem of overwriting an existing character, there are two things that can be done.
    1) Check if the file exists and don't allow it
    2) change the filename to something different (as described by zibadian)

    The first one is extremely easy to do. Built into TP is a function known as UPCASE(); Although this will only do 1 character, this can be modified. Look at this function:
    [code]
    FUNCTION UpStr(S : String) : String;
    VAR X : Byte;
    Begin
    For X := 1 to Length(S) Do
    S[X] := UpCase(S[X]);
    UpStr := S;
    End;
    [/code]
    This will take a String variable and go through it one letter at a time and change them to uppercase. So you could then use it like this:
    [code]
    write('Enter your name:');
    readln(name);
    name := UpStr(name); { NAME is now in all upper-case }
    if (name=UpStr(char_1))or(name=UpStr(char_2))then
    begin
    writeln('That name is already taken. Please repick.');
    goto 3;
    end;
    [/code]

    [hr]
    The second method is better because it will allow two different people to have the same name. You could number them, such as:
    BOB-0.
    BOB-1.
    BOB-2.

    or you could just have them saved as numbers like this:
    player00.sav
    player01.sav
    player02.sav

    This would not effect anything since the name is stored inside of the file and you could just read the name out for displaying. Here's a quick example of how to get these:
    [code]
    PROCEDURE LoadPlayer(Num : Byte); { Num = 0-99 }
    VAR
    PNum : String);

    Begin
    Str(Num, PNum); { Convert Number to String }
    If Length(PNum) < 2 Then PNum := '0' + PNum; { If < 10 add 0 to front }
    assign(F, 'PLAYER'+PNum)'; Reset(F);
    {...}
    close(F);
    End;
    [/code]

    [hr]
    The other thing I wanted to mention is that you should not load the file names until AFTER they have entered the name to Load. That way, you can easily have more than 2 saved games and you do not have to keep them in memory. Think of it like this:
    [b]
    You have three files: Play01, Play02 & Play03.
    If you get these names first, your program will only hold 2. Therefore if the player enters PLAY01 or PLAY02 then it will say they exist, but if they enter PLAY03 then it will think it is okay.
    [/b]

    Not, if you have the name, let's say PLAY06, you can just compare as you search through.
    [b]
    Get Players Name -> PLAY06
    Find the first File (PLAY01) and compare them. PLAY01 <> PLAY06, so get next file (PLAY02) and compare them. Again, PLAY02 <> PLAY06. Repeat this until you find PLAY06 (or no other files found). PLAY06 = PLAY06 so the file will not be overwritten.
    [/b]

    One last thing. Labels are very hard to follow, so it is better to use PROCEDURES or else REPEAT/UNTIL DO/WHILE statements. Here is a brief use of them and the CONTINUE statement.
    [code]
    X := 0;
    REPEAT
    X := X + 1;
    If X = 4 Then Continue;
    WriteLn(X);
    UNTIL X = 6;
    [/code]
    If you were to run this code, numbers 1,2,3,5 & 6 would be written on the screen, but 4 would be missed. The continue statement will go to the top of the Loop, skipping everything after it.

    Lots of info here, hope this helps. If you are still stuck or need/want any suggestions, just write back

    Phat Nat
  • wizarbowizarbo Posts: 29Member
    : : Ok i didn't quite undestand what you ment there. Ill show you what i have and mabey you can tell me a better way orsomething but i didn't really understand what you said just there. sorry :-(
    : : thanks though dude!
    : :
    : :
    : : [code]
    : : write('Do you have a character you can load?(y or n)');
    : : readln(load);
    : : if load='y' then
    : : begin
    : : 1:
    : : writeln('Select your character(hit "0" to make a new character.');
    : : writeln;
    : : FindFirst('*.', Archive, DirInfo);
    : : load_counter:=0;
    : : while DosError = 0 do
    : : begin
    : : load_counter:=load_counter+1;
    : : Writeln('1.',DirInfo.Name);
    : : char_1:=dirinfo.name;
    : : FindNext(DirInfo);
    : : if doserror <> 0 then
    : : goto 2;
    : : load_counter:=load_counter+1;
    : : Writeln('2.',DirInfo.Name);
    : : char_2:=dirinfo.name;
    : : FindNext(DirInfo);
    : : if doserror <> 0 then
    : : goto 2;
    : : {****I cut some out here, it goes to 10****}
    : : 2:
    : : end;
    : : writeln;
    : : if load_counter=0 then
    : : begin
    : : clrscr;
    : : writeln('!!!NO AVALIABLE CHARACTERS!!!');
    : : readln;
    : : goto 3;
    : : end;
    : : write('You are:');
    : : readln(char_load);
    : : if (char_load>load_counter) or (char_load<-1) then
    : : begin
    : : writeln('That is not a valid choice! Please relselect');
    : : goto 1;
    : : end;
    : : if char_load=0 then
    : : goto 3;
    : : if char_load=1 then
    : : name:=char_1;
    : : if char_load=2 then
    : : name:=char_2;
    : : assign(f, name);
    : : reset(f);
    : : readln(f, name);
    : : readln(f, password);
    : : write('Password:');
    : : readln(pass_check);
    : : pass_counter:=0;
    : : if pass_check<>password then
    : : begin
    : : repeat
    : : begin
    : : writeln('!!!Incorect!!!');
    : : writeln;
    : : write('Password:');
    : : readln(pass_check);
    : : pass_counter:=pass_counter+1;
    : : end;
    : : until (pass_counter=2) or (password=pass_check);
    : : if pass_counter=2 then
    : : goto 1;
    : : end;
    : : readln(f, name);
    : : readln(f, race);
    : : readln(f, money);
    : : close(f);
    : : end;
    : : if load<>'y' then
    : : begin
    : : 3:
    : : clrscr;
    : : FindFirst('*.', Archive, DirInfo);
    : : while DosError = 0 do
    : : begin
    : : char_1:=dirinfo.name;
    : : FindNext(DirInfo);
    : : if doserror <> 0 then
    : : goto 4;
    : : char_2:=dirinfo.name;
    : : FindNext(DirInfo);
    : : if doserror <> 0 then
    : : goto 4;
    : : 4:
    : : end;
    : : write('Enter your name:');
    : : readln(name);
    : : if (name=char_1)or(name=char_2)then
    : : begin
    : : writeln('That name is already taken. Please repick.');
    : : goto 3;
    : : end;
    : : [/code]
    : : WOW!! A lot to handle right. Anyway if you don't type in all caps you can continue on in making the guy with the same but it saves over the other save. And that sucks cuse then your guy could be erased before you beet the game. So if you know how to fix this, or know a better way id apreaciate you telling me!
    : : thanks again dude!!!!!
    : :
    :
    : Just examined your code and I'm going to make a few suggestions that you can take or leave, but will help you out in the long run.
    : To start with, to fix your problem of overwriting an existing character, there are two things that can be done.
    : 1) Check if the file exists and don't allow it
    : 2) change the filename to something different (as described by zibadian)
    :
    : The first one is extremely easy to do. Built into TP is a function known as UPCASE(); Although this will only do 1 character, this can be modified. Look at this function:
    : [code]
    : FUNCTION UpStr(S : String) : String;
    : VAR X : Byte;
    : Begin
    : For X := 1 to Length(S) Do
    : S[X] := UpCase(S[X]);
    : UpStr := S;
    : End;
    : [/code]
    : This will take a String variable and go through it one letter at a time and change them to uppercase. So you could then use it like this:
    : [code]
    : write('Enter your name:');
    : readln(name);
    : name := UpStr(name); { NAME is now in all upper-case }
    : if (name=UpStr(char_1))or(name=UpStr(char_2))then
    : begin
    : writeln('That name is already taken. Please repick.');
    : goto 3;
    : end;
    : [/code]
    :
    : [hr]
    : The second method is better because it will allow two different people to have the same name. You could number them, such as:
    : BOB-0.
    : BOB-1.
    : BOB-2.
    :
    : or you could just have them saved as numbers like this:
    : player00.sav
    : player01.sav
    : player02.sav
    :
    : This would not effect anything since the name is stored inside of the file and you could just read the name out for displaying. Here's a quick example of how to get these:
    : [code]
    : PROCEDURE LoadPlayer(Num : Byte); { Num = 0-99 }
    : VAR
    : PNum : String);
    :
    : Begin
    : Str(Num, PNum); { Convert Number to String }
    : If Length(PNum) < 2 Then PNum := '0' + PNum; { If < 10 add 0 to front }
    : assign(F, 'PLAYER'+PNum)'; Reset(F);
    : {...}
    : close(F);
    : End;
    : [/code]
    :
    : [hr]
    : The other thing I wanted to mention is that you should not load the file names until AFTER they have entered the name to Load. That way, you can easily have more than 2 saved games and you do not have to keep them in memory. Think of it like this:
    : [b]
    : You have three files: Play01, Play02 & Play03.
    : If you get these names first, your program will only hold 2. Therefore if the player enters PLAY01 or PLAY02 then it will say they exist, but if they enter PLAY03 then it will think it is okay.
    : [/b]
    :
    : Not, if you have the name, let's say PLAY06, you can just compare as you search through.
    : [b]
    : Get Players Name -> PLAY06
    : Find the first File (PLAY01) and compare them. PLAY01 <> PLAY06, so get next file (PLAY02) and compare them. Again, PLAY02 <> PLAY06. Repeat this until you find PLAY06 (or no other files found). PLAY06 = PLAY06 so the file will not be overwritten.
    : [/b]
    :
    : One last thing. Labels are very hard to follow, so it is better to use PROCEDURES or else REPEAT/UNTIL DO/WHILE statements. Here is a brief use of them and the CONTINUE statement.
    : [code]
    : X := 0;
    : REPEAT
    : X := X + 1;
    : If X = 4 Then Continue;
    : WriteLn(X);
    : UNTIL X = 6;
    : [/code]
    : If you were to run this code, numbers 1,2,3,5 & 6 would be written on the screen, but 4 would be missed. The continue statement will go to the top of the Loop, skipping everything after it.
    :
    : Lots of info here, hope this helps. If you are still stuck or need/want any suggestions, just write back
    :
    : Phat Nat
    :
    Thanks dude. First off i usually dont use labels but i was too tired to think when i wrote that part and didnt want to think of how to do the loop so i took the easy way out.(lol) I plan on changing it, but thanks for helping with that i didn't know about the continue statemnet which is cool. I dont think i will use the saving of player01, player02 and stuff becuase it would be kinda hard to make the number go up for each save. But thanks for that uppcase. If i knew that existed i would have used that because i used that loop you had to cause the text to look like it typed out on the screen using delays and the for x:=1 to length.Thanks alot for everything, i apreaciate it!!!
  • Phat NatPhat Nat Posts: 757Member
    : : The second method is better because it will allow two different people to have the same name. You could number them, such as:
    : : player00.sav
    : : player01.sav
    : : player02.sav
    : :
    : : This would not effect anything since the name is stored inside of the file and you could just read the name out for displaying. Here's a quick example of how to get these:
    [code]
    PROCEDURE LoadPlayer(Num : Byte); { Num = 0-99 }
    VAR
    PNum : String);

    Begin
    Str(Num, PNum); { Convert Number to String }
    If Length(PNum) < 2 Then PNum := '0' + PNum; { If < 10 add 0 to front }
    assign(F, 'PLAYER'+PNum)'; Reset(F);
    {...}
    close(F);
    End;
    [/code]
    : Thanks dude. First off i usually dont use labels but i was too tired to think when i wrote that part and didnt want to think of how to do the loop so i took the easy way out.(lol) I plan on changing it, but thanks for helping with that i didn't know about the continue statemnet which is cool. I dont think i will use the saving of player01, player02 and stuff becuase it would be kinda hard to make the number go up for each save. But thanks for that uppcase. If i knew that existed i would have used that because i used that loop you had to cause the text to look like it typed out on the screen using delays and the for x:=1 to length.Thanks alot for everything, i apreaciate it!!!
    :

    Your welcome. If you did want to use the numbering idea, it wouldn't be that hard. Above is code to load them and saving would just have to find the last number (if the player didn't already exist) and add 1 to it. This can easily be done using the Val() function.
    [code]
    VAR
    S : String;
    Num : Byte;
    Error : Word;
    LastNum : Byte;

    Begin
    FindFirst('PLAYER*.SAV',Archive,DirInfo);
    While DosError = 0 Do
    Begin
    S := Copy(DirInfo.Name,7,2); { copies the two numbers out }
    Val(S, Num, Error);
    If Error <> 0 Then Break;
    If LastNum < Num Then LastNum := Num;
    FindNext(DirInfo);
    End;
    [/code]

    Something like this anyways. Either way, it doesn't metter. Just thought I'd clear it up a bit for you.

    Phat Nat

Sign In or Register to comment.