All pastes #25056 Raw Edit

Mine

public text v1 · immutable
#25056 ·published 2005-10-09 21:31 UTC
rendered paste body
deck(['2S','3S','4S','5S','6S','7S','8S','9S','10S','JS','QS','KS','AS',
      '2C','3C','4C','5C','6C','7C','8C','9C','10C','JC','QC','KC','AC',
      '2H','3H','4H','5H','6H','7H','8H','9H','10H','JH','QH','KH','AH',
      '2D','3D','4D','5D','6D','7D','8D','9D','10D','JD','QD','KD','AD']).


deck_two([[2,spades],   [3,spades],    [4,spades],     [5,spades],     [6,spades],     [7,spades],    [8,spades],  
          [9,spades],   [10,spades],   ['J',spades],   ['Q',spades],   ['K',spades],   ['A',spades],
          [2,clubs],    [3,clubs],     [4,clubs],      [5,clubs],      [6,clubs],      [7,clubs],     [8,clubs],   
          [9,clubs],    [10,clubs],    ['J',clubs],    ['Q',clubs],    ['K',clubs],    ['A',clubs],
          [2,hearts],   [3,hearts],    [4,hearts],     [5,hearts],     [6,hearts],     [7,hearts],    [8,hearts],  
          [9,hearts],   [10,hearts],   ['J',hearts],   ['Q',hearts],   ['K',hearts],   ['A',hearts],
          [2,diamonds], [3,diamonds],  [4,diamonds],   [5,diamonds],   [6,diamonds],   [7,diamonds],  [8,diamonds],
          [9,diamonds], [10,diamonds], ['J',diamonds], ['Q',diamonds], ['K',diamonds], ['A',diamonds]
          ]).

print_deck([]).
print_deck([First|Rest]) :- 
	print_card(First), nl, print_deck(Rest).

print_card([Val,Suit]) :- 
	write(Val), write(' of '), write(Suit).

split_hand([], [], []).
split_hand([Card|Rest], [Cardhead|Cardrest], [Suithead|Suitrest]) :-
	split_card(Card, Cardhead, Suithead),
	split_hand(Rest, Cardrest, Suitrest).

split_card([Card,Suit], Card, Suit).

deal(_, [], Hand) :-
	length(Hand, 7),
	write('deal base case!  wheee'), nl.
deal(Deck, [Head|Rest], Sofar) :-
	Rand is random(52),
	nth0(Rand, Deck, Card),
	write(Card),
	not(member(Card, Sofar)), 
	write('card not a member'), nl,
	Head = Card,
	append([Card], Sofar, Sofarupdated),
	write(Sofarupdated),
	deal(Deck, Rest, Sofarupdated).
deal(Deck, List, Sofar) :-
	write('some card was repeated'), nl,
	deal(Deck, List, Sofar).

check_royalflush(Hand) :-
	member([10,  X], Hand),
	member(['J', X], Hand),
	member(['Q', X], Hand),
	member(['K', X], Hand),
	member(['A', X], Hand),
	write('Royal flush of '),
	write(X), nl.

check_straightflush(Hand) :-
	member(['A', X], Hand), member([2, X], Hand), member([3, X], Hand), 
	member([4, X], Hand), member([5, X], Hand), print_straightflush(5, X).
check_straightflush(Hand) :-
	member([2, X], Hand), member([3, X], Hand), member([4, X], Hand), 
	member([5, X], Hand), member([6, X], Hand), print_straightflush(6, X).
check_straightflush(Hand) :-
	member([3, X], Hand), member([4, X], Hand), member([5, X], Hand), 
	member([6, X], Hand), member([7, X], Hand), print_straightflush(7, X).
check_straightflush(Hand) :-
	member([4, X], Hand), member([5, X], Hand), member([6, X], Hand), 
	member([7, X], Hand), member([8, X], Hand), print_straightflush(8, X).
check_straightflush(Hand) :-
	member([5, X], Hand), member([6, X], Hand), member([7, X], Hand), 
	member([8, X], Hand), member([9, X], Hand), print_straightflush(9, X).
check_straightflush(Hand) :-
	member([6, X], Hand), member([7, X], Hand), member([8, X], Hand), 
	member([9, X], Hand), member([10, X], Hand), print_straightflush(10, X).
check_straightflush(Hand) :-
	member([7, X], Hand), member([8, X], Hand), member([9, X], Hand), 
	member([10, X], Hand), member(['J', X], Hand), print_straightflush('J', X).
