Disk Image
Many early text-based computer games (BASIC Computer Games, More BASIC Computer Games, etc.) needed the ability to display information (instructions, prompts, results, etc.) to the user, accept information (commands, guesses, etc.) from the user, and usually some kind of pseudo-random number generation. This program, GUESSNUM.P, is a simple example.
String is not a predefined Pascal data type. You can declare a String Type as an array of characters. If you do so, Kyan Pascal includes some String Type handling routines including Length, Index, Substring, and Concatenate.
This program uses the LENGTH.I include file from the Kyan Pascal 2.04 Disk 2 to find the actual length of string. It is used to eliminate trailing spaces when printing strings to the screen.
This program also uses the RANDOMS.I include file from the Kyan Pascal 2.04 Utilities Disk 1 to generate a pseudo-random number between 1 and 20.
What's on the disk?
LENGTH.I - String length functionality include file
RANDOMS.I - Pseudo-random number functionality include file
GUESSNUM.P - Source code file
P.OUT - Kyan Pascal intermediate file
GUESSNUM - Executable file
Source Code
(* Guessnum.p *)
(* Kyan Pascal 2.x / Atari 8-bit *)
Program Guessnum(Input,Output);
Const
Maxstring = 20;
Type
String = Array[1..Maxstring] of Char;
Var
Complete : Boolean;
Tries : Integer;
Guess : Integer;
Number : Integer;
Guessed :Boolean;
Name : String;
#I Randoms.i
#I Length.i
(* Main Procedure *)
Begin
Write(Chr(125)); (* Clear screen *)
Tries:=0;
Number:=Random(1,20);
Guessed:=False;
Complete:=False;
Write('Hello. What is your name? ');
Readln(Name);
Writeln;
Writeln('I''m thinking of a number between');
Writeln('1 and 20. You have six chances to');
Writeln('guess the number. Good luck, ', Name:Length(Name),'.');
Writeln;
While Complete=False DO
BEGIN
Write('What is your guess? ');
Readln(Guess);
Tries:=Tries+1;
If Guess = Number Then
Begin
Guessed:=True;
Complete:=True;
End;
If Guess < Number Then
Writeln('Your guess is too low.');
If Guess > Number Then
Writeln('Your guess is too high.');
If Tries=6 Then
Complete:=True;
Writeln;
End;
If Guessed=True Then
Begin
Writeln('Great job. You guessed the number');
Writeln('in ',Tries,' tries!');
End;
If Guessed=False Then
Writeln('Sorry, you failed to guess the number ', Number,'.');
Writeln;
Write('Press <RETURN> to exit program.');
Readln;
END. (* Guessnum.p *)
No comments:
Post a Comment