ack/examples/hilo.mod

105 lines
1.8 KiB
Modula-2
Raw Normal View History

2007-02-25 00:30:07 +00:00
(* $Source$
* $State$
* $Revision$
*)
MODULE HiLo;
FROM InOut IMPORT WriteInt, WriteLn, WriteString, ReadString, ReadInt;
FROM random IMPORT Uniform;
FROM Streams IMPORT FlushStream, OutputStream, StreamResult;
2007-02-25 00:30:07 +00:00
VAR
buffer : ARRAY [0..32] OF CHAR;
PROCEDURE flush;
VAR
strus : StreamResult;
BEGIN
FlushStream(OutputStream, strus);
END flush;
2007-02-25 00:30:07 +00:00
PROCEDURE reads;
BEGIN
WriteString("> ");
flush;
2007-02-25 00:30:07 +00:00
ReadString(buffer);
END reads;
PROCEDURE game;
VAR
Number : INTEGER;
Attempts : INTEGER;
guess : INTEGER;
finished : BOOLEAN;
BEGIN
WriteString("See if you can guess my number.");
WriteLn;
Number := Uniform(0, 99);
Attempts := 1;
finished := FALSE;
WHILE NOT finished DO
WriteString("> ");
flush;
2007-02-25 00:30:07 +00:00
ReadInt(guess);
IF guess = Number THEN
WriteLn;
WriteString("You got it right in only ");
WriteInt(Attempts, 0);
WriteString(" ");
IF Attempts = 1 THEN
WriteString("go");
ELSE
WriteString("goes");
END;
WriteString("!");
WriteLn;
finished := TRUE;
ELSIF guess < Number THEN
WriteLn;
WriteString("Try a bit higher.");
WriteLn;
ELSIF guess > Number THEN
WriteLn;
WriteString("Try a bit lower.");
WriteLn;
END;
Attempts := Attempts + 1;
END;
END game;
VAR
finished : BOOLEAN;
BEGIN
WriteLn;
WriteString("Hi there! I'm written in Modula-2. Before we start, what is your name?");
2007-02-25 00:30:07 +00:00
WriteLn;
reads;
WriteLn;
WriteString("Hello, ");
WriteString(buffer);
WriteString("! ");
finished := FALSE;
WHILE NOT finished DO
game;
WriteLn;
WriteString("Would you like another go?");
WriteLn;
reads;
IF (buffer[0] = 'n') OR (buffer[0] = 'N') THEN
finished := TRUE;
WriteLn;
WriteString("Thanks for playing --- goodbye!");
WriteLn;
ELSE
WriteLn;
WriteString("Excellent! ");
END;
END;
END HiLo.