check_straightflush(Hand) :-
	member([8, X], Hand), member([9, X], Hand), member([10, X], Hand), 
	member(['J', X], Hand), member(['Q', X], Hand), print_straightflush('Q', X).
check_straightflush(Hand) :-
	member([9, X], Hand), member([10, X], Hand), member(['J', X], Hand), 
	member(['Q', X], Hand), member(['K', X], Hand), print_straightflush('K', X).
check_straightflush(Hand) :-
	member([10, X], Hand), member(['J', X], Hand), member(['Q', X], Hand), 
	member(['K', X], Hand), member(['A', X], Hand), print_straightflush('A', X).
print_straightflush(Highcard, Suit) :-
	write(Highcard),
	write(' high straight flush of '), 
	write(Suit), nl.

check_four([X,X,X,X,_,_,_]) :- is_four(X).
check_four([_,X,X,X,X,_,_]) :- is_four(X).
check_four([_,_,X,X,X,X,_]) :- is_four(X).
check_four([_,_,_,X,X,X,X]) :- is_four(X).
is_four(Card) :-
	write('Four '),
	write(Card), write('s'), nl.

/* we have to describe the case where we have 2 sets of three
   so that we can pick the better for the set of 3 */
check_fullhouse([X,X,X,Y,Y,Y,_], _) :- is_fullhouse(X, Y).
check_fullhouse([X,X,X,_,Y,Y,Y], _) :- is_fullhouse(X, Y).
check_fullhouse([_,X,X,X,Y,Y,Y], _) :- is_fullhouse(X, Y).
check_fullhouse(_, Hand) :-
	member([X, A], Hand),
	member([X, B], Hand),
	member([X, C], Hand),
	member([Y, D], Hand),
	member([Y, E], Hand),
	not(A = B), not(A = C), not(B = C),
	not(D = E), not(X = Y),
	print_fullhouse(X,Y).
is_fullhouse(X, Y) :- X = 'A', print_fullhouse(X, Y).
is_fullhouse(X, Y) :- Y = 'A', print_fullhouse(Y, X).
is_fullhouse(X, Y) :- X = 'K', print_fullhouse(X, Y).
is_fullhouse(X, Y) :- Y = 'K', print_fullhouse(Y, X).
is_fullhouse(X, Y) :- X = 'Q', print_fullhouse(X, Y).
is_fullhouse(X, Y) :- Y = 'Q', print_fullhouse(Y, X).
is_fullhouse(X, Y) :- X = 'J', print_fullhouse(X, Y).
is_fullhouse(X, Y) :- Y = 'J', print_fullhouse(Y, X).
is_fullhouse(X, Y) :- X > Y, print_fullhose(X, Y).
is_fullhouse(X, Y) :- Y > X, print_fullhose(Y, X).
print_fullhouse(X, Y) :-
	write('Full House - '), 
	write(X), write('s full of '),
	write(Y), write('s'), nl.

check_flush([X,X,X,X,X,_,_]) :- print_flush(X).
check_flush([_,X,X,X,X,X,_]) :- print_flush(X).
check_flush([_,_,X,X,X,X,X]) :- print_flush(X).
print_flush(Suit) :-
	write('Flush of '),
	write(Suit), nl.

check_straight(Hand) :-
	member(['A', _], Hand), member([2, _], Hand), member([3, _], Hand), 
	member([4, _], Hand), member([5, _], Hand), print_straight(5).
check_straight(Hand) :-
	member([2, _], Hand), member([3, _], Hand), member([4, _], Hand), 
	member([5, _], Hand), member([6, _], Hand), print_straight(6).
check_straight(Hand) :-
	member([3, _], Hand), member([4, _], Hand), member([5, _], Hand), 
	member([6, _], Hand), member([7, _], Hand), print_straight(7).
check_straight(Hand) :-
	member([4, _], Hand), member([5, _], Hand), member([6, _], Hand), 
	member([7, _], Hand), member([8, _], Hand), print_straight(8).
check_straight(Hand) :-
	member([5, _], Hand), member([6, _], Hand), member([7, _], Hand), 
	member([8, _], Hand), member([9, _], Hand), print_straight(9).
check_straight(Hand) :-
	member([6, _], Hand), member([7, _], Hand), member([8, _], Hand), 
	member([9, _], Hand), member([10, _], Hand), print_straight(10).
