0% found this document useful (0 votes)
104 views2 pages

X O O X X O: COP4020 Spring 2011 Homework Assignment 4

This document provides instructions for a homework assignment involving Prolog programs for tic-tac-toe and classmate relationships. It asks students to: 1) Trace the execution of finding the next move in a tic-tac-toe board position. 2) Modify the board position and trace again, noting the difference in steps. 3) Modify the move definition and trace, explaining why it does not work. 4) Fix the issue with the modified move definition. 5) Modify a classmate relationship rule to exclude self-classmates. 6) Write a predicate to square only positive numbers in a list.

Uploaded by

ciberneto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views2 pages

X O O X X O: COP4020 Spring 2011 Homework Assignment 4

This document provides instructions for a homework assignment involving Prolog programs for tic-tac-toe and classmate relationships. It asks students to: 1) Trace the execution of finding the next move in a tic-tac-toe board position. 2) Modify the board position and trace again, noting the difference in steps. 3) Modify the move definition and trace, explaining why it does not work. 4) Fix the issue with the modified move definition. 5) Modify a classmate relationship rule to exclude self-classmates. 6) Write a predicate to square only positive numbers in a list.

Uploaded by

ciberneto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COP4020 Spring 2011 Homework Assignment 4

1. Consider the following tic-tac-toe board positions:


X O O
X X
O

That is, the program database holds the following facts (in this order):
x(1).
x(5).
x(6).
o(2).
o(3).
o(7).
Show a trace of the execution of sub-goals to solve the goal move(X).:

Use the tic-tac-toe program shown in the lecture notes, not the one in the textbook, which
is slightly different. You can download the program from:
http://www.cs.fsu.edu/~engelen/courses/COP4020/tictactoe.gz
Change the source so that the X and O are set as shown in the figure. On linprog, start
Prolog with pl (or swipl or xpce when available) and load the program with
[tictactoe]. (dont forget the period to terminate the command). Type
move(X). to query the system, but notice that the starting board positions are
different in this demo. Use listing. to show the program database. Use trace.
to activate the tracer before you enter a goal (use ? for help and type ENTER to creep
through the trace). The tracer will help you with this assignment, but in addition you have
to write down the trace depth (indentation), show failures, and redos.

2. Now change the position o(7) into o(9) in the program, reload the program, and
trace move(X).. You will notice it takes more steps to find the winning spot for the X.
How many steps are needed to find the move compared to the previous case?

3. Lets change the program to see if we can speed it up. Change move into:
move(A) :- empty(A), good(A), !.
Trace move(X).. What happens? Explain why this does not work. i.e. what is
required for empty(A) to use as a test?

4. Fix this problem so that the modified move clause above works. Hint: you need to
change the definition of empty so that it tries A for each grid cell we want to check.

5. Consider the Prolog program:
takes(jane_doe, his201).
takes(jane_doe, cs254).
takes(ajit_chandra, art302).
takes(ajit_chandra, cs254).
classmates(X, Y) :- takes(X, Z), takes(Y, Z).
The query
?- classmates(jane_doe, X).
will succeed three times: twice with X = jane_doe and once with X =
ajit_chandra. Modify the classmates(X, Y) rule so that a student is not
considered a classmate of him or herself.

6. Write a Prolog predicate that takes a list of numbers and returns the list of squares of
only those numbers that are positive (ignoring the non-positive values):
squares([], []).
squares([X|Xs], [Y|Ys]) :- (check if X is positive, Y=X
2
, compute Ys)
squares([X|Xs], Ys) :- (check if X is non-positive, compute Ys)
Complete the above. Is this predicate relational, i.e. can in/output parameters reversed?

You might also like