PDA

View Full Version : Trying to display a highscore file to the screen.



Konspiracy
24-05-2007, 08:59 AM
System: Windows Xp Pro/Pentium D 2.80gz - 480mb Ram
Compiler/IDE: TURBO PASCAL FOR DOS
Libraries/API: Not sure :(


I can't figure out how to write a user's score to the file, then display the file scores.txt to the screen.

I've been trying for weeks to get it to work, so any help would be greately appreciated!

thank you xx



PROGRAM PenaltyShootout (input, output);

{

Programmer: Konspiracy
Program Name: Penalty Shoot-Out
Program Version: BETA 6
Date: 03-MAY-07
}


USES CRT;
VAR
uname:string[20];
uage:integer;
shots_taken:integer;
user_score:integer;
user_choice:integer;
goal_keeper:integer;

play_again:char;
Shots_Left:integer;
Menu_Choice:integer;
fileout:text;

Procedure Details_Display;
{
This procedure is the first screen the user sees.
It logs the user's name and age
}
BEGIN
clrscr;
Writeln (' ');
Writeln ('Please enter your name: ');
readln (uname);
Writeln ('Please enter your age: ');
readln (uage);
clrscr;
END;

Procedure Shots;
{
This procedure is the actual game, it decides the goal keeper's choice
and logs the user's choice and decides on whether the ball is saved or not.
It also checks and displays how many goals you have left.
}
BEGIN
clrscr;
FOR Shots_Taken:=1 to 10
DO
BEGIN
repeat

Shots_Left:=11-Shots_Taken;
writeln ('Take a shot, you have ',Shots_Left, ' Shots Left!');
readln(user_choice);
clrscr;
if (user_choice>3) or (user_choice<1>=1) and (user_choice<=3);
goal_keeper:=RANDOM(3)+1;

IF user_choice=goal_keeper THEN
begin
writeln('The Goal Keeper Saved The Ball!');
writeln (' ');
end;

IF user_choice<goal_keeper>=5 THEN
writeln('Congratulations, ',uname, '! You scored ', user_score)
ELSE
Writeln('Unlucky, ',uname, '! You scored ', user_score, '. Try harder!');
END;

Procedure Play_Again_Proc;
{
This asks the user if they would like to play the game again, after taking
their 10 shots at the goal.
}
BEGIN
writeln(' ');
Writeln('Would you like to play again? (Y/N)');
user_score:=0;
readln(play_again);
END;

Procedure Intro_Instructions;
{
This is the instructions for the user to read from the main menu
}
BEGIN
clrscr;
Writeln (' ');
Writeln ('You have 10 penalty shots at the goal keeper.');
Writeln ('Try and score a goal!');
Writeln ('Press 1. Left');
Writeln ('Press 2. Right');
Writeln ('Press 3. Centre');
Writeln (' ');
Writeln ('Press 0. To Go Back To The Main Menu.');
readln(Menu_Choice);
END;

procedure high_scores;
{
Displays the file scores.txt to the screen, which contains all the
scores from the user
}

BEGIN
assign(fileout,'H:\scores.txt');
reset(fileout);
while not eof(fileout) do
readln(fileout, uname);
END;


Procedure Main_Menu;
{
This is the main menu where the user can select which option they want
}
BEGIN
clrscr;
Writeln('Welcome to Penalty Shoot-Out, ', uname, '.');
Writeln('Please choose from the following options:');
Writeln('1. Play the game.');
Writeln('2. Instructions.');
Writeln('3. Highscores.');
Writeln('4. Credits.');
Writeln('5. Exit.');
readln(Menu_Choice);

if Menu_Choice=1 THEN
repeat
Shots;
Score;
Play_Again_Proc;
until play_again='n';



if Menu_Choice=2 THEN
repeat
Intro_Instructions;
until Menu_Choice=0;
if Menu_Choice=0 THEN
Main_Menu;


if Menu_Choice=4 THEN
repeat
clrscr;
Writeln('Engine coded by Konspiracy');
Writeln('Tutor: John Robinson');
Writeln('Lesson: Software Development');
Writeln(' ');
Writeln('Press 0. For Main Menu');
readln(Menu_Choice);
until Menu_Choice=0;
if Menu_Choice=0 THEN
Main_Menu;


if Menu_Choice=3 THEN
repeat
high_scores;
readln(Menu_Choice);
until Menu_Choice=0;
if Menu_Choice=0 THEN
Main_Menu;



END;


BEGIN

RANDOMIZE;
Details_Display;
Main_Menu;

END.

DarknessX
24-05-2007, 10:42 AM
procedure high_scores;
{
Displays the file scores.txt to the screen, which contains all the
scores from the user
}

BEGIN
assign(fileout,'H:\scores.txt');
reset(fileout);
repeat
readln(fileout, uname);
until eof(fileout);
END;


Try that code. I don't know if it will work, but I've always had bad luck with a while not... statement for reading from files.

DarknessX
24-05-2007, 10:42 AM
procedure high_scores;
{
Displays the file scores.txt to the screen, which contains all the
scores from the user
}

BEGIN
assign(fileout,'H:\scores.txt');
reset(fileout);
x := 1;
repeat
readln(fileout, uname[x]);
x := x+1;
until eof(fileout);
x := 1;
END;

Try using that instead. Change uname into an array, and add variable x, like so:


var
uname : array[1..50] of string;
x : integer;


Now to write it to screen, simply do this:

x := 1;
repeat
writeln(uname[x]);
x := x+1;
until uname[x] = '';
x := 1;


I'm not positive it will work, but it should be good enough. You can also use Inc(x); as far as I know, but I tend to use x := x+1.

wodzu
24-05-2007, 02:51 PM
Hello :)
And where is your procedure which writes highscores to a HD?
Also this is very very bad;) :



if Menu_Choice=3 THEN
repeat
high_scores;
readln&#40;Menu_Choice&#41;;
until Menu_Choice=0;


Because your program is accessing disk all the time. You should firstly read all the data to some structure and than show your highscores.

So instead code of above do it like this:


if Menu_Choice=3 THEN
BEGIN
high_scores; //but this has to be rewritten DarknessX already showed you how to do this
repeat
readln&#40;Menu_Choice&#41;;
until Menu_Choice=0;
END;




Good luck :-)

NecroDOME
12-08-2007, 01:50 PM
Turbo pascal for dos

.... I remember the good-old times ...