我正在解决这个问题:
在四人桥牌游戏中,每个玩家得到 13 张牌,按顺序发牌。编写一个谓词deal(Cards, H1, H2, H3, H4)
,将一副牌作为其第一个参数,并通过绑定H1
到玩家 1 在交易中收到的 13 张牌等成功。
提示:一种策略是编写四个辅助谓词deal1
来处理玩家 1、deal2
处理玩家 2 等。
这是给出的代码:
cards(ace).
cards(deuce).
cards(three).
cards(four).
cards(five).
cards(six).
cards(seven).
cards(eight).
cards(nine).
cards(ten).
cards(jack).
cards(queen).
cards(king).
suits(spades).
suits(hearts).
suits(clubs).
suits(diamonds).
deck([(ace,spades),(deuce,spades),(three,spades),(four,spades),
(five,spades),(six,spades),(seven,spades),(eight,spades),
(nine,spades),(ten,spades),(jack,spades),(queen,spades),
(king,spades),
(ace,hearts),(deuce,hearts),(three,hearts),(four,hearts),
(five,hearts),(six,hearts),(seven,hearts),(eight,hearts),
(nine,hearts),(ten,hearts),(jack,hearts),(queen,hearts),
(king,hearts),
(ace,clubs),(deuce,clubs),(three,clubs),(four,clubs),
(five,clubs),(six,clubs),(seven,clubs),(eight,clubs),
(nine,clubs),(ten,clubs),(jack,clubs),(queen,clubs),
(king,clubs),
(ace,diamonds),(deuce,diamonds),(three,diamonds),(four,diamonds),
(five,diamonds),(six,diamonds),(seven,diamonds),(eight,diamonds),
(nine,diamonds),(ten,diamonds),(jack,diamonds),(queen,diamonds),
(king,diamonds)]).
这是我的答案:
deal([],[],[],[],[]).
deal([C1,C2,C3,C4|Cards],[C1|H1],[C2|H2],[C3|H3],[C4|H4]) :-
deal(Cards,H1,H2,H3,H4).
我几乎可以肯定我的答案是正确的,但我在查询时遇到了问题。
?- deal(deck,A,B,C,D).
查询返回fail
,但放入实际的卡片列表,而不是deck
工作。我认为问题出在我试图插入甲板的地方,当它不是一个列表时,但我不知道如何进入deck
. 我是 Prolog 的新手,所以希望这只是一个简单的修复。谢谢
编辑:
添加以下建议后。我的新答案是
dealer([],[],[],[],[]).
dealer([C1,C2,C3,C4|Cards],[C1|H1],[C2|H2],[C3|H3],[C4|H4]) :-
dealer(Cards,H1,H2,H3,H4).
deal(_,A,B,C,D) :-
deck(Deck),
dealer(Deck,A,B,C,D).