home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / mbug / mbug013.arc / ANIMAL.PAS < prev    next >
Pascal/Delphi Source File  |  1979-12-31  |  10KB  |  363 lines

  1. program ANIMAL;
  2.  
  3. {               Version for Turbo Pascal
  4.    Developed, with major modifications, by Bob Burt from
  5.   a Pascal/Z source written for the Pascal/Z Users Group
  6.      (Volume 26) by Roy Allen of Mill Valley, CA.
  7.  
  8.    Note that the Turbo version compiles to a 14k .COM file,
  9.   compared with 26k for the Pascal/Z version.  Additionally,
  10.    the Turbo version allows a LISTing of the known animals,
  11.     and will print out the data file format and contents.
  12.  
  13.   This is a useful demonstration program to assist newcomers
  14.    to Turbo Pascal to learn how to structure and manipulate
  15.                      data files.                              }
  16.  
  17. const
  18.   inviter   = 'Would you like to play the animal guessing game';
  19.   insulted  = 'Well, exCUUUUSE ME! !  So you don''t want to play, huh?';
  20.   start1    = 'You think of an animal, and I''ll try to guess what it is';
  21.   start2    = 'When you''re ready to begin, press <RETURN>';
  22.   askagain  = 'Would you like to play another round';
  23.   want_list = 'Do you want a list of the animals I now know';
  24.   see_file  = 'Do you want to see how the file is constructed';
  25.   continue  = 'Press any key to continue';
  26.   vowels    : set of char = ['A','E','I','O','U','a','e','i','o','u'];
  27.   shiftup   : integer = -32; { ord('A') - ord(a') }
  28. type
  29.   string128 = string[128];
  30.   string24  = string[24];
  31.   animal_record = record
  32.                     name_or_question : string128;
  33.                     yes_node,no_node : integer
  34.                   end;
  35. var
  36.   animal_data : file of animal_record;
  37.   animal_rec  : animal_record;
  38.   count,animal_count : integer;
  39.   answer : integer;
  40.   OK,play_game,match,found : boolean;
  41.   ch : char;
  42.   query : string128;
  43.   old_animal : string24;
  44.  
  45. {$I getyes.fun}
  46. {$I explain.pro}
  47.  
  48. procedure guessing; forward;
  49.  
  50. procedure starter_file;
  51. begin
  52.   assign(animal_data,'animal.dat');
  53.   {$I-} reset(animal_data) {$I+};
  54.   OK := (ioresult = 0);
  55.   if not OK then
  56.     begin
  57.       rewrite(animal_data);
  58.       seek(animal_data,0);
  59.       with animal_rec do
  60.       begin
  61.         name_or_question := 'Does it live in the water';
  62.         yes_node := 1; no_node := 2
  63.       end;
  64.       write(animal_data,animal_rec);
  65.       seek(animal_data,1);
  66.       with animal_rec do
  67.       begin
  68.         name_or_question := 'octopus';
  69.         yes_node := 0; no_node := 0;
  70.       end;
  71.       write(animal_data,animal_rec);
  72.       seek(animal_data,2);
  73.       with animal_rec do
  74.       begin
  75.         name_or_question := 'moose';
  76.         yes_node := 0; no_node := 0;
  77.       end;
  78.       write(animal_data,animal_rec)
  79.     end; {if not OK}
  80.     close(animal_data)
  81. end; {procedure starter_file}
  82.  
  83. procedure extract_file;
  84. begin
  85.   animal_count := 0;
  86.   writeln;
  87.   assign(animal_data,'animal.dat');
  88.   reset(animal_data);
  89.   for count := 0 to filesize(animal_data) - 1 do
  90.     begin
  91.       seek(animal_data,count);
  92.       read(animal_data,animal_rec);
  93.       with animal_rec do
  94.       begin
  95.         write('Animal Record No.',count,' contains :');
  96.         writeln(name_or_question,' Nodes : ',yes_node,',',no_node);
  97.         if yes_node = 0 then animal_count := animal_count + 1
  98.       end; {with}
  99.     end; {for count}
  100.     writeln;
  101.     writeln('Total number of animals I know is : ',animal_count);
  102.     close(animal_data)
  103. end; {procedure extract_file}
  104.  
  105. procedure list_animals;
  106. var
  107.   x,y : integer;
  108. begin
  109.   y := 2;
  110.   animal_count := 0;
  111.   clrscr;
  112.   writeln('Animals I know :');
  113.   assign(animal_data,'animal.dat');
  114.   reset(animal_data);
  115.   for count := 0 to filesize(animal_data) - 1 do
  116.     begin
  117.       seek(animal_data,count);
  118.       read(animal_data,animal_rec);
  119.       with animal_rec do
  120.         if yes_node = 0 then
  121.         begin
  122.           if (animal_count mod 6 = 0)
  123.             then begin
  124.                    x := -12; y := y + 1;
  125.                  end;
  126.           x := x + 13;
  127.           gotoxy(x,y);
  128.           write(name_or_question);
  129.           animal_count := animal_count + 1
  130.         end {if}
  131.     end; {for count}
  132.     close(animal_data);
  133.     writeln; writeln;
  134.     writeln('Thus I know a total of ',animal_count,' animals')
  135. end; {procedure list_animals}
  136.  
  137. procedure animal_total;
  138. begin
  139.   animal_count := 0;
  140.   assign(animal_data,'animal.dat');
  141.   reset(animal_data);
  142.   for count := 0 to filesize(animal_data) - 1 do
  143.   begin
  144.     seek(animal_data,count);
  145.     read(animal_data,animal_rec);
  146.     with animal_rec do
  147.       if yes_node = 0 then
  148.         animal_count := animal_count + 1
  149.   end; {for count}
  150.   close(animal_data)
  151. end; {procedure animal_total}
  152.  
  153. function voweler(noun : string24) : string24;
  154. var
  155.   holder : string24;
  156. begin
  157.   if noun[1] in vowels
  158.     then holder := ' an '
  159.     else holder := ' a ';
  160.     voweler := holder + noun
  161. end; {voweler function}
  162.  
  163. procedure right_guess;
  164. const
  165.   boast = 'How about that - - - I WON!';
  166. begin
  167.   writeln;
  168.   writeln(^G,boast);
  169.   delay(2000);
  170.   close(animal_data)
  171. end; {procedure right_guess}
  172.  
  173. procedure depun(var dtext : string24);
  174. begin
  175.   repeat
  176.     if (dtext[length(dtext)] = '.')
  177.     or (dtext[length(dtext)] = ',')
  178.     or (dtext[length(dtext)] = '!')
  179.     or (dtext[length(dtext)] = '?')
  180.     or (dtext[length(dtext)] = ' ') then
  181.       dtext := copy(dtext,1,length(dtext)-1)
  182.   until (dtext[length(dtext)] in ['a'..'z'])
  183.      or (dtext[length(dtext)] in ['A'..'Z'])
  184. end; {procedure depun}
  185.  
  186. procedure lower(var dtext : string24);
  187. var
  188.  i : integer;
  189.  letter : char;
  190.  sloppy : boolean;
  191. begin
  192.   sloppy := true;
  193.   for i := 1 to 4 do
  194.     if i <= length(dtext)
  195.       then if dtext[i] in ['a'..'z']
  196.         then sloppy := false;
  197.   if sloppy
  198.     then for i := 1 to length(dtext) do
  199.       begin
  200.         letter := dtext[i];
  201.         if ((letter >= 'A') and (letter <= 'Z'))
  202.           then dtext[i] := chr(ord(letter) - shiftup)
  203.       end
  204. end; {procedure lower}
  205.  
  206. procedure markout(var btext : string24);
  207. begin
  208.   while btext[1] = ' ' do
  209.     btext := copy(btext,2,length(btext));
  210.   if  (btext[1] = 'a')
  211.   and (btext[2] = ' ') then
  212.     btext := copy(btext,3,length(btext));
  213.   if  (btext[1] = 'a')
  214.   and (btext[2] = 'n')
  215.   and (btext[3] = ' ') then
  216.     btext := copy(btext,4,length(btext));
  217.   while btext[1] = ' ' do
  218.     btext := copy(btext,2,length(btext))
  219. end; {procedure markout}
  220.  
  221. procedure learning;
  222. const
  223.   puzzled  = 'Really?  What sort of animal is it then?';
  224.   humbler  = 'Oh!  I didn''t know about';
  225.   request1 = 'I''d like to know more about animals.';
  226.   request2 = 'What''s a yes-or-know question';
  227.   request3 = 'to discriminate between';
  228.   clarify1 = 'Which answer to that question would mean';
  229.   clarify2 = ' - yes or no';
  230.   thanks   = 'Thank you!  Now I know ';
  231. var
  232.   yes_answer : boolean;
  233.   holder : string24;
  234.  
  235. begin {procedure learning}
  236.   writeln(puzzled);
  237.   readln(holder);
  238.   depun(holder);
  239.   lower(holder);
  240.   markout(holder);
  241.   writeln(humbler,voweler(holder),'.');
  242.   writeln(request1);
  243.   writeln(request2);
  244.   write(request3,voweler(holder),' and');
  245.   writeln(voweler(animal_rec.name_or_question),'?');
  246.   readln(query);
  247.   write(clarify1,voweler(holder));
  248.   write(clarify2);
  249.   old_animal := animal_rec.name_or_question;
  250.   yes_answer := getyes;
  251.   seek(animal_data,answer);
  252.   with animal_rec do
  253.     begin
  254.       name_or_question := query;
  255.       if yes_answer
  256.       then begin
  257.              yes_node := filesize(animal_data);
  258.              no_node  := filesize(animal_data) + 1
  259.            end
  260.       else begin
  261.              yes_node := filesize(animal_data) + 1;
  262.              no_node  := filesize(animal_data)
  263.            end
  264.     end; {with animal_rec}
  265.   write(animal_data,animal_rec);
  266.   seek(animal_data,filesize(animal_data));
  267.   with animal_rec do
  268.     begin
  269.       name_or_question := holder;
  270.       yes_node := 0; no_node := 0
  271.     end; {with animal_rec}
  272.   write(animal_data,animal_rec);
  273.   seek(animal_data,filesize(animal_data)); {new filesize after write}
  274.   with animal_rec do
  275.     begin
  276.       name_or_question := old_animal;
  277.       yes_node := 0; no_node := 0
  278.     end; {with animal_rec}
  279.   write(animal_data,animal_rec);
  280.   close(animal_data);
  281.   animal_total;
  282.   write(thanks,animal_count:1,' animals')
  283. end; {procedure learning}
  284.  
  285. procedure animal_guess;
  286. begin
  287.   found := false;
  288.   seek(animal_data,answer);
  289.   read(animal_data,animal_rec);
  290.   write('Is it',voweler(animal_rec.name_or_question));
  291.   OK := getyes;
  292.   if OK
  293.     then begin
  294.            found := true;
  295.            right_guess
  296.          end;
  297.   if (not found) and (not OK)
  298.     then begin
  299.            found := true;
  300.            learning
  301.          end
  302. end; {procedure animal_guess}
  303.  
  304. procedure next_node;
  305. begin
  306.   seek(animal_data,answer);
  307.   read(animal_data,animal_rec);
  308.   with animal_rec do
  309.     begin
  310.       write(name_or_question);
  311.       OK := getyes;
  312.       if OK then answer := yes_node;
  313.       if not OK then answer := no_node
  314.     end;
  315.   guessing
  316. end; {procedure next_node}
  317.  
  318. procedure guessing;
  319. begin
  320.   seek(animal_data,answer);
  321.   read(animal_data,animal_rec);
  322.   if animal_rec.yes_node = 0
  323.     then animal_guess
  324.     else next_node
  325. end; {procedure guessing}
  326.  
  327. begin {program animal}
  328.   clrscr; starter_file;
  329.   animal_total; explain;
  330.   write(inviter);
  331.   play_game := getyes;
  332.   if not play_game then
  333.     begin
  334.       writeln; writeln(insulted)
  335.     end; {if not play_game}
  336.   while play_game do
  337.     begin {while play_game}
  338.       clrscr; answer := 0;
  339.       writeln(start1); writeln(start2);
  340.       readln(ch);
  341.       assign(animal_data,'animal.dat');
  342.       reset(animal_data);
  343.       guessing; writeln;
  344.       write(askagain); play_game := getyes
  345.     end; {while play_game}
  346.   writeln; write(want_list);
  347.   OK := getyes;
  348.   if OK then
  349.     begin
  350.       list_animals; writeln;
  351.       write(continue); repeat until keypressed;
  352.     end; {if OK}
  353.   writeln; write(see_file);
  354.   OK := getyes;
  355.   if OK then
  356.     begin
  357.       clrscr; extract_file;
  358.       write(continue); repeat until keypressed;
  359.     end; {if OK}
  360.   writeln; writeln('Okay!  Goodbye!')
  361. end. {program animal}
  362.  
  363.