Prolog Assignment: due ?.
For this assignment, you are going to implement that same thing as
you did for
your Java and Scheme projects, the clock patience program. To help you along, we're
giving you the Prolog code that takes the input card list and
builds a data structure to hold the results of the deal. You can use
this code as a starting point, or write it yourself if you like.
The code is:
/***************************************************************
each card is represented as a list. For example,
the queen of spades is [q, s]
The hand itself is stored in 13 'stacks', from the
Ace stack to King. To make this searchable, the
name of the stack is stored with the stack. For
example [5, [k, d], [2, c]] means that the stack associated
with 5 has a K of diamonds on top of a two of clubs.
So, an entire hand might look something like:
[[a, [j, c], [2, h], [3, s],[4, d]], [2, [5, d], [q, d], [7, h], [2, c]] ..., [k ...]]
To run:
?- consult(patience.pl).
The cards are stacked onto the piles from right to left,
the leftmost card is top.
|?- patience.
Game over, Piles = [[a,[k,c],[5,d],[9,c],[3,c]],[2,[k,s],[4,d],[6,s],[6,c]],
[3,[t,h],[a,d],[4,c],[q,d]],[4,[2,h],[5,h],[3,s],[t,c]],[5,[9,h],[4,h],[8,c],[5,c]],
[6,[k,h],[2,c],[3,d],[a,c]],[7,[3,h],[t,d],[8,h],[7,d]],[8,[2,d],[q,s],[j,s],[j,c]],
[9,[q,h],[2,s],[7,s],[k,d]],[t,[8,d],[j,d],[5,s],[6,h]],[j,[8,s],[7,h],[9,s],[a,h]],
[q,[q,c],[j,h],[4,s],[7,c]],[k,[9,d],[6,d],[a,s]]]
Move 1 Card=[]
***************************************************************/
/*
Start with these functions to develop:
stack([],[], []).
stack(Elem,[Elem|St], St).
hand([[a,[k,c],[5,d],[9,c],[3,c]],[2,[k, s],[4,d],[6,s],[6,c]],
[3,[t,h],[a,d],[4,c],[q,d]],[4,[2,h],[5,h],[3,s],[t,c]],[5,[9,h],[4,h],[8,c],[5,c]],
[6,[k,h],[2,c],[3,d],[a,c]],[7,[3,h],[t,d],[8,h],[7,d]],[8,[2,d],[q,s],[j,s],[j,c]],
[9,[q,h],[2,s],[7,s],[k,d]],[t,[8,d],[j,d],[5,s],[6,h]],[j,[8,s],[7,h],[9,s],[a,h]],
[q,[q,c],[j,h],[4,s],[7,c]],[k,[t,s],[9,d],[6,d],[a,s]]]).
findpile(_, [], []).
findpile(Rank, [[Rank|Pile]|Rest], Pile).
findpile(Rank, [First|Rest], Pile) :-
findpile(Rank,Rest, Pile).
pushrank(Rank, Elem, [[Rank|St1]|Rest], [[Rank|St2]|Rest]) :-
stack(Elem, St2, St1).
pushrank(Rank, Elem, [X|Rest1], [X|Rest2]) :-
pushrank(Rank, Elem, Rest1, Rest2).
poprank(Rank, Elem, [[Rank|St1]|Rest], [[Rank|St2]|Rest]) :-
stack(Elem, St1, St2).
poprank(Rank, Elem, [X|Rest1], [X|Rest2]) :-
poprank(Rank, Elem, Rest1, Rest2).
*/
patience :-
sample(Hand),
deal(Hand, k, Piles),
% getTopCard(Piles, k, Card),
pickTopCard(Piles, k, NewPiles),
play(NewPiles, Card, 1, []).
play(Piles, [], Move, PreviousCard) :-
write('Game over, Piles = '), write(Piles), nl,
write('Move '), write(Move),
write(' Card='), write(PreviousCard),nl.
play(Piles, [NextRank|[Suit]], Move, PreviousCard) :- MAKE THE **** LINES WORK:
% getTopCard(Piles, NextRank, NextCard), ****
% pickTopCard(Piles, NextRank, NewPiles), ****
NextMove is Move + 1,
% play(NewPiles, NextCard, NextMove, [NextRank|Suit]). ****
%write('Nextrank='), write(NextRank),nl,
play(NewPiles, [], NextMove, [NextRank|[Suit]]). ****REPLACE THIS LINE
WITH "play..." ABOVE
deal([], _,InitialHand) :- initialCards(InitialHand).
deal([First|RestCards], Rank, Piles) :-
nextRank(Rank, NextRank),
deal(RestCards, NextRank, Cards),
addToStack(First, Rank, Cards, Piles).
sample([[t,s],[q,c],[8,s],[8,d],[q,h],[2,d],[3,h],[k,h],[9,h],[2,h],[t,h],[k,s],
[k,c],[9,d],[j,h],[7,h],[j,d],[2,s],[q,s],[t, d],[2,c],[4,h],[5,h],[a,d],[4,d],[5,d],
[6,d],[4, s],[9,s],[5,s],[7,s],[j,s],[8,h],[3,d],[8,c],[3,s],[4,c],[6,s],[9,c],
[a,s],[7,c],[a,h],[6,h],[k,d],[j,c],[7,d],[a,c],[5,c],[t,c],[q,d],[6,c],[3,c]]).
initialCards([[a],[2],[3],[4],[5],[6],[7],[8],[9],[t],[j],[q],[k]] ).
append([],List, List).
append([First|Rest], List2, [First|List3]) :-
append(Rest, List2, List3).
nextRank(k, q).
nextRank(q, j).
nextRank(j, t).
nextRank(t, 9).
nextRank(9, 8).
nextRank(8, 7).
nextRank(7, 6).
nextRank(6, 5).
nextRank(5, 4).
nextRank(4, 3).
nextRank(3, 2).
nextRank(2, a).
nextRank(a,k).