home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / PROGRAMS / MONSTERS.PRO < prev    next >
Encoding:
Text File  |  1990-03-26  |  8.8 KB  |  248 lines

  1. /*
  2.   Copyright (c) 1986, 90 by Prolog Developmment Center
  3. */
  4. CONSTANTS
  5.   monster_speed = 125                 % smaller number faster speed (3 min)
  6.   max_num_shots = 6
  7.    
  8. DOMAINS
  9.   integerlist = integer*
  10.         final = won ; lost
  11.     
  12. DATABASE 
  13.    dead_monster ( integer, integer )   % ( row, column )
  14.    monster ( integer, integer )        % ( row, column )
  15.    shot ( integer, integer )           % ( row, column )
  16.    DETERM spaceship ( integer )        % ( column )
  17.        
  18. PREDICATES
  19.    another_game 
  20.    bindkey ( char, char )
  21.    check_monster ( integer, integer )
  22.    clear_dead_monsters ( integer )
  23.    clearkeys
  24.    create_em ( integer )
  25.    display_aliens 
  26.    end_game ( final )
  27.    length ( integer, integerlist )
  28.  
  29.    make_aliens
  30.    monster_direction ( integer, integer, integer )
  31.    move_monsters ( integer )
  32.    move_ship ( char, integer )
  33.    move_shots
  34.    my_inkey ( char )    
  35.    my_retract ( dbasedom )
  36.  
  37.    number_shots 
  38.    play
  39.    randcol ( integer )
  40.    NONDETERM repeat
  41.    row_member ( integer, integerlist )
  42.    run
  43.    test1
  44.    test_ans ( string )
  45.    testresult 
  46.  
  47. GOAL
  48.     makewindow(1,12,0,"",0,0,25,80),
  49.     run.
  50.  
  51. CLAUSES
  52.   run :-
  53.       write("\n Use the following keys :" ,
  54.             "\n                          Left Arrow = Move Left" ,
  55.             "\n                         Right Arrow = Move Right" ,
  56.             "\n                          Down Arrow = Transport" ,
  57.             "\n                            Spacebar = Fire Shot\n\n") ,
  58.       repeat ,
  59.       retractall( spaceship(_) ), retractall( monster(_,_) ) ,
  60.       retractall( shot(_,_) ) ,
  61.       asserta( spaceship(40) ) ,
  62.       make_aliens,
  63.       play, !.
  64.  
  65.   make_aliens :-
  66.         write("How many monsters do you want to fight? "),
  67.         readint(Clones) ,
  68.         create_em(Clones) ,
  69.         clearwindow ,
  70.         display_aliens.
  71.  
  72.   create_em(0) :- !.
  73.   create_em(Clones) :- More_clones = Clones - 1 ,
  74.            random(75, Col_1), Col = Col_1 +2 ,
  75.            random(4, Row_1), Row = Row_1 + 1 ,
  76.            assertz( monster(Row,Col) ) , 
  77.            create_em(More_clones).
  78.  
  79.   display_aliens :- monster(Row, Col) ,
  80.                     scr_char(Row, Col, '\157') ,
  81.                     fail ;
  82.                     spaceship(Col), ! ,
  83.                     scr_char(24,Col,'\234').
  84.  
  85. /* MAIN LOOP */
  86.   play :- 
  87.         testresult,                       % test to see if game is over
  88.         my_inkey(Ch) ,                    % get direction to move 
  89.         clearkeys ,
  90.         move_shots ,
  91.         spaceship(Col) ,
  92.         move_ship(Ch, Col) ,
  93.         move_monsters(5), ! ,
  94.         play.
  95.  
  96.   testresult :- monster(_,_), ! ,                     % Any monster left?
  97.                 test1 ;
  98.                 end_game(won).
  99.  
  100.   test1 :- findall(Row, monster(Row,_), Rows) ,
  101.            not( row_member(24, Rows) ), ! ;
  102.         end_game(lost).
  103.                                   
  104. % Screen Wrap Ship
  105.   move_ship('\75',1):- !, scr_char(24,1,' ') ,    
  106.                        scr_char(24,79,'\234') ,
  107.                        asserta( spaceship(79) ).
  108.   move_ship('\77',79):- !, scr_char(24,79,' ') ,  
  109.                         scr_char(24,1,'\234') ,
  110.                         asserta( spaceship(1) ).
  111.   move_ship('\80',Col):- !, randcol(Rand_col),  % tele-port to a random spot 
  112.                          scr_char(24,Col,' ') ,
  113.                          scr_char(24,Rand_col,'\234') ,
  114.                          asserta( spaceship(Rand_col) ).
  115.  
  116. % Move the Ship left or right
  117.   move_ship('\75',OldCol) :- !, NewCol=OldCol-1 ,
  118.                              scr_char(24,OldCol,' ') ,
  119.                              scr_char(24,NewCol,'\234') ,
  120.                              asserta( spaceship(NewCol) ).
  121.   move_ship('\77',OldCol) :- !, NewCol=OldCol+1 ,
  122.                              scr_char(24,OldCol,' ') ,
  123.                              scr_char(24,NewCol,'\234') ,
  124.                              asserta( spaceship(NewCol) ).
  125. % FIRE SHOT!
  126.   move_ship('\32',_) :- 
  127.           number_shots ,
  128.           spaceship(Col) ,
  129.           assertz( shot(23,Col) ), 
  130.           scr_char(23, Col, '\24') ,
  131.           check_monster( 23, Col),!.
  132.   move_ship(_,_).         % Anything else, stay put.
  133.  
  134.   move_monsters(0) :- !.
  135.   move_monsters(Num)  :- monster(Row, Col) ,
  136.                          random(monster_speed, Dir) ,
  137.                          monster_direction(Dir, Row, Col) ,
  138.                          Num_1 = Num - 1, ! ,
  139.                          move_monsters(Num_1) ; true.
  140.                     
  141.   monster_direction(1,R,1) :- ! ,                  
  142.                               scr_char(R,1,' ') ,
  143.                               my_retract( monster(R,1) ) ,
  144.                               assertz( monster(R,79) ) ,
  145.                               scr_char(R,79,'\157').
  146.   monster_direction(2,R,79) :- ! ,                  
  147.                               scr_char(R,79,' ') ,
  148.                               my_retract( monster(R,79) ) ,
  149.                               assertz( monster(R,1) ) ,
  150.                               scr_char(R,1,'\157').
  151.   monster_direction(0,R,C) :- ! ,                  
  152.                               scr_char(R,C,' ') ,
  153.                               R1 = R + 1 ,
  154.                               my_retract( monster(R,C) ) ,
  155.                               assertz( monster(R1,C) ) ,
  156.                               scr_char(R1,C,'\157') ,
  157.                               clear_dead_monsters(3).
  158.                               
  159.   monster_direction(1,R,C) :- ! ,                  
  160.                               scr_char(R,C,' ') ,
  161.                               C1 = C - 1 ,
  162.                               my_retract( monster(R,C) ) ,
  163.                               assertz( monster(R,C1) ) ,
  164.                               scr_char(R,C1,'\157').
  165.   monster_direction(2,R,C) :- ! ,                  
  166.                               scr_char(R,C,' ') ,
  167.                               C1 = C + 1 ,
  168.                               my_retract( monster(R,C) ) ,
  169.                               assertz( monster(R,C1) ) ,
  170.                               scr_char(R,C1,'\157').
  171.   monster_direction(_,_,_).   % Anything else, stay put!
  172.  
  173.   move_shots :- shot(Row, Col) ,
  174.                 Row > 0 ,
  175.                 Row1 = Row - 1 ,
  176.                 my_retract( shot(Row, Col) ) ,
  177.                 asserta( shot(Row1, Col) ) ,
  178.                 check_monster(Row1, Col), 
  179.                 scr_char(Row,Col,' ') ,
  180.                 scr_char(Row1,Col,'\24'), fail ;
  181.              shot(0,Col) ,
  182.              scr_char(0,Col,' '), 
  183.              my_retract( shot(0,COL) ), fail ; 
  184.           true.
  185.                
  186.   number_shots :- findall(X, shot(X,_), Shots) ,
  187.                   length(Num, Shots) ,
  188.                   Num < max_num_shots + 1 , 
  189.                   ! ; sound(3,440), fail.
  190.  
  191.   check_monster(Row, Col) :- monster(Row, Col), ! ,
  192.                              Row1 = Row + 1 ,
  193.                              scr_char(Row1, Col, ' ') ,
  194.                              scr_char(Row, Col, '\15') ,
  195.                              my_retract( monster(Row, Col) ) ,
  196.                              assertz( dead_monster(Row, Col) ) ,
  197.                              my_retract( shot(Row, Col) ) ,
  198.                              fail ; true.
  199.  
  200.   clear_dead_monsters(0) :- !.
  201.   clear_dead_monsters(X) :- retract( dead_monster(R,C) ) ,
  202.                             scr_char(R,C,' ') ,
  203.                             X1 = X - 1, ! ,
  204.                             clear_dead_monsters(X1) ; true.
  205.  
  206.   end_game(won) :- !, makewindow(7,14,3," WINNER ",8,20,10,40) ,
  207.                    write("\n Well done, Champion Zapper !\n") ,
  208.                    another_game.
  209.  
  210.   end_game(lost) :- makewindow(13,14,10," Looser ",8,20,10,40) ,
  211.                     write("\n Too late, YOU have been zapped !\n") ,
  212.                     another_game.
  213.   
  214.   another_game :- write("\n Do you wish to play another game?" ,
  215.                         "\n   (enter 'Y' to play another game" ,
  216.                         "\n       or 'Q' to quit) : ") ,
  217.                   repeat ,
  218.                   readchar(Ans) ,
  219.                   str_char(Ans1, Ans) ,
  220.                   upper_lower(Ans1, Ans2) ,
  221.                   test_ans(Ans2), !, fail.
  222.                
  223.    test_ans(Ans) :- Ans = "q" , ! , exit ;
  224.                     Ans = "y", removewindow, clearwindow.
  225.  
  226. /* * * * * * * * * * * * *
  227.    AUXILIARY PREDICATES
  228. * * * * * * * * * * * * */       
  229.   bindkey(C,C1) :- C = '\0',inkey(C1),! ; C1 = C.
  230.     
  231.   clearkeys :- inkey(_), !, clearkeys ; !.
  232.   
  233.   length(0,[]) :- !.
  234.   length(Num, [_|T]) :- length(Num1, T) ,
  235.                         Num = Num1 + 1.
  236.                         
  237.   my_inkey(C) :- inkey(C1),bindkey(C1,C),!.
  238.   my_inkey('\1').
  239.    
  240.   my_retract(X) :- retract(X),!.
  241.         
  242.   randcol(Col) :- random(77,X), Col = X + 2.
  243.  
  244.   repeat.
  245.   repeat:-repeat.
  246.    
  247.   row_member(X,[H|T]) :- X = H, ! ; row_member(X,T).
  248.