Help with game
Author Message
Help with game

Hello

I am beginner at Prolog and am having trouble programming a variant of
Tic-Tac-Toe. Two players put coins alternately on the board (4 by 4) until
one has three in a line. The board is assumed to be upright so the coins
fall down.
Betti represents the computer and she muct search for a move from C to a
configuration D where Alex doesn't have a winning strategy. C has a winning
startegy if C has not terminal lost and C can make a move to some D such
that D does not have a winning strategy. If Betti is unsuccessful she can
ask for help to be entered throught the keyboard. So far I have been unable
to work out how to get Betti to move automatically.

I'm guessing I need a third select_move rule something like:

select_move([P, b], D) :- move([P, b], D), not(won(D)).

Any help would be greatly appreciated (I have been at this for days and am
very frustrated).

Here is what I have so far:

other(a, b).
other(b, a).

% sublist definition.
sublist(Q,P) :-
append(Q1,_,P), append(_,Q,Q1).

% append.
append([], L, L).
append([X|K], L, [X|M]) :- append(K, L, M).

% length(L,N).
lh([],0).
lh([_|L], s(N)):- lh(L,N).

terminal_lost([P, X]) :-
other(X, Y),
sublist(Q, P),
good(Y, Q).

% diagonal line from bottom.
good(Y,[L1,L2,L3]) :-
append(_,[Y|K1],L1),
append(_,[Y|K2],L2),
append(_,[Y|K3],L3),
lh(K1, N),
lh(K2, s(N)),
lh(K3, s(s(N))).

% diagonal line from top.
good(Y,[L1,L2,L3]) :-
append(_,[Y|K1],L1),
append(_,[Y|K2],L2),
append(_,[Y|K3],L3),
lh(K3, N),
lh(K2, s(N)),
lh(K1, s(s(N))).

% horizontal row.
good(Y,[L1,L2,L3]) :-
append(_,[Y|K1],L1),
append(_,[Y|K2],L2),
append(_,[Y|K3],L3),
lh(K1, N),
lh(K2, N),
lh(K3, N).

% vertical line.
good(Y, [L]):-
sublist([Y,Y,Y],L).

action([P, X], N, [P1, Y]) :- update_position([P, X], N, P1), other(X, Y).

update_position([[List,X,Y,Z], Player], 1, [[Player|List],X,Y,Z]).
update_position([[X,List,Y,Z], Player], 2, [X,[Player|List],Y,Z]).
update_position([[X,Y,List,Z], Player], 3, [X,Y,[Player|List],Z]).
update_position([[X,Y,Z,List], Player], 4, [X,Y,Z,[Player|List]]).

move(C, D) :- action(C, _, D).

won(C) :- not(terminal_lost(C)), move(C, D), not(won(D)).

select_move([P, a], D) :- display_configuration(P), write('Which column
Alex? '), read(N), action([P, a], N, D).
select_move([P, b], D) :- ?
Betti!'), nl,
write('Which column should she
take?'), read(N), action([P, b], N, D).

full_board([[_,_,_,_], [_,_,_,_], [_,_,_,_], [_,_,_,_]]).

display_configuration(P) :- write(P), nl, nl.

run_game([P, X]) :- terminal_lost([P, X]), write('Game Over '),
display_configuration(P), nl.
run_game(C) :- select_move(C, D), run_game(D).

play :-
write('How about a game of Three Wins?'), nl,
run_game([[[],[],[],[]], a]).

_________________________________________________________
Do You Yahoo!?

--
Posted from smtp015.mail.yahoo.com [216.136.173.59]
via Mailgate.ORG Server - http://www.*-*-*.com/

Wed, 22 Oct 2003 21:33:19 GMT
Help with game

Quote:
> Hello

> I am beginner at Prolog and am having trouble programming a variant of
> Tic-Tac-Toe. Two players put coins alternately on the board (4 by 4) until
> one has three in a line. The board is assumed to be upright so the coins
> fall down.
> Betti represents the computer and she muct search for a move from C to a
> configuration D where Alex doesn't have a winning strategy. C has a
winning
> startegy if C has not terminal lost and C can make a move to some D such
> that D does not have a winning strategy. If Betti is unsuccessful she can
> ask for help to be entered throught the keyboard. So far I have been
unable
> to work out how to get Betti to move automatically.

See if you can find a Prolog or A.I. book that tells you about "minimaxing".
This is the principle of choosing the move that minimises the strength of
Likewise your opponent chooses a move that minimises the strength of your
best move.

I once tried to write a program to play tic-tac-toe, in Basic, many years
ago.  It was too hard for me ; I didn't manage to finish it.
But now that I'm familiar with recursion, used in many prolog predicates, I
might be able to do it, using prolog.  It must be less difficult using
prolog than Basic.

--
Martin Sondergaard,
London.

Thu, 23 Oct 2003 05:37:49 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages