home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / PITCH / PITCH.P < prev    next >
Text File  |  1996-01-21  |  44KB  |  1,135 lines

  1. /* not definition required RS6000 prolog */
  2. /*not(P) :- call(P), !, fail.
  3. not(P). */
  4. /* member and append predicates from the Art of Prolog. */ 
  5. member(X,[X|Xs]).
  6. member(X,[Y|Ys]) :- member(X,Ys).
  7. append([],Ys,Ys).
  8. append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).
  9. /* predicate eq defined for use with not above */
  10. eq(A,B) :- A = B.
  11. /**********************************************************************/
  12. /* deck representation follows                                        */
  13. /* for each suite these is a predicate which is true of cards of that */
  14. /* suite represented as a list of data for that card. The data is : */
  15. /* [card number(as represented in deck and other lists of cards ,   */
  16. /* sequence value in this suite,                                    */
  17. /* suite,                            */                                */
  18. /* name of card,                           */                            */
  19. /* points for taking this card,                                     */
  20. /* points for playing this card]                                     */
  21. /*******************************************************************/
  22.  
  23. heart([0,2,hearts,c_2_of_hearts,0,1]).
  24. heart([1,3,hearts,c_3_of_hearts,0,0]).
  25. heart([2,4,hearts,c_4_of_hearts,0,0]).
  26. heart([3,5,hearts,c_5_of_hearts,0,0]).
  27. heart([4,6,hearts,c_6_of_hearts,0,0]).
  28. heart([5,7,hearts,c_7_of_hearts,0,0]).
  29. heart([6,8,hearts,c_8_of_hearts,0,0]).
  30. heart([7,9,hearts,c_9_of_hearts,0,0]).
  31. heart([8,10,hearts,c_10_of_hearts,1,0]).
  32. heart([52,11,hearts,low_joker,1,0]).
  33. heart([53,12,hearts,high_joker,1,0]).
  34. heart([22,13,hearts,j_of_diamonds,1,0]).
  35. heart([9,14,hearts,j_of_hearts,1,0]).
  36. heart([10,15,hearts,q_of_hearts,0.0]).
  37. heart([11,16,hearts,k_of_hearts,0,0]).
  38. heart([12,17,hearts,a_of_hearts,0,1]).
  39. diamond([13,2,diamonds,c_2_of_diamonds,0,1]).
  40. diamond([14,3,diamonds,c_3_of_diamonds,0,0]).
  41. diamond([15,4,diamonds,c_4_of_diamonds,0,0]).
  42. diamond([16,5,diamonds,c_5_of_diamonds,0,0]).
  43. diamond([17,6,diamonds,c_6_of_diamonds,0,0]).
  44. diamond([18,7,diamonds,c_7_of_diamonds,0,0]).
  45. diamond([19,8,diamonds,c_8_of_diamonds,0,0]).
  46. diamond([20,9,diamonds,c_9_of_diamonds,0,0]).
  47. diamond([21,10,diamonds,c_10_of_diamonds,1,0]).
  48. diamond([52,11,diamonds,low_joker,1,0]).
  49. diamond([53,12,diamonds,high_joker,1,0]).
  50. diamond([9,13,diamonds,j_of_hearts,1,0]).
  51. diamond([22,14,diamonds,j_of_diamonds,1,0]).
  52. diamond([23,15,diamonds,q_of_diamonds,0,0]).
  53. diamond([24,16,diamonds,k_of_diamonds,0,0]).
  54. diamond([25,17,diamonds,a_of_diamonds,0,1]).
  55. spade([26,2,spades,c_2_of_spades,0,1]).
  56. spade([27,3,spades,c_3_of_spades,0,0]).
  57. spade([28,4,spades,c_4_of_spades,0,0]).
  58. spade([29,5,spades,c_5_of_spades,0,0]).
  59. spade([30,6,spades,c_6_of_spades,0,0]).
  60. spade([31,7,spades,c_7_of_spades,0,0]).
  61. spade([32,8,spades,c_8_of_spades,0,0]).
  62. spade([33,9,spades,c_9_of_spades,0,0]).
  63. spade([34,10,spades,c_10_of_spades,1,0]).
  64. spade([52,11,spades,low_joker,1,0]).
  65. spade([53,12,spades,high_joker,1,0]).
  66. spade([48,13,spades,j_of_clubs,1,0]).
  67. spade([35,14,spades,j_of_spades,1,0]).
  68. spade([36,15,spades,q_of_spades,0,0]).
  69. spade([37,16,spades,k_of_spades,0,0]).
  70. spade([38,17,spades,a_of_spades,0,1]).
  71. club([39,2,clubs,c_2_of_clubs,0,1]).
  72. club([40,3,clubs,c_3_of_clubs,0,0]).
  73. club([41,4,clubs,c_4_of_clubs,0,0]).
  74. club([42,5,clubs,c_5_of_clubs,0,0]).
  75. club([43,6,clubs,c_6_of_clubs,0,0]).
  76. club([44,7,clubs,c_7_of_clubs,0,0]).
  77. club([45,8,clubs,c_8_of_clubs,0,0]).
  78. club([46,9,clubs,c_9_of_clubs,0,0]).
  79. club([47,10,clubs,c_10_of_clubs,1,0]).
  80. club([52,11,clubs,low_joker,1,0]).
  81. club([53,12,clubs,high_joker,1,0]).
  82. club([35,13,clubs,j_of_spades,1,0]).
  83. club([48,14,clubs,j_of_clubs,1,0]).
  84. club([49,15,clubs,q_of_clubs,0,0]).
  85. club([50,16,clubs,k_of_clubs,0,0]).
  86. club([51,17,clubs,a_of_clubs,0,1]).
  87. valid_bid(Val) :- 2 =< Val , Val =< 7.
  88. valid_bid(Val) :- Val = pass.
  89. valid_suite(hearts).
  90. valid_suite(diamonds).
  91. valid_suite(spades).
  92. valid_suite(clubs).
  93.  
  94. /* the card predicate combines infomation of all suits */
  95.  
  96. card([No,Val,Suite,Name,Pt_taken,Pt_played]) :- 
  97.         heart([No,Val,Suite,Name,Pt_taken,Pt_played]).
  98. card([No,Val,Suite,Name,Pt_taken,Pt_played]) :- 
  99.         diamond([No,Val,Suite,Name,Pt_taken,Pt_played]).
  100. card([No,Val,Suite,Name,Pt_taken,Pt_played]) :- 
  101.         spade([No,Val,Suite,Name,Pt_taken,Pt_played]).
  102. card([No,Val,Suite,Name,Pt_taken,Pt_played]) :- 
  103.         club([No,Val,Suite,Name,Pt_taken,Pt_played]).
  104. valid_card(Name,Suite,H) :-        
  105.        card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  106.        member(No,H).
  107. /* some predicates follow which are used for initialization */
  108.  
  109. bind_teams([[dennis,dick],[sussy,john]]).
  110. bind_players([dennis,sussy,dick,john]).
  111. succ_player(dennis,sussy).
  112. succ_player(sussy,dick).
  113. succ_player(dick,john).
  114. succ_player(john,dennis).
  115. partner(dennis,dick).
  116. partner(dick,dennis).
  117. partner(sussy,john).
  118. partner(john,sussy).
  119. bind_dealer(dennis).
  120. bind_played([[],[],[],[]]).
  121. bind_taken([[],[],[],[]]).
  122. bind_score([0,0]).
  123. /*******************************************************************/
  124. /* The main predicate for invoking a game of pitch follows        */
  125. /* to invoke a game the query - play_game(R,S). - may be used.     */
  126. /******************************************************************/
  127.  
  128. play_game(Teams,Scores) :- bind_teams(Teams),
  129.         bind_players(Players),
  130.         bind_score(Scores),bind_dealer(Dealer),
  131.         consult('shuffle.pl'), 
  132.         open('pitch.log',write,Log),
  133.         play_hand(Players,Teams,Scores,Dealer,Deck,0,Log).
  134. play_hand(Players,Teams,Scores,Dealer,Deck,Cnt,Log) :- done(Teams,Scores).
  135. play_hand(Players,Teams,Scores,Dealer,Deck,Cnt,Log) :-
  136.  
  137.         bind_played(Played),
  138.         consult('is_out.pl'),
  139.         bind_taken(Taken),
  140. /*        shell('shuffle >shuffle.p'),  */
  141.         bind_deck(Cnt,Deck),
  142.         deal(Players,Deck,Newdeckx,Hands,Dealer,Log),
  143.         bid(Players,Hands,Dealer,Bidder,Bid,Suite,Log),
  144.         print_bl(bid_is,Log),
  145.         print_bl(Bidder,Log),
  146.         print_bl(Bid,Log),
  147.         print_nl(Suite,Log),
  148.         discard(Players,Hands,Hands2,Suite,Log),
  149.         deal2(Players,Newdeckx,Hands2,Hands3,Dealer,Bidder,Log),
  150.         discard(Players,Hands3,Hands4,Suite,Log),
  151.         abolish(is_out,1), 
  152.         consult('is_out.pl'), 
  153.         play_cards(Players,Hands4,Played,Taken,Bidder,Suite,
  154.                 Newplayed,Newtaken,Npret,Ntret,Log),
  155.         score(Players,Teams,Npret,Ntret,Scores,Newscores,Bidder,Bid,Suite,Log), 
  156.         succ_player(Dealer,Newdealer),
  157.         Cnt2 is Cnt + 1,
  158.         play_hand(Players,Teams,Newscores,Newdealer,Newdeck,Cnt2,Log).
  159. /* game is over when score reaches 21 */
  160. done(Teams,[S1,S2]) :- S1 >= 21.
  161. done(Teams,[S1,S2]) :- S2 >= 21. 
  162. /* deal cards 1st round.  Each player gets 9 cards */
  163. deal(Players,Deck,Newdeckx,Hands,Dealer,Log) :- print_nl(p_deal,Log),
  164.         Deck = [C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,C15,
  165.         C16,C17,C18,C19,C20,C21,C22,C23,C24,C25,C26,C27,C28,C29,C30,
  166.         C31,C32,C33,C34,C35,C36,C37,C38,C39,C40,C41,C42,C43,C44,C45,
  167.         C46,C47,C48,C49,C50,C51,C52,C53,C54],
  168.         !,
  169.         
  170.         Newdeckx = [C37,C38,C39,C40,C41,C42,C43,C44,
  171.         C45,C46,C47,C48,C49,C50,C51,C52,C53,C54],
  172.         
  173.         Hands = [[C1,C2,C3,C13,C14,C15,C25,C26,C27],
  174.         [C4,C5,C6,C16,C17,C18,C28,C29,C30],
  175.         [C7,C8,C9,C19,C20,C21,C31,C32,C33],
  176.         [C10,C11,C12,C22,C23,C24,C34,C35,C36]],
  177.         print_nl(Hands,Log).
  178. /* rules foer what a player can bid follow */
  179. /*
  180. can_bid(4,Hand,Suite1,Bidder) :- 
  181.         member(No1,Hand),
  182.         member(No2,Hand),
  183.         card([No1,2,Suite1,Name1,Pt_taken1,Pt_played1]),
  184.         card([No2,17,Suite2,Name2,Pt_taken2,Pt_played2]),
  185.         Suite1 = Suite2.
  186. can_bid(3,Hand,Suite,Bidder) :- 
  187.         member(No1,Hand),
  188.         card([No1,17,Suite,Name1,Pt_taken1,Pt_played1]).
  189. can_bid(2,Hand,Suite1,Bidder) :- 
  190.         member(No1,Hand),
  191.         member(No2,Hand),
  192.         card([No1,2,Suite1,Name1,Pt_taken1,Pt_played1]),
  193.         card([No2,Val2,Suite2,Name2,Pt_taken2,Pt_played2]),
  194.         Val2 >= 10,
  195.         Suite1 = Suite2.
  196. can_bid(0,Hand,pass,Bidder).
  197. */
  198. /* the bidding process is described below */
  199. bid(Players,Hands,Dealer,Bidder,Bid,Suite,Log) :- print_nl(p_bid,Log),
  200.         succ_player(Dealer,Bidder1),
  201.         get_hand(Players,Hands,Bidder1,Hand1),
  202.         trybid(Bidder1,Hand1,0,pass,pass,Maxbidder1,Maxbid1,Maxsuite1,Log),
  203.         succ_player(Bidder1,Bidder2),
  204.         get_hand(Players,Hands,Bidder2,Hand2),
  205.         trybid(Bidder2,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  206.                 Maxbidder2,Maxbid2,Maxsuite2,Log),
  207.         succ_player(Bidder2,Bidder3),
  208.         get_hand(Players,Hands,Bidder3,Hand3),
  209.         trybid(Bidder3,Hand3,Maxbid2,Maxbidder2,Maxsuite2,
  210.                 Maxbidder3,Maxbid3,Maxsuite3,Log),
  211.         succ_player(Bidder3,Bidder4),
  212.         get_hand(Players,Hands,Bidder4,Hand4),
  213.         trybid(Bidder4,Hand4,Maxbid3,Maxbidder3,Maxsuite3,
  214.                 Bidder5,Bid5,Suite5,Log),
  215.         no_bid(Dealer,Bidder5,Bid5,Suite5,Bidder,Bid,Suite).
  216. no_bid(Dealer,Bidder5,Bid5,Suite5,Bidder,Bid,Suite) :-
  217.      Bid5 = 0,
  218.      Bidder = Dealer,
  219.      Bid = 2,
  220.      Suite = spades.
  221. no_bid(Dealer,Bidder5,Bid5,Suite5,Bidder,Bid,Suite) :-
  222.      not (Bid5 = 0),
  223.      Bidder = Bidder5,
  224.      Bid = Bid5,
  225.      Suite = Suite5.
  226.      
  227. /* get hand provodes a list of cards associated with a player */
  228. get_hand(Players,Hands,P1,H1) :-
  229.         Players = [P1,P2,P3,P4],
  230.         Hands = [H1,H2,H3,H4].
  231. get_hand(Players,Hands,P2,H2) :-
  232.         Players = [P1,P2,P3,P4],
  233.         Hands = [H1,H2,H3,H4].
  234. get_hand(Players,Hands,P3,H3) :-
  235.         Players = [P1,P2,P3,P4],
  236.         Hands = [H1,H2,H3,H4].
  237. get_hand(Players,Hands,P4,H4) :-
  238.         Players = [P1,P2,P3,P4],
  239.         Hands = [H1,H2,H3,H4].
  240. /* put_hand updates a list of cards associated with a player */
  241. put_hand(Players,Hands,P1,Hand,Newhands) :-
  242.         Players = [P1,P2,P3,P4],
  243.         Hands = [H1,H2,H3,H4],
  244.         Newhands = [Hand,H2,H3,H4].
  245. put_hand(Players,Hands,P2,Hand,Newhands) :-
  246.         Players = [P1,P2,P3,P4],
  247.         Hands = [H1,H2,H3,H4],
  248.         Newhands = [H1,Hand,H3,H4].
  249. put_hand(Players,Hands,P3,Hand,Newhands) :-
  250.         Players = [P1,P2,P3,P4],
  251.         Hands = [H1,H2,H3,H4],
  252.         Newhands = [H1,H2,Hand,H4].
  253. put_hand(Players,Hands,P4,Hand,Newhands) :-
  254.         Players = [P1,P2,P3,P4],
  255.         Hands = [H1,H2,H3,H4],
  256.         Newhands = [H1,H2,H3,Hand].
  257. /* current player can bid higher than high bid so far */
  258. trybid(Bidder2,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  259.         Maxbidder2,Maxbid2,Maxsuite2,Log) :-
  260.         not(eq(dennis,Bidder2)),
  261.         max_bid(Bidval,Hand2,Suite,Bidder2),
  262.         Bidval > Maxbid1, 
  263.         print_bl(Bidder2,Log),
  264.         print_bl(bids,Log),
  265.         print_nl(Bidval,Log),
  266.         Maxbidder2 = Bidder2,
  267.         Maxbid2 = Bidval,
  268.         Maxsuite2 = Suite.
  269. /* current player can not bid higher than high bid so far */
  270. trybid(Bidder2,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  271.         Maxbidder2,Maxbid2,Maxsuite2,Log) :-
  272.         not(eq(dennis,Bidder2)),
  273.         max_bid(Bidval,Hand2,Suite,Bidder2),
  274.         Bidval =< Maxbid1,
  275.         Maxbidder2 = Maxbidder1,
  276.         Maxbid2 = Maxbid1,
  277.         Maxsuite2 = Maxsuite1.
  278. /* rule repeated 3 times for 3 chances for valid answer */
  279. trybid(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  280.         Maxbidder2,Maxbid2,Maxsuite2,Log) :-
  281.         display_hand(Hand2,Log),
  282.         print_bl(enter,Log),
  283.         print_bl(bid,Log),
  284.         print_nl(value,Log),
  285.         read(Bidval),
  286.         valid_bid(Bidval),
  287.         print_bl(enter,Log),
  288.         print_bl(bid,Log),
  289.         print_nl(suite,Log),
  290.         read(Suite),
  291.         valid_suite(Suite),!,
  292.         trybid_b(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  293.         Maxbidder2,Maxbid2,Maxsuite2,Log,Bidval,Suite).
  294. trybid(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  295.         Maxbidder2,Maxbid2,Maxsuite2,Log) :-
  296.         display_hand(Hand2,Log),
  297.         print_bl(enter,Log),
  298.         print_bl(bid,Log),
  299.         print_nl(value,Log),
  300.         read(Bidval),
  301.         valid_bid(Bidval),
  302.         print_bl(enter,Log),
  303.         print_bl(bid,Log),
  304.         print_nl(suite,Log),
  305.         read(Suite),
  306.         valid_suite(Suite),!,
  307.         trybid_b(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  308.         Maxbidder2,Maxbid2,Maxsuite2,Log,Bidval,Suite).
  309. trybid(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  310.         Maxbidder2,Maxbid2,Maxsuite2,Log) :-
  311.         display_hand(Hand2,Log),
  312.         print_bl(enter,Log),
  313.         print_bl(bid,Log),
  314.         print_nl(value,Log),
  315.         read(Bidval),
  316.         valid_bid(Bidval),
  317.         print_bl(enter,Log),
  318.         print_bl(bid,Log),
  319.         print_nl(suite,Log),
  320.         read(Suite),
  321.         valid_suite(Suite),!,
  322.         trybid_b(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  323.         Maxbidder2,Maxbid2,Maxsuite2,Log,Bidval,Suite).
  324. trybid_b(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  325.         Maxbidder2,Maxbid2,Maxsuite2,Log,Bidval,Suite) :-
  326.         Bidval > Maxbid1, 
  327.         print_bl(Bidder2,Log),
  328.         print_bl(bids,Log),
  329.         print_nl(Bidval,Log),
  330.         Maxbidder2 = dennis,
  331.         Maxbid2 = Bidval,
  332.         Maxsuite2 = Suite.
  333. trybid_b(dennis,Hand2,Maxbid1,Maxbidder1,Maxsuite1,
  334.         Maxbidder2,Maxbid2,Maxsuite2,Log,Bidval,Suite) :-
  335.         Bidval = pass,
  336.         Maxbidder2 = Maxbidder1,
  337.         Maxbid2 = Maxbid1,
  338.         Maxsuite2 = Maxsuite1.
  339.  
  340. /* max bid appear in order lowest to highest */
  341.  
  342.  
  343. max_bid(7,Hand,Suite,Bidder) :- can_bid(7,Hand,Suite,Bidder),!.
  344. max_bid(6,Hand,Suite,Bidder) :- can_bid(6,Hand,Suite,Bidder),!.
  345. max_bid(5,Hand,Suite,Bidder) :- can_bid(5,Hand,Suite,Bidder),!.
  346. max_bid(4,Hand,Suite,Bidder) :- can_bid(4,Hand,Suite,Bidder),!.
  347. max_bid(3,Hand,Suite,Bidder) :- can_bid(3,Hand,Suite,Bidder),!.
  348. max_bid(2,Hand,Suite,Bidder) :- can_bid(2,Hand,Suite,Bidder),!.
  349. max_bid(0,Hand,Suite,Bidder) :- can_bid(0,Hand,Suite,Bidder),!.
  350.  
  351. /* delete item (card,Log) from list of items (cards,Log) */
  352. delete(Item,List,Newlist,Log) :-
  353.         discard_item(Item,List,Nl,[],Newlist,Log).
  354.                  
  355.                  
  356. discard_item(Item,List,Nl,Partl,Newlist,Log) :-
  357.         List = [Item | Nl],
  358.         Partl2 = Partl,
  359.         discard_item(Item,Nl,Nl2,Partl2,Newlist,Log).
  360. discard_item(Item,List,Nl,Partl,Newlist,Log) :-
  361.         List = [Item],
  362.         Partl2 = Partl,
  363.         discard_item(Item,[],Nl2,Partl2,Newlist,Log).
  364.  
  365. discard_item(Item,List,Nl,Partl,Newlist,Log) :-
  366.         List = [I | Nl],
  367.         not(eq(I , Item)), 
  368.         Partl2 = [I | Partl],
  369.         discard_item(Item,Nl,Nl2,Partl2,Newlist,Log).
  370. discard_item(Item,List,Nl,Partl,Newlist,Log) :-
  371.         List = [I],
  372.         not(eq(I,Item)),
  373.         Partl2 = [I,Part],
  374.         discard_item(Item,[],Nl2,Partl2,Newlist,Log).
  375.  
  376. discard_item(Item,List,Nl,Partl,Newlist,Log) :-
  377.         List = [],!,
  378.         Newlist = Partl.
  379. /* once suite is determined by high bidder players must discard cards
  380. not of that suite (except left jack & jokers,Log) & also not retain more 
  381. than 6 cards*/
  382. discard(Players,Hands,Hands2,Suite,Log) :- print_nl(p_discard,Log),
  383.         Hands = [H1,H2,H3,H4],
  384.         discard_non_suite(H1,Nh1,[],Hand1,Suite),
  385.         discard_non_suite(H2,Nh2,[],Hand2,Suite),
  386.         discard_non_suite(H3,Nh3,[],Hand3,Suite),
  387.         discard_non_suite(H4,Nh4,[],Hand4,Suite),
  388.         discard_excess(Hand1,Newhand1,Suite),
  389.         discard_excess(Hand2,Newhand2,Suite),
  390.         discard_excess(Hand3,Newhand3,Suite),
  391.         discard_excess(Hand4,Newhand4,Suite),
  392.         Hands2 = [Newhand1,Newhand2,Newhand3,Newhand4],
  393.         print_nl(Hands2,Log).
  394. discard_non_suite(H,Nh,Parthand,Hand,Suite) :-
  395.         H = [No | Nh],
  396.         card([No,Val,Suite2,Name,Pt_taken,Pt_played]),
  397.         Suite = Suite2,
  398.         append(Parthand , [No] ,Parthand2),
  399.         discard_non_suite(Nh,Nh2,Parthand2,Hand,Suite).
  400. discard_non_suite(H,Nh,Parthand,Hand,Suite) :-
  401.         H = [No | Nh],
  402.         not(card([No,Val,Suite,Name,Pt_taken,Pt_played])),
  403.      /*   print_nl(discarded,Log),
  404.         print_nl(Name,Log), */
  405.         discard_non_suite(Nh,Nh2,Parthand,Hand,Suite).
  406. discard_non_suite(H,Nh,Parthand,Hand,Suite) :-
  407.         Hand = Parthand, !.
  408. discard_excess(Hand,Newhand,Suite) :-
  409.         length(Hand,Len),
  410.         Len =< 6, 
  411.         Newhand = Hand.
  412. discard_excess(Hand,Newhand,Suite) :-
  413.         length(Hand,Len),
  414.         Len > 6,
  415.         card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  416.         member(No,Hand),
  417.         2 < Val,
  418.         10 > Val,
  419.     /*    print_nl(excess,Log),
  420.         print_nl(Name,Log), */
  421.         delete(No,Hand,Newhand2),
  422.         discard_excess(Newhand2,Newhand,Suite).
  423. discard_excess(Hand,Newhand,Suite) :-
  424.         length(Hand,Len),
  425.         Len > 6,
  426.         card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  427.         member(No,Hand),
  428.         15 = Val,
  429.      /*   print_nl(excess2,Log),
  430.         print_nl(Name,Log), */
  431.         delete(No,Hand,Newhand2),
  432.         discard_excess(Newhand2,Newhand,Suite).
  433. discard_excess(Hand,Newhand,Suite) :-
  434.         length(Hand,Len),
  435.         Len > 6,
  436.         card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  437.         member(No,Hand),
  438.         16 = Val,
  439.         delete(No,Hand,Newhand2,Log),
  440.         discard_excess(Newhand2,Newhand,Suite).
  441. discard_excess(Hand,Newhand,Suite) :-
  442.         length(Hand,Len),
  443.         Len > 6,
  444.         card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  445.         member(No,Hand),
  446.         2 = Val,
  447.         delete(No,Hand,Newhand2,Log),
  448.         discard_excess(Newhand2,Newhand,Suite).
  449.         
  450. /* after discarding players (other than the bidder,Log) receive enough
  451. cards th raise their total cards to 6.  The bidder gets to look through
  452. whatever is then left */
  453.  
  454. deal2(Players,Newdeck,Hands2,Hands3,Dealer,Bidder,Log) :- 
  455.         print_nl(p_deal2,Log),
  456.         succ_player(Dealer,P1),
  457.         extras(Players,Newdeck,Newdeck2,Hands2,Hands2a,P1,Bidder,Log),
  458.         succ_player(P1,P2),
  459.         extras(Players,Newdeck2,Newdeck3,Hands2a,Hands2b,P2,Bidder,Log),
  460.         succ_player(P2,P3),
  461.         extras(Players,Newdeck3,Newdeck4,Hands2b,Hands2c,P3,Bidder,Log),
  462.         succ_player(P3,P4),
  463.         extras(Players,Newdeck4,Newdeck5,Hands2c,Hands2d,P4,Bidder,Log),
  464.         remaining(Players,Newdeck5,Hands2d,Hands3,Bidder,Log),
  465.         print_nl(final_hands,Log),
  466.         print_nl(Hands3,Log).
  467.         
  468. extras(Players,Newdeck,Newdeck2,Hands2,Hands2a,P1,Bidder,Log) :-
  469.         P1 = Bidder, ! ,
  470.         Newdeck2 = Newdeck,
  471.         Hands2a = Hands2.
  472. extras(Players,Newdeck,Newdeck2,Hands2,Hands2a,P1,Bidder,Log) :-
  473.         Newdeck = [], ! ,
  474.         Newdeck2 = Newdeck,
  475.         Hands2a = Hands2.
  476. extras(Players,Newdeck,Newdeck2,Hands2,Hands2a,P1,Bidder,Log) :-
  477.         get_hand(Players,Hands2,P1,H1),
  478.         length(H1,Len),
  479.         Len = 6, ! ,
  480.         Newdeck2 = Newdeck,
  481.         Hands2a = Hands2.
  482. extras(Players,Newdeck,Newdeck2,Hands2,Hands2a,P1,Bidder,Log) :-
  483.         get_hand(Players,Hands2,P1,H1),
  484.         length(H1,Len),
  485.         Len < 6,
  486.         Newdeck = [No | Newdeck3],
  487.         H1a = [No | H1],
  488.         put_hand(Players,Hands2,P1,H1a,Hands3),
  489.         extras(Players,Newdeck3,Newdeck2,Hands3,Hands2a,P1,Bidder,Log).
  490.         
  491.         
  492. remaining(Players,Newdeck5,Hands2d,Hands3,Bidder,Log) :-
  493.         get_hand(Players,Hands2d,Bidder,H1),
  494.         append(H1,Newdeck5,H1a),
  495.         put_hand(Players,Hands2d,Bidder,H1a,Hands3).
  496.         
  497. /* each player plays a card the highest card wins */
  498. play_cards(Players,Hands,Played,Taken,Leader,Suite,Newplayed,Newtaken,
  499.         Npret,Ntret,Log) :- 
  500.         Hands = [H1,H2,H3,H4],
  501.         sort_by_val(H1,H1s,Suite),
  502.         sort_by_val(H2,H2s,Suite),
  503.         sort_by_val(H3,H3s,Suite),
  504.         sort_by_val(H4,H4s,Suite),
  505.         Hands2 = [H1s,H2s,H3s,H4s], /* sorted hands */
  506.         print_bl(dennis,Log),
  507.         print_nl(hand,Log),
  508.         display_hand(H1s,Log),
  509.     /*    print_nl('sussy hand2',Log),
  510.         display_hand(H2s,Log),
  511.         print_nl('dick hand3',Log),
  512.         display_hand(H3s,Log),
  513.         print_nl('john hand4',Log),
  514.         display_hand(H4s,Log), */
  515.         play_card([Players,Hands2,Played,Taken,Leader,
  516.         Newhands,Newplayed,Newtaken,Newleader,Suite],Log),
  517.         play_cards(Players,Newhands,Newplayed,Newtaken,Newleader,Suite,
  518.                 Np,Nt,Npret,Ntret,Log).  
  519. /* a hand ends when all players are out of cards */
  520. play_cards(Players,Hands,Played,Taken,Leader,Suite,Newplayed,Newtaken,
  521.         Npret,Ntret,Log) :- 
  522.         out(Players,Hands,Log), !,
  523.         Npret =Played,
  524.         Ntret = Taken.
  525.         
  526. out(Players,Hands,Log) :- 
  527.         Hands = [[],[],[],[]], !,
  528.                 print_nl(p_out,Log).
  529. play_card([Players,Hands,Played,Taken,Leader,Newhands,Newplayed,
  530.         Newtaken,Newleader,Suite],Log) :-
  531.         play_1card([Players,Hands,Played,Leader,Newhands1,Newplayed1,
  532.         0,none,Newhigh1,Newwhohigh1,[],Newcards1,Suite],Log),
  533.         succ_player(Leader,P2),
  534.         play_1card([Players,Newhands1,Newplayed1,P2,Newhands2,Newplayed2,
  535.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  536.         Newcards1,Newcards2,Suite],Log),
  537.         succ_player(P2,P3),
  538.         play_1card([Players,Newhands2,Newplayed2,P3,Newhands3,Newplayed3,
  539.         Newhigh2,Newwhohigh2,Newhigh3,Newwhohigh3,
  540.         Newcards2,Newcards3,Suite],Log),
  541.         succ_player(P3,P4),
  542.         play_1card([Players,Newhands3,Newplayed3,P4,Newhands,Newplayed,
  543.         Newhigh3,Newwhohigh3,Newhigh4,Newwhohigh4,
  544.         Newcards3,Newcards4,Suite],Log),
  545.         summary_cards_taken([Players,Taken,Newtaken,Newhigh4,Newwhohigh4,
  546.         Newcards4,Newleader],Log).
  547. play_1card([Players,Newhands1,Newplayed1,P2,Newhands2,Newplayed2,
  548.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  549.         Newcards1,Newcards2,Suite],Log) :-
  550.         not(eq(P2,dennis)),
  551.         get_hand(Players,Newhands1,P2,H2),
  552.         best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  553.         Suite,Newhigh1,Newwhohigh1,Log),
  554.         card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  555.         Val > Newhigh1, ! ,
  556.         print_bl(P2,Log),
  557.         print_bl(plays,Log),
  558.         print_nl(Name,Log),
  559.         delete(Card1,H2,Nh2,Log),
  560.         Newhigh2 = Val,
  561.         Newwhohigh2 = P2,
  562.         put_hand(Players,Newhands1,P2,Nh2,Newhands2),
  563.         get_hand(Players,Newplayed1,P2,Pl1),
  564.         Pl2 = [Card1 | Pl1],
  565.         Newcards2 = [Card1 | Newcards1],
  566.   /*      print_bl(newcards2,Log),print_nl(Newcards2,Log), */
  567.         put_hand(Players,Newplayed1,P2,Pl2,Newplayed2). /* was , */
  568.     /*    print_bl(newplayed2,Log),print_nl(Newplayed2,Log). */
  569. play_1card([Players,Newhands1,Newplayed1,P2,Newhands2,Newplayed2,
  570.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  571.         Newcards1,Newcards2,Suite],Log) :-
  572.         not(eq(P2,dennis)),
  573.         get_hand(Players,Newhands1,P2,H2),
  574.         best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  575.         Suite,Newhigh1,Newwhohigh1,Log),
  576.         card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  577.         Val < Newhigh1, ! ,
  578.         print_bl(P2,Log),
  579.         print_bl(plays,Log),
  580.         print_nl(Name,Log),
  581.         delete(Card1,H2,Nh2,Log),
  582.         Newhigh2 = Newhigh1,
  583.         Newwhohigh2 = Newwhohigh1,
  584.         put_hand(Players,Newhands1,P2,Nh2,Newhands2),
  585.         get_hand(Players,Newplayed1,P2,Pl1),
  586.         Pl2 = [Card1 | Pl1],
  587.         Newcards2 = [Card1 | Newcards1],
  588.    /*     print_bl(newcards2,Log),print_nl(Newcards2,Log), */
  589.         put_hand(Players,Newplayed1,P2,Pl2,Newplayed2). /* was , */
  590.    /*     print_bl(newplayed2,Log),print_nl(Newplayed2,Log). */
  591. play_1card([Players,Newhands1,Newplayed1,P2,Newhands2,Newplayed2,
  592.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  593.         Newcards1,Newcards2,Suite],Log) :-
  594.         get_hand(Players,Newhands1,P2,H2),
  595.         H2 = [],!,
  596.         asserta(is_out(P2)), 
  597.                 print_bl(P2,Log),
  598.                 print_nl(is_out,Log),
  599.         Newhigh2 = Newhigh1,
  600.         Newhands2 = Newhands1,
  601.         Newplayed2 = Newplayed1,
  602.         Newcards2 = Newcards1, 
  603.         Newwhohigh2 = Newwhohigh1.
  604. /* rule repeated 3 times for 3 chances */
  605. play_1card([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  606.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  607.         Newcards1,Newcards2,Suite],Log) :-
  608.         get_hand(Players,Newhands1,dennis,H2),
  609.         print_bl(play,Log),
  610.         print_bl(what,Log),
  611.         print_nl(card,Log),
  612.         read(Name),
  613.         valid_card(Name,Suite,H2),!,
  614.         play_1card_b([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  615.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  616.         Newcards1,Newcards2,Suite],Log,Name,H2).
  617. play_1card([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  618.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  619.         Newcards1,Newcards2,Suite],Log) :-
  620.         get_hand(Players,Newhands1,dennis,H2),
  621.         print_bl(play,Log),
  622.         print_bl(what,Log),
  623.         print_nl(card,Log),
  624.         read(Name),
  625.         valid_card(Name,Suite,H2),!,
  626.         play_1card_b([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  627.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  628.         Newcards1,Newcards2,Suite],Log,Name,H2).
  629. play_1card([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  630.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  631.         Newcards1,Newcards2,Suite],Log) :-
  632.         get_hand(Players,Newhands1,dennis,H2),
  633.         print_bl(play,Log),
  634.         print_bl(what,Log),
  635.         print_nl(card,Log),
  636.         read(Name),
  637.         valid_card(Name,Suite,H2),!,
  638.         play_1card_b([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  639.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  640.         Newcards1,Newcards2,Suite],Log,Name,H2).
  641.  
  642. play_1card_b([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  643.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  644.         Newcards1,Newcards2,Suite],Log,Name,H2) :-
  645.         card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  646.         Val > Newhigh1, ! ,
  647.         print_bl(dennis,Log),
  648.         print_bl(plays,Log),
  649.         print_nl(Name,Log),
  650.         delete(Card1,H2,Nh2,Log),
  651.         Newhigh2 = Val,
  652.         Newwhohigh2 = dennis,
  653.         put_hand(Players,Newhands1,dennis,Nh2,Newhands2),
  654.         get_hand(Players,Newplayed1,dennis,Pl1),
  655.         Pl2 = [Card1 | Pl1],
  656.         Newcards2 = [Card1 | Newcards1],
  657.   /*      print_bl(newcards2,Log),print_nl(Newcards2,Log), */
  658.         put_hand(Players,Newplayed1,dennis,Pl2,Newplayed2). /* was , */
  659.     /*    print_bl(newplayed2,Log),print_nl(Newplayed2,Log). */
  660.         
  661.  
  662. play_1card_b([Players,Newhands1,Newplayed1,dennis,Newhands2,Newplayed2,
  663.         Newhigh1,Newwhohigh1,Newhigh2,Newwhohigh2,
  664.         Newcards1,Newcards2,Suite],Log,Name,H2) :-
  665.         card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  666.         Val < Newhigh1, ! ,
  667.         print_bl(dennis,Log),
  668.         print_bl(plays,Log),
  669.         print_nl(Name,Log),
  670.         delete(Card1,H2,Nh2,Log),
  671.         Newhigh2 = Newhigh1,
  672.         Newwhohigh2 = Newwhohigh1,
  673.         put_hand(Players,Newhands1,dennis,Nh2,Newhands2),
  674.         get_hand(Players,Newplayed1,dennis,Pl1),
  675.         Pl2 = [Card1 | Pl1],
  676.         Newcards2 = [Card1 | Newcards1],
  677.    /*     print_bl(newcards2,Log),print_nl(Newcards2,Log), */
  678.         put_hand(Players,Newplayed1,dennis,Pl2,Newplayed2). /* was , */
  679.    /*     print_bl(newplayed2,Log),print_nl(Newplayed2,Log). */
  680.  
  681. summary_cards_taken([Players,Taken,Newtaken,Newhigh4,Newwhohigh4,
  682.         Newcards4,Newleader],Log):-
  683.         get_hand(Players,Taken,Newwhohigh4,H2),
  684.         append(H2,Newcards4,H3),
  685.                 print_bl(Newwhohigh4,Log),
  686.                 print_nl(takes_the_hand,Log),
  687.         Newleader = Newwhohigh4,
  688.         put_hand(Players,Taken,Newwhohigh4,H3,Newtaken). /* was , */
  689.    /*     print_bl(newtaken,Log),print_nl(Newtaken,Log). */
  690. print_nl(X,Log) :- print(X), put(46), put(10), put(13),
  691.                 print(Log,X), put(Log,46), put(Log,10), put(Log,13).
  692. print_bl(X,Log) :- print(X), put(32),
  693.                  print(Log,X), put(Log,32).
  694.         
  695.         
  696. score(Players,Teams,Played,Taken,Scores,Newscores,Bidder,Bid,Suite,Log) :- 
  697.         Scores = [S1,S2],
  698.                 Players = [P1,P2,P3,P4],
  699.                 get_hand(Players,Taken,P1,Card1s_taken1),
  700.                 pts_taken(Card1s_taken1,Suite,Score_addt1,0,Log),
  701.                 get_hand(Players,Played,P1,Card1s_played1),
  702.                 pts_played(Card1s_played1,Suite,Score_addp1,0,Log),
  703.                 get_hand(Players,Taken,P2,Card1s_taken2),
  704.                 pts_taken(Card1s_taken2,Suite,Score_addt2,0,Log),
  705.                 get_hand(Players,Played,P2,Card1s_played2),
  706.                 pts_played(Card1s_played2,Suite,Score_addp2,0,Log),
  707.                 get_hand(Players,Taken,P3,Card1s_taken3),
  708.                 pts_taken(Card1s_taken3,Suite,Score_addt3,0,Log),
  709.                 get_hand(Players,Played,P3,Card1s_played3),
  710.                 pts_played(Card1s_played3,Suite,Score_addp3,0,Log),
  711.                 get_hand(Players,Taken,P4,Card1s_taken4),
  712.                 pts_taken(Card1s_taken4,Suite,Score_addt4,0,Log),
  713.                 get_hand(Players,Played,P4,Card1s_played4),
  714.                 pts_played(Card1s_played4,Suite,Score_addp4,0,Log),
  715.                 Pts1 is Score_addt1 + Score_addp1 + Score_addt3 + 
  716.                 Score_addp3,
  717.                 Pts2 is Score_addt2 + Score_addp2 + Score_addt4 + 
  718.                 Score_addp4,
  719.                 Teams = [T1,T2],
  720.                 member(Bidder,T1),
  721.                 Pts1 >= Bid, 
  722.         S1a is S1 + Pts1,
  723.         S2a is S2 + Pts2, 
  724.         Newscores = [S1a,S2a],
  725.         print_nl(p_score,Log),
  726.         print_nl(Teams,Log),
  727.         print_nl(Scores,Log),
  728.         print_nl(Newscores,Log).
  729.  
  730. score(Players,Teams,Played,Taken,Scores,Newscores,Bidder,Bid,Suite,Log) :- 
  731.         Scores = [S1,S2],
  732.                 Players = [P1,P2,P3,P4],
  733.                 get_hand(Players,Taken,P1,Card1s_taken1),
  734.                 pts_taken(Card1s_taken1,Suite,Score_addt1,0,Log),
  735.                 get_hand(Players,Played,P1,Card1s_played1),
  736.                 pts_played(Card1s_played1,Suite,Score_addp1,0,Log),
  737.                 get_hand(Players,Taken,P2,Card1s_taken2),
  738.                 pts_taken(Card1s_taken2,Suite,Score_addt2,0,Log),
  739.                 get_hand(Players,Played,P2,Card1s_played2),
  740.                 pts_played(Card1s_played2,Suite,Score_addp2,0,Log),
  741.                 get_hand(Players,Taken,P3,Card1s_taken3),
  742.                 pts_taken(Card1s_taken3,Suite,Score_addt3,0,Log),
  743.                 get_hand(Players,Played,P3,Card1s_played3),
  744.                 pts_played(Card1s_played3,Suite,Score_addp3,0,Log),
  745.                 get_hand(Players,Taken,P4,Card1s_taken4),
  746.                 pts_taken(Card1s_taken4,Suite,Score_addt4,0,Log),
  747.                 get_hand(Players,Played,P4,Card1s_played4),
  748.                 pts_played(Card1s_played4,Suite,Score_addp4,0,Log),
  749.                 Pts1 is Score_addt1 + Score_addp1 + Score_addt3 + 
  750.                 Score_addp3,
  751.                 Pts2 is Score_addt2 + Score_addp2 + Score_addt4 + 
  752.                 Score_addp4,
  753.                 Teams = [T1,T2],
  754.                 member(Bidder,T2),
  755.                 Pts2 >= Bid, 
  756.         S1a is S1 + Pts1,
  757.         S2a is S2 + Pts2, 
  758.         Newscores = [S1a,S2a],
  759.         print_nl(p_score,Log),
  760.         print_nl(Teams,Log),
  761.         print_nl(Scores,Log),
  762.         print_nl(Newscores,Log).
  763.  
  764. score(Players,Teams,Played,Taken,Scores,Newscores,Bidder,Bid,Suite,Log) :- 
  765.         Scores = [S1,S2],
  766.                 Players = [P1,P2,P3,P4],
  767.                 get_hand(Players,Taken,P1,Card1s_taken1),
  768.                 pts_taken(Card1s_taken1,Suite,Score_addt1,0,Log),
  769.                 get_hand(Players,Played,P1,Card1s_played1),
  770.                 pts_played(Card1s_played1,Suite,Score_addp1,0,Log),
  771.                 get_hand(Players,Taken,P2,Card1s_taken2),
  772.                 pts_taken(Card1s_taken2,Suite,Score_addt2,0,Log),
  773.                 get_hand(Players,Played,P2,Card1s_played2),
  774.                 pts_played(Card1s_played2,Suite,Score_addp2,0,Log),
  775.                 get_hand(Players,Taken,P3,Card1s_taken3),
  776.                 pts_taken(Card1s_taken3,Suite,Score_addt3,0,Log),
  777.                 get_hand(Players,Played,P3,Card1s_played3),
  778.                 pts_played(Card1s_played3,Suite,Score_addp3,0,Log),
  779.                 get_hand(Players,Taken,P4,Card1s_taken4),
  780.                 pts_taken(Card1s_taken4,Suite,Score_addt4,0,Log),
  781.                 get_hand(Players,Played,P4,Card1s_played4),
  782.                 pts_played(Card1s_played4,Suite,Score_addp4,0,Log),
  783.                 Pts1 is Score_addt1 + Score_addp1 + Score_addt3 + 
  784.                 Score_addp3,
  785.                 Pts2 is Score_addt2 + Score_addp2 + Score_addt4 + 
  786.                 Score_addp4,
  787.                 Teams = [T1,T2],
  788.                 member(Bidder,T1),
  789.                 Pts1 < Bid, 
  790.         S1a is S1 - Bid,
  791.         S2a is S2 + Pts2, 
  792.         Newscores = [S1a,S2a],
  793.         print_nl(bidder_went_set,Log),
  794.         print_nl(p_score,Log),
  795.         print_nl(Teams,Log),
  796.         print_nl(Scores,Log),
  797.         print_nl(Newscores,Log).
  798.  
  799. score(Players,Teams,Played,Taken,Scores,Newscores,Bidder,Bid,Suite,Log) :- 
  800.         Scores = [S1,S2],
  801.                 Players = [P1,P2,P3,P4],
  802.                 get_hand(Players,Taken,P1,Card1s_taken1),
  803.                 pts_taken(Card1s_taken1,Suite,Score_addt1,0,Log),
  804.                 get_hand(Players,Played,P1,Card1s_played1),
  805.                 pts_played(Card1s_played1,Suite,Score_addp1,0,Log),
  806.                 get_hand(Players,Taken,P2,Card1s_taken2),
  807.                 pts_taken(Card1s_taken2,Suite,Score_addt2,0,Log),
  808.                 get_hand(Players,Played,P2,Card1s_played2),
  809.                 pts_played(Card1s_played2,Suite,Score_addp2,0,Log),
  810.                 get_hand(Players,Taken,P3,Card1s_taken3),
  811.                 pts_taken(Card1s_taken3,Suite,Score_addt3,0,Log),
  812.                 get_hand(Players,Played,P3,Card1s_played3),
  813.                 pts_played(Card1s_played3,Suite,Score_addp3,0,Log),
  814.                 get_hand(Players,Taken,P4,Card1s_taken4),
  815.                 pts_taken(Card1s_taken4,Suite,Score_addt4,0,Log),
  816.                 get_hand(Players,Played,P4,Card1s_played4),
  817.                 pts_played(Card1s_played4,Suite,Score_addp4,0,Log),
  818.                 Pts1 is Score_addt1 + Score_addp1 + Score_addt3 + 
  819.                 Score_addp3,
  820.                 Pts2 is Score_addt2 + Score_addp2 + Score_addt4 + 
  821.                 Score_addp4,
  822.                 Teams = [T1,T2],
  823.                 member(Bidder,T2),
  824.                 Pts2 < Bid, 
  825.         S1a is S1 + Pts1,
  826.         S2a is S2 - Bid, 
  827.         Newscores = [S1a,S2a],
  828.         print_nl(bidder_went_set,Log),
  829.         print_nl(p_score,Log),
  830.         print_nl(Teams,Log),
  831.         print_nl(Scores,Log),
  832.         print_nl(Newscores,Log).
  833.  
  834. pts_taken(Card1s_taken1,Suite,Score_add1,Temp,Log) :-
  835.         Card1s_taken1 = [Card1 | Card1s_left],
  836.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  837.         Temp2 is Temp + Pt_taken,
  838.         pts_taken(Card1s_left,Suite,Score_add1,Temp2,Log).
  839. pts_taken(Card1s_taken1,Suite,Score_add1,Temp,Log) :-
  840.         Card1s_taken1 = [], ! ,
  841.         Score_add1 is Temp.
  842.         
  843. pts_played(Card1s_played1,Suite,Score_add1,Temp,Log) :-
  844.         Card1s_played1 = [Card1 | Card1s_left],
  845.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]),
  846.         Temp2 is Temp + Pt_played,
  847.         pts_played(Card1s_left,Suite,Score_add1,Temp2,Log).
  848. pts_played(Card1s_played1,Suite,Score_add1,Temp,Log) :-
  849.         Card1s_played1 = [], ! ,
  850.         Score_add1 is Temp.
  851.  
  852. /* lead highest card left */
  853. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  854.     Suite,Newhigh1,Newwhohigh1,Log) :-
  855.     not(am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  856.     Card1,Suite,Log)),
  857.     have_highest_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  858.     Cardx,Suite),
  859.     Card1 = Cardx,
  860. /*    print_nl(have_highest_card,Log), */
  861.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  862. /* take point card - last (with smallest card that woll take because sorted*/
  863. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  864.     Suite,Newhigh1,Newwhohigh1,Log) :-
  865.     am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  866.     Card1,Suite,Log),
  867.     pt_card_played(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  868.     Cardp,Suite,Log),
  869.     have_higher_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  870.     Cardx,Suite),
  871.     not(partner(P2,Newwhohigh1)),
  872.     Card1 = Cardx,
  873. /*    print_nl(take_pt_card_last,Log), */
  874.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  875. /* save point cards playing last */
  876. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  877.     Suite,Newhigh1,Newwhohigh1,Log) :-
  878.     am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  879.     Card1,Suite,Log),
  880.     can_save_point(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  881.     Cardx,Suite,Log),
  882.     Card1 = Cardx,
  883. /*    print_nl(can_save_point,Log), */
  884.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  885. /* opponent has played high card - plays small card */
  886. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  887.     Suite,Newhigh1,Newwhohigh1,Log) :-
  888.     not(have_highest_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  889.     Cardw,Suite)),
  890.     not(partner(P2,Newwhohigh1)),
  891.     have_small_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  892.     Cardx,Suite,Log),
  893.     Card1 = Cardx,
  894.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  895. /*    print_nl(play_small_card,Log). */
  896.     
  897. /* partner has played high card - plays point card */
  898. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  899.     Suite,Newhigh1,Newwhohigh1,Log) :-
  900.     not(have_highest_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  901.     Cardw,Suite)),
  902.     partner(P2,Newwhohigh1),
  903.     have_point_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  904.     Cardx,Suite,Log),
  905.     Card1 = Cardx,
  906.     card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  907. /*    print_nl(play_point_card,Log). */
  908.     
  909. /* no special rule applied so play any card */
  910. best_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,Card1,
  911.     Suite,Newhigh1,Newwhohigh1,Log) :-
  912.         H2 = [Card1 | H3],
  913.         card([Card1,Val,Suite,Name,Pt_taken,Pt_played]).
  914.         
  915. have_highest_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  916.     Cardx,Suite) :-
  917.     member(Cardx,H2),
  918.     card([Cardx,Val,Suite,Name,Pt_taken,Pt_played]),
  919.     not(exists_higher_card_not_in_list(Newcards1,Suite,Val,Newplayed1)).
  920. exists_higher_card_not_in_list(Newcards1,Suite,Val,Newplayed1) :-
  921.     card([Card2,Val2,Suite,Name,Pt_taken,Pt_played]),
  922.     Val2 > Val,
  923.     not(sub_member(Card2,Newplayed1,Newcards1)).
  924. sub_member(Card2,Newplayed1,Newcards1) :-
  925.     member(Pl,Newplayed1),
  926.     not(member(Card2,Newcards1)),
  927.     member(Card2,Pl).
  928.  
  929. point_card_played(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  930.     Card1,Suite,Log) :-
  931.     member(Card,Newcards1),
  932.     card([Card,Val,Suite,Name,Pt_taken,Pt_played]),
  933.     Pt_taken > 0.
  934. am_first_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  935.     Card1,Suite,Log) :-
  936.     length(Newcards1,Len),
  937.     Len = 0.    
  938. am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  939.     Card1,Suite,Log) :-
  940.     length(Newcards1,Len),
  941.     Len = 3.    
  942. am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  943.     Card1,Suite,Log) :-
  944.     length(Newcards1,Len),
  945.     succ_player(P2,P3),
  946.     is_out(P3),
  947.     Len = 2.    
  948. am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  949.     Card1,Suite,Log) :-
  950.     length(Newcards1,Len),
  951.     succ_player(P2,P3),
  952.     is_out(P3),
  953.     succ_player(P3,P4),
  954.     is_out(P4),
  955.     Len = 1.    
  956. point_card_may_be_played(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  957.     Card1,Suite,Log) :-
  958.     not(am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  959.     Card1,Suite,Log)),
  960.     card([Card2,Val2,Suite,Name,Pt_taken,Pt_played]),
  961.     Pt_taken > 0,
  962.     not(sub_member(Card2,Newplayed1)).
  963. can_save_point(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  964.     Cardx,Suite,Log) :-
  965.     am_last_to_play(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  966.     Cardx,Suite,Log),
  967.     member(Cardx,H2),
  968.     card([Cardx,Val,Suite,Name,Pt_taken,Pt_played]),
  969.     Pt_taken > 0,
  970.     not(exists_higher_card_in_list(Val,Newcards1,Suite)).
  971. exists_higher_card_in_list(Val,Newcards1,Suite) :-
  972.     member(Card2,Newcards1),
  973.     card([Card2,Val2,Suite,Name,Pt_taken,Pt_played]),
  974.     Val2 > Val.
  975.     
  976. partner_played_highest_card_so_far(Players,P2,H2,Newplayed1,Newtaken1,
  977.     Newcards1,Card1,Suite,Newhigh1,Newwhohigh1,Log) :-
  978.     partner(P2,Newwhohigh1).
  979.     
  980. have_small_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  981.     Cardx,Suite,Log) :-
  982.     member(Cardx,H2),
  983.     card([Cardx,Val,Suite,Name,Pt_taken,Pt_played]),
  984.     Val < 10.
  985. have_point_card(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  986.     Cardx,Suite,Log) :-
  987.     member(Cardx,H2),
  988.     card([Cardx,Val,Suite,Name,Pt_taken,Pt_played]),
  989.     Pt_taken > 0.
  990.     
  991.     
  992. display_hand(H,Log) :-
  993.     H = [C | H2],
  994.     card([C,Val,Suite,Name,Pt_taken,Pt_played]),
  995.     print_bl(C,Log),
  996.     print_nl(Name,Log),
  997.     display_hand(H2,Log).
  998. display_hand([],Log).
  999.  
  1000. pt_card_played(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  1001. Cardp,Suite,Log) :-
  1002.     member(Cardp,Newcards1),
  1003.     card([Cardp,Val,Suite,Name,Pt_taken,Pt_played]),
  1004.     Pt_taken > 0.
  1005.     
  1006. have_higher_card_left(Players,P2,H2,Newplayed1,Newtaken1,Newcards1,
  1007. Cardx,Suite) :-
  1008.     member(Cardx,H2),
  1009.     card([Cardx,Val,Suite,Name,Pt_taken,Pt_played]),
  1010.     not(exists_higher_card_in_list(Newcards1,Suite,Val)).
  1011.     
  1012. exists_higher_card_in_list(Newcards1,Suite,Val) :-
  1013.     card([Card2,Val2,Suite,Name,Pt_taken,Pt_played]),
  1014.     Val2 > Val,
  1015.     member(Card2,Newcards1).
  1016. sort_by_val(H,Hs,Suite) :-
  1017.     get_vals(H,V,Suite),
  1018.     sort(V,Vs),
  1019.     get_nos(Vs,Hs,Suite).
  1020. get_vals([],[],Suite).
  1021. get_vals(H,V,Suite) :-
  1022.     H = [No | Nos],
  1023.     card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  1024.     get_vals(Nos,Vals,Suite),
  1025.     V = [Val | Vals].
  1026. get_nos([],[],Suite).
  1027. get_nos(V,H,Suite) :-
  1028.     V = [Val | Vals],
  1029.     card([No,Val,Suite,Name,Pt_taken,Pt_played]),
  1030.     get_nos(Vals,Nos,Suite),
  1031.     H = [No | Nos].
  1032. minimimd(N1,N2,N1) :- N1 =< N2.
  1033. minimimd(N1,N2,N2) :- N2 =< N1.
  1034. can_bid(Guess,Hand,Suite1,Bidder) :-
  1035.     member(S,[diamonds,clubs,spades,hearts]),
  1036.     can_bid_suite(Guess2,Hand,S,Bidder),
  1037.     Suite1 = S,
  1038.     minimimd(7,Guess2,Guess).
  1039. can_bid(0,Hand,pass,Bidder).
  1040. bind_cs(0.6,0.04,0.04,1.0,0.2,dennis).
  1041. bind_cs(0.6,0.04,0.04,1.0,0.2,dick).
  1042. bind_cs(0.5,0.05,0.05,1.0,0.2,sussy).
  1043. bind_cs(0.5,0.05,0.05,1.0,0.2,john).
  1044. can_bid_suite(Guess,Hand,Suite1,Bidder) :- 
  1045.     discard_non_suite(Hand,Nh1,[],Handsuite,Suite1),
  1046.     get_vals(Handsuite,Vals,Suite1),
  1047.     no_high_cards(Vals,No_high),
  1048. /*    print_bl(no_high,Log),print_nl(No_high,Log), */
  1049.     tot_face(Vals,Tot_face),
  1050. /*    print_bl(no_face,Log),print_nl(Tot_face,Log), */
  1051.     tot_pts(Vals,Tot_pts),
  1052. /*    print_bl(no_pts,Log),print_nl(Tot_pts,Log), */
  1053.     no_sure(Vals,No_sure),
  1054. /*    print_bl(no_sure,Log),print_nl(No_sure,Log), */
  1055.     length(Vals,No_cds),
  1056. /*    print_bl(no_cds,Log),print_nl(No_cds,Log), */
  1057.     bind_cs(C1,C2,C3,C4,C5,Bidder),
  1058.     Guessf1 is C1 * No_high ,
  1059.         Guessf2 is Guessf1 + C2 * Tot_face,
  1060.     Guessf3 is Guessf2 + C3 * Tot_pts,
  1061.     Guessf4 is Guessf3 + C4 * No_sure,
  1062.     Guessf5 is Guessf4 + C5 * No_cds,
  1063.     Guess is floor(Guessf5).
  1064.  
  1065. no_high_cards(Vals,6) :-
  1066.     member(17,Vals),
  1067.     member(16,Vals),
  1068.     member(15,Vals),
  1069.     member(14,Vals),
  1070.     member(13,Vals),
  1071.     member(12,Vals),!.
  1072. no_high_cards(Vals,5) :-
  1073.     member(17,Vals),
  1074.     member(16,Vals),
  1075.     member(15,Vals),
  1076.     member(14,Vals),
  1077.     member(13,Vals),!.
  1078. no_high_cards(Vals,4) :-
  1079.     member(17,Vals),
  1080.     member(16,Vals),
  1081.     member(15,Vals),
  1082.     member(14,Vals),!.
  1083. no_high_cards(Vals,3) :-
  1084.     member(17,Vals),
  1085.     member(16,Vals),
  1086.     member(15,Vals),!.
  1087. no_high_cards(Vals,2) :-
  1088.     member(17,Vals),
  1089.     member(16,Vals),!.
  1090. no_high_cards(Vals,1) :-
  1091.     member(17,Vals),!.
  1092. no_high_cards(Vals,0) :-
  1093.     not(member(17,Vals)),!.
  1094.     
  1095. tot_face([],0). 
  1096. tot_face(Vals,Tot_face) :-
  1097.     Vals = [V1 | Vs],
  1098.     V1 > 10,!,
  1099.     tot_face(Vs,Tot2),
  1100.     Tot_face is V1 + Tot2. 
  1101. tot_face(Vals,Tot_face) :-
  1102.     Vals = [V1 | Vs],
  1103.     V1 =< 10,!,
  1104.     tot_face(Vs,Tot2),
  1105.     Tot_face = Tot2.
  1106. tot_pts([],0).
  1107. tot_pts(Vals,Tot_pts) :-
  1108.     Vals = [V1 | Vs],
  1109.     V1 > 9,
  1110.     V1 < 14,!,
  1111.     tot_pts(Vs,Tot2),
  1112.     Tot_pts is V1 + Tot2. 
  1113. tot_pts(Vals,Tot_pts) :-
  1114.     Vals = [V1 | Vs],
  1115.     V1 < 10,!,
  1116.     tot_pts(Vs,Tot2),
  1117.     Tot_pts = Tot2. 
  1118. tot_pts(Vals,Tot_pts) :-
  1119.     Vals = [V1 | Vs],
  1120.     V1 < 10,!,
  1121.     tot_pts(Vs,Tot2),
  1122.     Tot_pts = Tot2. 
  1123. no_sure(Vals,2) :-
  1124.     member(2,Vals),
  1125.     member(17,Vals),!.
  1126. no_sure(Vals,1) :-
  1127.     member(2,Vals),
  1128.     not(member(17,Vals)),!.
  1129. no_sure(Vals,1) :-
  1130.     not(member(2,Vals)),
  1131.     member(17,Vals),!.
  1132. no_sure(Vals,0) :-
  1133.     not(member(2,Vals)),
  1134.     not(member(17,Vals)),!.
  1135.