Lab 12: Programming with cut and not
A tic-tac-toe table is encoded as a list [ [X1,Y1,Z1], [X2,Y2,Z2], [X3,Y3,Z3] ]
where a variable is bound to atom x
or z
(zero) if the position has been played or it is uninstantiated otherwise.
1. Write the predicate diags(Tbl,R)
which computes the list R
of diagonals in table Tbl
.
2. Write the predicate cols(Tbl,R)
which computes the list of columns in table Tbl
. Hint: use maplist/3
.
3. Write the predicate winner(X,Tbl)
which verifies if Tbl
is winning for player X
.
4. Write the predicate succ/2
where -? R = SomePos, succ(X,SomePos)
binds variable R
to all possible moves which player X
can play in SomePos
.
Example:
?- R = [[_,_,_],[z,_,x],[x,_,z]], succ(x,R). R = [[x, _12646, _12652], [z, _12670, x], [x, _12694, z]] ; R = [[_12640, x, _12652], [z, _12670, x], [x, _12694, z]] ; R = [[_12640, _12646, x], [z, _12670, x], [x, _12694, z]] ; R = [[_12640, _12646, _12652], [z, x, x], [x, _12694, z]] ; R = [[_12640, _12646, _12652], [z, _12670, x], [x, x, z]] ;
5. Write a predicate csp(X,G,Dom,R)
where X
is an uninstantiated variable, G
is a goal which contains the uninstantiated variable X
, Dom
is a list of possible values for X
. R
will be bound to the set of values for X
from Dom
which satisfy G
.
6. Write the predicate ncsp(Vars,G,Doms,Tuples)
, where:
Vars
is a list $ X_1, X_2, \ldots, X_n$ of uninstantiated variables.G
is a goal containing $ X_1, X_2, \ldots, X_n$ .Doms
is a list of $ D_1, D_2, \ldots, D_n$ of domains for each variableTuples
will be bound to a list of tuples (encoded themselves as lists) $ v_1, v_2, \ldots, v_n$ such that $ v_i \in D_i$ and $ v_1, v_2, \ldots, v_n$ satisfiesG
.
Example:
?- nscp([X,Y], (X+Y>4, X+Y<10), [[1,2,3,4], [2,3,8,9]], R). R = [[1,8],[2,2],[2,3],[3,2],[3,3],[4,2],[4,3]].