check_straight(Hand) :-
	member([7, _], Hand), member([8, _], Hand), member([9, _], Hand), 
	member([10, _], Hand), member(['J', _], Hand), print_straight('J').
check_straight(Hand) :-
	member([8, _], Hand), member([9, _], Hand), member([10, _], Hand), 
	member(['J', _], Hand), member(['Q', _], Hand), print_straight('Q').
check_straight(Hand) :-
	member([9, _], Hand), member([10, _], Hand), member(['J', _], Hand), 
	member(['Q', _], Hand), member(['K', _], Hand), print_straight('K').
check_straight(Hand) :-
	member([10, _], Hand), member(['J', _], Hand), member(['Q', _], Hand), 
	member(['K', _], Hand), member(['A', _], Hand), print_straight('A').
print_straight(Highcard) :-
	write(Highcard),
	write(' high straight'), nl.

check_three(Hand) :-
	member([X, A], Hand),
	member([X, B], Hand),
	member([X, C], Hand),
	not(A = B), not(A = C), not(B = C),
	write('Three '), write(X), write('s'), nl.

/* We have to describe the case where there are three sets of 2 so we can
   pick the best 2 pair */
check_twopair([X,X,Y,Y,Z,Z,_], _) :- three_pairs(X, Y, Z).
check_twopair([X,X,Y,Y,_,Z,Z], _) :- three_pairs(X, Y, Z).
check_twopair([X,X,_,Y,Y,Z,Z], _) :- three_pairs(X, Y, Z).
check_twopair([_,X,X,Y,Y,Z,Z], _) :- three_pairs(X, Y, Z).
three_pairs(X, Y, Z) :-
	
check_twopair(_, Hand) :-
	member([X, A], Hand),
	member([X, B], Hand),
	member([Y, C], Hand),
	member([Y, D], Hand),
	not(A = B), not(C = D), not(X = Y),
	print_twopair(X, Y).

print_twopair(X, Y) :-
	write('Two Pair -- '),
	write(X), write('s and '),
	write(Y), write('s'), nl.

check_pair(Hand) :-
	member([X, A], Hand),
	member([X, B], Hand),
	not(A = B),
	write('Pair of '),
	write(X), write('s'), nl.


check_highcard_hand(_, Hand)  :- member(['A', _], Hand), print_highcard('A').
check_highcard_hand(_, Hand)  :- member(['K', _], Hand), print_highcard('K').
check_highcard_hand(_, Hand)  :- member(['Q', _], Hand), print_highcard('Q').
check_highcard_hand(_, Hand)  :- member(['J', _], Hand), print_highcard('J').
check_highcard_card(Cards, _) :- nth1(7, Cards, Card), print_highcard(Card).
print_highcard(Card) :-
	write('High card is a '),
	write(Card), nl.

check_hands(_, _, Hand)     :- check_royalflush(Hand).
check_hands(_, _, Hand)     :- check_straightflush(Hand).
check_hands(Cards, _, _)    :- check_four(Cards).
check_hands(Cards, _, Hand) :- check_fullhouse(Cards, Hand).
check_hands(_, Suits, _)    :- check_flush(Suits).
check_hands(_, _, Hand)     :- check_straight(Hand).
check_hands(_, _, Hand)     :- check_three(Hand).
check_hands(Cards, _, Hand) :- check_twopair(Cards, Hand).
check_hands(_, _, Hand)     :- check_pair(Hand).
check_hands(Cards, _, Hand) :- check_highcard(Cards, Hand).
check_hands(_, _, Hand)     :- check_highcard_hand(Hand).
check_hands(Cards, _, _)    :- check_highcard_cards(Cards).

play :- nl,
	write(' *** Texas Hold em Poker Simulation - Prolog style *** '), nl, nl, 
	deck_two(Deck), %print_deck(Deck), nl,
	deal(Deck, Hand, []),
	write('The available 7 cards are ...'), nl,
	print_deck(Hand), nl,
	%Hand = [[10,spades],['J',diamonds],['Q',diamonds],['K',diamonds],['K',spades],[2,clubs],[2,spades]],
	split_hand(Hand, Cardsx, Suitsx),
	write(Cardsx), nl,
	write(Suitsx), nl, nl,
	write('Sort them bitches!'), nl, nl,
	msort(Cardsx, Cards),
	msort(Suitsx, Suits),
	write(Cards), nl,
	write(Suits), nl, nl,
	check_hands(Cards, Suits, Hand),		
	nl.