Advertisement
Guest User

Untitled

a guest
Mar 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 2.93 KB | None | 0 0
  1. % CS-E4800 Artificial intelligence
  2. % Round: Applications via Logic Programming
  3. %
  4. % Base Encoding for the ASP Exercise: AI Planning / Tic-Tac-Toe
  5. %
  6. % Some parts of the encoding have been deleted and indicated by
  7. % question marks "???". You are supposed to fill in the missing details
  8. % on the basis of descriptions given as well as general understanding of
  9. % the problem.
  10.  
  11. % The dimension (n x n) of the Tic-Tac-Toe board
  12. % (can be changed from the command line -cn=... of clingo/gringo).
  13.  
  14. #const n=2.
  15.  
  16. % Domains for coordinate values, time steps, and players
  17. % The domain of "tuples" is handy in a number of rules
  18. % that concern the moves of players and changes in state
  19.  
  20. coord(1..n).
  21. step(0..n*n-1).
  22. player(x).
  23. player(o).
  24. tuple(P,X,Y,T) :- player(P), coord(X), coord(Y), step(T).
  25.  
  26. % Choose the moves of the players: at most one move per time step 0..n*n-1
  27. % - play(P,X,Y,T) means that player P puts his/her mark at
  28. %   coordinates X,Y at step T
  29. % - mark(P,X,Y,T) records the state of the board which is
  30. %   updated on the basis of moves made (and later by laws of inertia)
  31.  
  32. { play(P,X,Y,T): player(P), coord(X), coord(Y) } <=1 :- step(T), T<(n*n).
  33. mark(P,X,Y,T+1) :- play(P,X,Y,T+1).
  34. % A player must not mark the same cell again
  35.  
  36. :- play(P,X,Y,T1), play(P,X,Y,T2), step(T1), step(T2), T1!=T2.% tuple(P,X,Y,T), step(T+1).
  37.  
  38. % Subsequent moves by the same player are forbidden
  39. % - turn(P,T) means that player P makes her/his move at step T
  40.  
  41. turn(P,T) :- step(T), play(P,X,Y,T).
  42. :- turn(P,T), turn(P,T+1), player(P), step(T), step(T+1).
  43.  
  44. % Plays are contiguous and should not contain gaps
  45. % - used(T): means that a move is made at step T
  46.  
  47. used(T) :- play(P,X,Y,T), tuple(P,X,Y,T).
  48. :- used(T), not used(T-1), step(T-1), step(T), T>0.
  49.  
  50. % Detect the end of play using the used/1 predicate
  51.  
  52. last(T+1) :- used(T), not used(T+1), step(T), step(T+1).
  53.  
  54. % Inertia: a cell that is marked by a played will stay marked
  55.  
  56. mark(P,X,Y,T) :- mark(P,X,Y,T+1), tuple(P,X,Y,T), step(T+1).
  57.  
  58. % Detecting winning positions
  59. % - winning(P,T) means that player P has reached a winning position
  60. %   at time step T, i.e., has his/her mark on either
  61. %   1. an entire column at coordinate X,
  62. %   2. an entire row at coordinate Y,
  63. %   3. the entire diagonal 1,1 ??? n,n; or
  64. %   4. the entire diagonal 1,n ??? n,1.
  65.  
  66. winning(P,T) :- player(P), step(T), coord(X), mark(P,X,Y,T): coord(Y).
  67. winning(P,T) :- player(P), step(T), coord(Y), mark(P,X,Y,T): coord(X).
  68. winning(P,T) :- player(P), step(T), mark(P,X,X,T): coord(X).
  69. winning(P,T) :- player(P), step(T), mark(P,X,Y,T): coord(X), coord(Y), Y=n-X+1.
  70.  
  71. % Determine the winner and deny moves if a winning position has been reached
  72.  
  73. winner(P) :- winning(P,T).
  74. :- used(T), winning(P,T-1), step(T).
  75.  
  76. % Draws
  77.  
  78. draw :- last(n*n), not winner(P): player(P).
  79.  
  80. % Ensure that either player has won the game or a draw resulted
  81.  
  82. :- not winner(x), not winner(o), not draw.
  83.  
  84. #show play/4.
  85. #show winner/1.
  86. #show draw/0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement