prolog style
- From: decuypeb@xxxxxxxxxx
- Date: Wed, 14 May 2008 01:20:10 -0700 (PDT)
Hello,
I am fairly new to Prolog programming, and I would like to have some
comment on the design and style of this somewhat larger Prolog
program.
It is an enumerator for the solutions to the Connect 4 problem. I'd
like to use both arguments to the play predicate for input and output.
I have following questions:
1) Is this a sound design to tackle this problem?
2) Do you see performance issues?
3) Do you see simplifications?
4) I read some discussions about "Pure" and "Impure" Prolog
programming style. Would you call this "Pure"?
Thanks!
Bart.
Sorry for the somewhat lengthy code snippet, but it is relevant to my
question:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
board([[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-],
[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-]]).
cols2rows([[HA|A],[HB|B],[HC|C],[HD|D],[HE|E],[HF|F],[HG|G]],[Row|
Rows]):-
atoms2list(HA,HB,HC,HD,HE,HF,HG,Row),
cols2rows([A,B,C,D,E,F,G],Rows).
cols2rows([[],[],[],[],[],[],[]],[]).
atoms2list(A,B,C,D,E,F,G,[A,B,C,D,E,F,G|[]]).
peek([], +).
peek([H|List],H).
% insertcol(Player,[],[]).
insertcol(Player,[H|Col],[H|Col]):-
not(H = -),!,
fail.
insertcol(Player,[H|Col],[Player|Col]):-
peek(Col, Next),
not(Next = -).
insertcol(Player,[H|Col],[H|Col1]):-
insertcol(Player, Col, Col1).
addhead2list([],Target,Target).
addhead2list([H|Source],Target,[H|Target]).
addallheads2list([],Target,Target).
addallheads2list([H|Lists],Target,NewTarget):-
addhead2list(H,Target,IntTarget),
addallheads2list(Lists,IntTarget,NewTarget).
deletehead([],[]).
deletehead([_|Tail],Tail).
deleteheads([],[]).
deleteheads([First|Tail],[First1|NewList]):-
deletehead(First, First1),
deleteheads(Tail,NewList).
removenulls([],[]).
removenulls([[]|Tail],Tail1):-
removenulls(Tail,Tail1).
removenulls([A|Tail],[A|Tail1]):-
removenulls(Tail,Tail1).
diag([],[[]],[[]]).
diag([],[],[]).
diag([],[[]],[]).
diag([],[],[[]]).
diag([Diag|Result], Process, []):-
addallheads2list(Process,[],Diag),
deleteheads(Process,Process2),
removenulls(Process2,Process3),
diag(Result,Process3,[]).
diag([Diag|Result], Process, Waiting):-
addhead2list(Waiting,Process,Process1),
deletehead(Waiting,Waiting1),
addallheads2list(Process1,[],Diag),
deleteheads(Process1,Process2),
removenulls(Process2,Process3),
diag(Result,Process3,Waiting1),!.
check([A,A,A,A],A).
check([_,A,A,A,A],A).
check([A,A,A,A,_],A).
check([A,A,A,A,_,_],A).
check([_,A,A,A,A,_],A).
check([_,_,A,A,A,A],A).
check([A,A,A,A,_,_,_],A).
check([_,A,A,A,A,_,_],A).
check([_,_,A,A,A,A,_],A).
check([_,_,_,A,A,A,A],A).
perform(Seq,A):-
check(Seq,A),
not(A = -).
doColumns([First|Board],Winner):-
perform(First,Winner).
doColumns([First|Board],Winner):-
doColumns(Board,Winner).
doRows(Board,Winner):-
cols2rows(Board,Board1),
doColumns(Board1,Winner).
doDiags(Board,Winner):-
diag(Board1,[],Board),
doColumns(Board1,Winner).
doOtherDiags(Board,Winner):-
cols2rows(Board,Board1),
diag(Board2,[],Board1),
doColumns(Board2,Winner).
win(Board,Winner):-
doColumns(Board,Winner).
win(Board,Winner):-
doRows(Board,Winner).
win(Board,Winner):-
doDiags(Board,Winner).
win(Board,Winner):-
doOtherDiags(Board,Winner).
doit(_,Board, Winner, []):-
win(Board,Winner).
doit(Player, Board, Winner,[Col|NewList]):-
move(Player,Board,Board1,Col),
player(Player,Player1),
doit(Player1,Board1,Winner,NewList).
move(Player,[Col|Rest],[NewCol|Rest],1):-
insertcol(Player,Col,NewCol).
move(Player,[Col|Rest],[Col|Rest1],Id1):-
move(Player,Rest,Rest1,Id),
Id1 is Id + 1.
play(Steps, Winner):-
board(Start),
doit( +, Start, Winner, Steps).
player(o,+).
player(+,o).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.
- Prev by Date: Re: Networking in Prolog - a survey
- Next by Date: Re: Aquarius prolog so fast?
- Previous by thread: Networking in Prolog - a survey
- Next by thread: Discriminating facts from rules
- Index(es):
Relevant Pages
|