# Cards representation in Prolog

1 view

I'm trying to learn Prolog. This is my first steps with this language. As for exercise, I want to write a program that can recognize some poker hands (Straight flush, Four of a kind, Full house, etc.).

I'm looking for a good card representation in Prolog. I need to have the possibility to check if one card is bigger than the other if cards are suited and so one.

I have started with code:

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.

That gives mi possibility to check if rank A is bigger than for example J.

But I'm not sure how to represent a single card. This representation should contain the rank of card and also suit. There is also some issue with Ace because Ace has rank 14 but it can be also 1 in straight.

So my question is how to represents cards if I want to make rules like:

isStraight(C1, C2, C3, C4, C5) :-

[...]

or

isStraightFlush(C1, C2, C3, C4, C5) :-

[...]

I'm sure that this is a kind of simple question if you know the language, but it is not so easy to 'switch' thinking from languages like C or python. :-)

by (108k points)

• Design a data structure and the associated methods to define and manipulate a deck of playing cards.

• The deck should contain 52 unique cards.

• The methods must include the ability to:

1.   make a new deck

2.   shuffle (randomize) the deck

3.   deal from the deck

4.   print the current contents of a deck

Each card needs to have a pip value and a suit value that constitute the unique value of the card.

You can use Unicode and SWI to make good-looking programs...

:- 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(♠(_),♠(_),♠(_),♠(_),♠(_)).