Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. % CS-E4800 Artificial intelligence
  2. % Round: Logic Applications
  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=3.
  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) } <= 1 :- step(T), coord(X), coord(Y) .
  33. mark(P,X,Y,T+1) :- play(P,X,Y,T), step(T+1) .
  34.  
  35. % A player must not mark the same cell again
  36.  
  37. :- play(P,X,Y,T+1), mark(P,X,Y,T), tuple(P,X,Y,T), step(T+1).
  38.  
  39. % Subsequent moves by the same player are forbidden
  40. % - turn(P,T) means that player P makes her/his move at step T
  41.  
  42. turn(P,T) :- play(P,_,_,T) .
  43. :- turn(P,T), turn(P,T+1), player(P), step(T), step(T+1) .
  44.  
  45. % Plays are contiguous and should not contain gaps
  46. % - used(T): means that a move is made at step T
  47.  
  48. used(T) :- play(P,X,Y,T), tuple(P,X,Y,T).
  49. :- used(T-1), step(T-1), step(T), T>0.
  50.  
  51. % Detect the end of play using the used/1 predicate
  52.  
  53. last(T+1) :- not used(T+1), step(T), step(T+1).
  54.  
  55. % Inertia: a cell that is marked by a played will stay marked
  56.  
  57. mark(P,X,Y,T+1) :- mark(P,X,Y,T), tuple(P,X,Y,T), step(T+1).
  58.  
  59. % Detecting winning positions
  60. % - winning(P,T) means that player P has reached a winning position
  61. % at time step T, i.e., has his/her mark on either
  62. % 1. an entire column at coordinate X,
  63. % 2. an entire row at coordinate Y,
  64. % 3. the entire diagonal 1,1 ??? n,n; or
  65. % 4. the entire diagonal 1,n ??? n,1.
  66.  
  67. winning(P,T) :- player(P), step(T), coord(X), mark(P,X,Y,T): coord(Y).
  68. winning(P,T) :- player(P), step(T), coord(Y), mark(P,X,Y,T): coord(X).
  69. winning(P,T) :- player(P), step(T), mark(P,X,X,T): coord(X).
  70. winning(P,T) :- player(P), step(T), mark(P,X,n-X,T): coord(X).
  71.  
  72. % Determine the winner and deny moves if a winning position has been reached
  73.  
  74. winner(P) :- winning(P,_).
  75. :- used(T), winning(_,T-1), step(T).
  76.  
  77. % Draws
  78.  
  79. draw :- last(n*n), not winner(P): player(P).
  80.  
  81. % Ensure that either player has won the game or a draw resulted
  82.  
  83. :- not winner(o), not winner(x), not draw .
  84.  
  85. #show play/4.
  86. #show winner/1.
  87. #show draw/0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement