Ada*_*dam 5 poker artificial-intelligence prolog declarative-programming
我正在努力学习Prolog.这是我使用这种语言的第一步.作为练习我想写一些可以识别一些扑克手的程序(同花顺,四种,满屋等).
我正在寻找Prolog的优秀卡片代表.我需要有可能检查一张卡是否比其他卡大,如果卡适合,那么一张.
我已经开始使用代码:
rank(2).
rank(3).
rank(4).
rank(5).
rank(6).
rank(7).
rank(8).
rank(9).
rank(t).
rank(j).
rank(q).
rank(k).
rank(a).
value(2, 2).
value(3, 3).
value(4, 4).
value(5, 5).
value(6, 6).
value(7, 7).
value(8, 8).
value(9, 9).
value(t, 10).
value(j, 11).
value(q, 12).
value(k, 13).
value(a, 14).
%value(a, 1).
suite(d).
suite(h).
suite(c).
suite(s).
rank_bigger(X, Y) :-
value(X, A),
value(Y, B),
A > B.
Run Code Online (Sandbox Code Playgroud)
这样可以检查等级A是否大于例如J.
但我不知道如何代表单卡.此表示应包含卡的等级并且也适合.Ace也存在一些问题,因为Ace排名第14,但也可以是1.
所以我的问题是,如果我想制定以下规则,如何表示卡片:
isStraight(C1, C2, C3, C4, C5) :-
[...]
Run Code Online (Sandbox Code Playgroud)
要么
isStraightFlush(C1, C2, C3, C4, C5) :-
[...]
Run Code Online (Sandbox Code Playgroud)
如果你了解语言,我确信这是一个简单的问题,但是用C或python这样的语言"切换"思维并不容易.:-)
Gre*_*olz 10
您可以使用unicode和SWI制作漂亮的程序......
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
:- op(200, xf, ?).
main :- print([2?,3?,'K'?,10?,3?]),
isFlush(2?,3?,'K'?,10?,3?).
isFlush(?(_),?(_),?(_),?(_),?(_)).
isFlush(?(_),?(_),?(_),?(_),?(_)).
isFlush(?(_),?(_),?(_),?(_),?(_)).
isFlush(?(_),?(_),?(_),?(_),?(_)).
Run Code Online (Sandbox Code Playgroud)
您可以使用以下形式将卡片表示为术语Rank-Suite
。
为了检查卡片是否来自同一套件,定义一个谓词:
same_suit(_-S, _-S).
Run Code Online (Sandbox Code Playgroud)
您可以使用此谓词来检查您是否有同花:
?- Cards = [1-d, 2-d, 3-d, 4-d, 5-d], maplist(same_suit(_-S), Cards).
Cards = [1-d, 2-d, 3-d, 4-d, 5-d],
S = d.
Run Code Online (Sandbox Code Playgroud)
为了检测您是否有一对、两对、三对、葫芦或四对,您只需计算手牌中对子的数量,然后将结果映射到手牌的名称。
% Count the number of pairs in the given list of cards.
count_pairs([], 0).
count_pairs([R-_ | Cs], Pairs) :-
count_rank(R, Cs, RankCount),
count_pairs(Cs, Pairs0),
Pairs is RankCount + Pairs0.
% Count the number of cards with the given rank
count_rank(R, Cs, RankCount) :-
count_rank(R, Cs, 0, RankCount).
count_rank(_, [], RankCount, RankCount) :- !.
count_rank(R, [R-_ | Cs], RankCount0, RankCount) :-
!,
RankCount1 is RankCount0 + 1,
count_rank(R, Cs, RankCount1, RankCount).
count_rank(R, [_ | Cs], RankCount0, RankCount) :-
count_rank(R, Cs, RankCount0, RankCount).
% Map the number of pairs to the name of the hand
pairs_hand(1, one_pair).
pairs_hand(2, two_pair).
pairs_hand(3, three_of_a_kind).
pairs_hand(4, full_house).
%pairs_hand(5, 'NOT POSSIBLE').
pairs_hand(6, four_of_a_kind).
Run Code Online (Sandbox Code Playgroud)
用法示例:
?- count_pairs([q-c, q-d, q-s, j-s, q-h], PairsCount), pairs_hand(PairsCount, Hand).
PairsCount = 6,
Hand = four_of_a_kind.
?- count_pairs([j-c, q-d, q-s, j-s, q-h], PairsCount), pairs_hand(PairsCount, Hand).
PairsCount = 4,
Hand = full_house.
?- count_pairs([j-c, q-d, q-s, j-s, 7-h], PairsCount), pairs_hand(PairsCount, Hand).
PairsCount = 2,
Hand = two_pair.
Run Code Online (Sandbox Code Playgroud)