Advertisement
symonhasan

Decision Game

Nov 21st, 2019
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 3.27 KB | None | 0 0
  1. % Author:
  2. % Date: 11/15/2015
  3.  
  4. /* animal identification game.
  5.  
  6.     start with ?- go.     */
  7.  
  8. go :- hypothesize(Animal),
  9.       write('I guess that the animal is: '),
  10.       write(Animal),
  11.       nl,
  12.       undo.
  13.  
  14. /* hypotheses to be tested */
  15. hypothesize( frog ) :- frog , !.
  16. hypothesize( caecilian  ) :- caecilian , !.
  17. hypothesize( snake ) :- snake , !.
  18. hypothesize( alligator ) :- alligator , !.
  19. hypothesize(cheetah)   :- cheetah, !.
  20. hypothesize(tiger)     :- tiger, !.
  21. hypothesize(giraffe)   :- giraffe, !.
  22. hypothesize(zebra)     :- zebra, !.
  23. hypothesize(ostrich)   :- ostrich, !.
  24. hypothesize(penguin)   :- penguin, !.
  25. hypothesize(albatross) :- albatross, !.
  26. hypothesize(hilsha)     :- hilsha , !.
  27. hypothesize(unknown).             /* no diagnosis */
  28.  
  29. /* animal identification rules */
  30. cheetah :- mammal,
  31.            carnivore,
  32.            verify(has_tawny_color),
  33.            verify(has_dark_spots).
  34. tiger :- mammal,
  35.          carnivore,
  36.          verify(has_tawny_color),
  37.          verify(has_black_stripes).
  38. giraffe :- ungulate,
  39.            verify(has_long_neck),
  40.            verify(has_long_legs).
  41. zebra :- ungulate,
  42.          verify(has_black_stripes).
  43.  
  44. ostrich :- bird,
  45.            verify(does_not_fly),
  46.            verify(has_long_neck).
  47. penguin :- bird,
  48.            verify(does_not_fly),
  49.            verify(swims),
  50.            verify(is_black_and_white).
  51. albatross :- bird,
  52.              verify(appears_in_story_Ancient_Mariner),
  53.              verify(flys_well).
  54. hilsha    :- fish,
  55.          verify( lives_in_sea ),
  56.          verify( lays_eggs_in_river ).
  57. snake     :- reptile,
  58.          verify( lay_their_egg_on_land ),
  59.          verify( three_chambered_heart ),
  60.          verify( have_no_leg ).
  61. alligator :- reptile,
  62.          verify( lay_their_egg_on_land ),
  63.          verify(  four_chambered_heart ).
  64. frog :- amphibians,
  65.     verify( lay_eggs_in_clusters ),
  66.         verify( has_slimy_skin ).
  67. caecilian :- amphibians,
  68.          verify( live_hidden_in_the_ground ),
  69.          verify( has_smoother_skins ),
  70.          verify( has_limited_vision ).
  71.  
  72. /* classification rules */
  73. reptile :- verify(  walked_in_the_chest ) , !.
  74. reptile :- verify( cold_blooded ).
  75. amphibians :- verify( breathe_through_skins ) , !.
  76. amphibians :- verify( hibernate_during_cold_winter ).
  77. fish  :- verify( has_gill ) , !.
  78. fish  :- verify( has_post_anal_tail).
  79. mammal    :- verify(has_hair), !.
  80. mammal    :- verify(gives_milk).
  81. bird      :- verify(has_feathers), !.
  82. bird      :- verify(flys),
  83.              verify(lays_eggs).
  84. carnivore :- verify(eats_meat), !.
  85. carnivore :- verify(has_pointed_teeth),
  86.              verify(has_claws),
  87.              verify(has_forward_eyes).
  88. ungulate :- mammal,
  89.             verify(has_hooves), !.
  90. ungulate :- mammal,
  91.             verify(chews_cud).
  92.  
  93. /* how to ask questions */
  94. ask(Question) :-
  95.     write('Does the animal have the following attribute: '),
  96.     write(Question),
  97.     write('? '),
  98.     read(Response),
  99.     nl,
  100.     ( (Response == yes ; Response == y)
  101.       ->
  102.        assert(yes(Question)) ;
  103.        assert(no(Question)), fail).
  104.  
  105. :- dynamic yes/1,no/1.
  106.  
  107. /* How to verify something */
  108. verify(S) :-
  109.    (yes(S)
  110.     ->
  111.     true ;
  112.     (no(S)
  113.      ->
  114.      fail ;
  115.      ask(S))).
  116.  
  117. /* undo all yes/no assertions */
  118. undo :- retract(yes(_)),fail.
  119. undo :- retract(no(_)),fail.
  120. undo.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement