perm filename CAT.SIM[SIM,SYS] blob sn#459985 filedate 1979-07-20 generic text, type T, neo UTF8
00010	OPTIONS(/l); options(/s:"libsim[106,346]");
00020	BEGIN
00030	  EXTERNAL CHARACTER PROCEDURE getch;
00040	  EXTERNAL INTEGER PROCEDURE trmop, checkreal, checkint;
00050	  EXTERNAL PROCEDURE echo, abort;
00050	  EXTERNAL CLASS vista;
00080	
00090	  vista CLASS boardvista(boardwidth, boardheight);
00100	  INTEGER boardwidth, boardheight;
00110	  BEGIN
00120	    INTEGER boardwidthm1, boardheightm1;
00130	    PROCEDURE make←board;
00140	    COMMENT
00150	    ======================================================
00160	    =======;
00170	    COMMENT: Will make the whole screen blank. If
00180	    boardheight
00190	    and boardwidth are greater than zero,
00200	    a board of this size
00210	    is written on the screen;
00220	    BEGIN
00230	      INTEGER h, v;
00240	      sysout.outimage;
     	blank←the←screen;
00290	      IF boardwidth > 0 AND boardheight > 0 THEN
00300	      BEGIN
00310	        COMMENT type screen border;
00320	        home←the←cursor;
00330	        FOR h:= -1 STEP 1 UNTIL boardwidth DO
00340	        outchar('-');
00350	        FOR v:= 1 STEP 1 UNTIL boardheight DO
00360	        BEGIN
00370	          set←char←on←screen('|',0,v);
00380	          set←char←on←screen('|',boardwidth+1,v);
00390	        END;
00400	        move←the←cursor←to(0,boardheight+1);
00410	        FOR h:= -1 STEP 1 UNTIL boardwidth DO
00420	        outchar('-');
00430	      END;
00440	      home←the←cursor; breakoutimage;
00450	    END;
00460	    boardwidthm1:= boardwidth-1;
00470	    boardheightm1:= boardheight-1;
00480	  END;
00490	
00500	  INSPECT NEW boardvista(78, 19, sysin, sysout, FALSE, 1, 20,
00510	  8) DO
00520	  BEGIN
00530	    simulation BEGIN
00540	      INTEGER numberofdeadrats, numberofsavedrats, exith,
00550	      u;
00560	      INTEGER numberofratholes;
00570	      INTEGER playtime, number←of←fillers,
00580	      sysin←strip←length;
00590	      INTEGER avoidingdistance;
00600	      REAL real←time←step, terminal←speed,
00610	      rat←intelligence, ratfactor;
00620	      TEXT blanks6;
00630	      BOOLEAN direction←ordered;
00640	      CHARACTER starter, direction;
00650	      REF(rat) therat;
00660	      REF(cat) thecat;
00670	
00680	      PROCEDURE setratfactor;
00690	      BEGIN
00700	        IF ratfactor < 0.5 THEN ratfactor:= 0.5 ELSE
00710	        IF ratfactor >= 0.96 THEN ratfactor:= 0.96;
00720	        ratfactor:= 0.005*(rat←intelligence+40);
00730	        IF numberofratholes >= 0 THEN
00740	        BEGIN
00750	          move←the←cursor←to(45,4);
00760	          outtext("Rat intelligence: ");
00770	          outfix(rat←intelligence,0,4);
00780	          home←the←cursor;
00790	        END;
00800	      END;
00810	
00820	
00830	      PROCEDURE
00840	      put←question←in←lower←left←corner(question);
00850	      VALUE question; TEXT question;
00860	      BEGIN
00870	        move←the←cursor←to(0,boardheight+8);
00880	        outtext(question);
00890	        outimage; outtext(blanks6); outimage;
00900	        outtext(blanks6);
00910	        move←the←cursor←to(0,boardheight+9);
00920	      END;
00930	
00940	      process CLASS animal;
00950	      BEGIN INTEGER v, h, vnew, hnew;
00960	      END;
00970	
00980	00990	
01000	
01010	      animal CLASS rat;
01020	      BEGIN
01030	        INTEGER i; BOOLEAN dead;
01040	
01050	        INTEGER PROCEDURE clever←gives←positive;
01060	        clever←gives←positive:= IF draw(ratfactor,
01070	        u) THEN 1 ELSE -1;
01080	
01090	        jumpin:
01100	        h:= randint(1,boardwidth,u);
01110	        v:= randint(1,boardheight,u);
01120	        IF get←char←from←screen(h,
01130	        v) NE ' ' THEN GOTO jumpin;
01140	        WHILE NOT dead DO
01150	        BEGIN
01160	          set←char←on←screen(' ',h,v);
01170	          IF h = 0 OR h > boardwidth THEN
01180	          BEGIN
01190	            rat←intelligence:= rat←intelligence-42;
01200	            setratfactor;
01210	            numberofsavedrats:= numberofsavedrats+1;
01220	            set←char←on←screen('o',numberofsavedrats,
01230	            boardheight+5);
01240	            thecat.eat:= 1; GOTO out;
01250	          END;
01260	          IF h = 1 AND v = exith AND numberofratholes >=
01270	          1 THEN h:= h-1 ELSE
01280	          IF h = boardwidth AND v = exith AND
01290	          numberofratholes >= 2
01300	          THEN h:= h+1 ELSE
01310	          BEGIN
01320	            hnew:= h + clever←gives←positive
01330	            * (IF numberofratholes <= 0 THEN
01340	            (IF h > boardwidth//2 THEN -1 ELSE 1)
01350	            ELSE IF numberofratholes <= 1 THEN -1
01360	            ELSE IF h > thecat.h THEN 1 ELSE IF h <
01370	            thecat.h THEN -1
01380	            ELSE ((IF h > boardwidth//2 THEN 1 ELSE -1)
01390	            * (IF h = 1 OR h = boardwidth THEN -1 ELSE
01400	            1)));
01410	            IF abs(v-thecat.v)+abs(h-thecat.h) <=
01420	            avoidingdistance THEN
01430	            vnew:= v + clever←gives←positive
01440	            * (IF v > thecat.v THEN 1 ELSE -1)
01450	            ELSE
01460	            vnew:= v + clever←gives←positive
01470	            * (IF v > exith THEN -1 ELSE 1);
01480	            IF get←char←from←screen(hnew, vnew) = ' ' THEN
01490	            BEGIN
01500	              h:= hnew; v:= vnew;
01510	            END ELSE
01520	            IF get←char←from←screen(hnew,vnew) = '|' THEN
01530	            BEGIN
01540	              hnew:= h;
01550	              IF get←char←from←screen(hnew,
01560	              vnew) = ' ' THEN
01570	              v:= vnew;
01580	            END ELSE
01590	            BEGIN
01600	              vnew:= v;
01610	              IF get←char←from←screen(hnew,
01620	              vnew) = ' ' THEN
01630	              h:= hnew;
01640	            END;
01650	
01660	          END;
01670	          set←char←on←screen('o',h,v);
01680	          IF h >= 0 THEN hold(1.0);
01690	          cause←real←time←delay((IF direction←ordered
01700	          THEN 2 ELSE 1)
01710	          *number←of←fillers);
01720	        END;
01730	        out:
01740	        ACTIVATE thecat DELAY 0;
01750	      END rat;
01760	
01770	01780	
01790	
01800	      animal CLASS cat;
01810	      BEGIN
01820	        INTEGER eat;
01830	        v:= boardheight/2; h:= boardwidth/2;
01840	        GOTO jump;
01850	        WHILE TRUE DO
01860	        BEGIN
01870	          set←char←on←screen(' ',h,v);
01880	          jump:
01890	          IF (IF direction←ordered THEN direction = up OR
01900	          direction = down
01910	          ELSE draw(0.5,u)) THEN
01920	          BEGIN COMMENT move vertically;
01930	            IF (IF direction←ordered THEN direction = up
01940	            ELSE v > therat.v)
01950	            THEN vnew:= v-1 ELSE vnew:= v+1;
01960	            hnew:= h;
01970	          END ELSE
01980	          BEGIN COMMENT move horizontally;
01990	            IF (IF direction←ordered THEN direction =
02000	            left ELSE h > therat.h)
02010	            THEN hnew:= h-1 ELSE hnew:= h+1;
02020	            vnew:= v;
02030	          END;
02040	          IF get←char←from←screen(hnew,vnew) = '-'
02050	          OR get←char←from←screen(hnew,vnew) = '|'
02060	          OR hnew = 0 OR hnew = boardwidth+1 THEN
02070	          BEGIN
02080	            IF NOT direction←ordered THEN GOTO jump
02090	            ELSE
02100	            BEGIN
02110	              hnew:= h; vnew:= v;
02120	            END;
02130	          END;
02140	
02150	          h:= hnew; v:= vnew;
02160	          IF get←char←from←screen(h,v) = 'o' THEN
02170	          BEGIN
02180	            therat.dead:= TRUE;
02190	            numberofdeadrats:= numberofdeadrats+1;
02200	            eat:= 1;
02210	            rat←intelligence:= rat←intelligence+25;
02220	            setratfactor;
02230	            set←char←on←screen(startblink,boardwidth+7,0);
02240	            set←char←on←screen('o',
02250	            boardwidth+8+mod(numberofdeadrats-1,10),
02260	            1+((numberofdeadrats-1)//10));
02270	          END;
02280	          set←char←on←screen('&',h,v);
02290	          hold(1.0);
02300	          IF NOT direction←ordered THEN
02310	          cause←real←time←delay(number←of←fillers);
02320	          IF eat > 0 THEN
02330	          BEGIN
02340	            eat:= eat-1; IF eat = 0 THEN
02350	            BEGIN
02360	              therat:- NEW rat; ACTIVATE therat DELAY 0.5;
02370	              set←char←on←screen(' ',boardwidth+7,0);
02380	            END;
02390	          END ELSE
02400	          BEGIN
02410	          END;
02420	        END;
02430	      END cat;
02440	02450	
02460	
02470	      blanks6:- blanks(6);
02480	      INSPECT sysout DO INSPECT sysin DO
02490	      BEGIN
02500	        cancel←display;
02510	        outtext("Answer RETURN to get default values");
     	        outtext(" on preliminary questions!");
02540	        outimage; outimage;
02550	        numberofratholes:= -1;
02560	        outtext("Input start random integer: ");
02570	        breakoutimage; inimage;
02580	        u:= IF sysin.image.strip.length = 0 THEN 87697
02590	        ELSE inint;
02600	        outtext(
02610	        "Input initial rat intelligence quotient: ");
02620	        breakoutimage; inimage;
02630	        rat←intelligence:= IF sysin.image.strip.length =
02640	        0 THEN 80 ELSE inreal;
02650	        outtext(
02660	        "Input distance at which rat is afraid of cat: ");
02670	        breakoutimage; inimage;
02680	        avoidingdistance:= IF sysin.image.strip.length =
02690	        0 THEN 2 ELSE inint;
02700	        outtext("Input number of ratholes, 0, 1 or 2: ");
02710	        breakoutimage; inimage;
02720	        numberofratholes:= IF sysin.image.strip.length =
02730	        0 THEN 2 ELSE inint;
02740	        outtext(
02750	        "Input wanted real step time in seconds: ");
02760	        breakoutimage; inimage;
02770	        real←time←step:= IF sysin.image.strip.length = 0
02780	        THEN 0.15 ELSE inreal;
02790	        outtext(
02800	        "Input terminal speed in characters/second: ");
02810	        breakoutimage; inimage;
02820	        terminal←speed:= IF sysin.image.strip.length = 0
02830	        THEN 240 ELSE inreal;
02840	        number←of←fillers:= real←time←step*terminal←speed;
02850	        resume←display;
02860	      END;
02870	
02880	      make←board;
02890	      exith:= boardheight//2+1;
02900	      IF numberofratholes > 0 THEN set←char←on←screen(' ',
02910	      0,exith);
02920	      IF numberofratholes > 1 THEN set←char←on←screen(' ',
02930	      boardwidth+1,exith);
02940	      home←the←cursor;
02950	      move←the←cursor←to(boardwidth+8,0);
02960	      outtext("Dead rats:"); outchar(stopblink);
02970	      setratfactor;
02980	      move←the←cursor←to(0,boardheight+3);
02990	      outtext("Safe rats:");
03000	      move←the←cursor←to(0,boardheight+5);
03010	      outchar(startblink);
03020	      move←the←cursor←to(75,boardheight+5);
03030	      outchar(stopblink);
03040	      home←the←cursor;
03050	      thecat:- NEW cat; ACTIVATE thecat DELAY 0.5;
03060	      therat:- NEW rat; ACTIVATE therat;
03070	      hold(1.25);
03080	      WHILE TRUE DO
03090	      BEGIN
03100	        randomquestion:
03110	        put←question←in←lower←left←corner
03120	("Do you want random cat walk?                    ");
03130	        IF inyes THEN
03140	        BEGIN
03150	          put←question←in←lower←left←corner
03160	("Give number of play steps:                     ");
03170	          playtime:= inint;
03180	          IF playtime < 0 THEN
03190	          BEGIN
03200	            playtime:= -playtime;
03210	            restore←the←whole←screen;
03220	          END;
03230	          hold(playtime);
03240	        END ELSE COMMENT ordered cat walk;
03250	        BEGIN
03260	          put←question←in←lower←left←corner
03270	("Give direction of cat movement:                      ");
03280	          WHILE TRUE DO
03290	          BEGIN
03300	            getdirection:
     	             move←the←cursor←to(thecat.h,thecat.v); breakoutimage;
03330	            direction:= insingle(FALSE);
03350	            direction←ordered:= direction = up OR
03360	            direction = down
03370	            OR direction = left OR direction = right;
03380	            IF NOT direction←ordered THEN
03390	            BEGIN
03400	              IF direction = '!' THEN GOTO exit ELSE
03410	              IF direction = altmode THEN GOTO
03420	              randomquestion
03430	              ELSE
03440	              BEGIN
03450	                restore←the←whole←screen;
03460	                GOTO getdirection;
03470	              END;
03480	            END;
03490	            hold(1.0);
03500	          END;
03510	        END;
03520	      END;
03530	
03540	
03550	    END comment simulation;
03560	  END comment inspect vista;
03570	  exit:
03580	END;