Advertisement
edamazzio

cards

Apr 6th, 2018
3,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. 1 • val card_color = fn : card -> color
  3. 2 • val card_value = fn : card -> int
  4. 3 • val remove_card = fn : card list * card * exn -> card list
  5. 4 • val all_same_color = fn : card list -> bool
  6. 5 • val sum_cards = fn : card list -> int
  7. 6 • val score = fn : card list * int -> int
  8. 7 • val officiate = fn : card list * move list * int -> int
  9. *)
  10. datatype suit = Clubs
  11.               | Diamonds
  12.               | Hearts
  13.               | Spades;
  14. datatype rank = Jack
  15.               | Queen
  16.               | King
  17.               | Ace
  18.               | Num of int;
  19. type card = suit * rank;
  20. datatype color = Red | Black
  21. datatype move = Discard of card | Draw
  22. exception IllegalMove
  23.  
  24.  
  25. fun card_color c =
  26.     case c of
  27.        (Clubs, _) => Black
  28.      | (Diamonds, _) => Red
  29.      | (Hearts, _) => Red
  30.      | (Spades, _) => Black;
  31.  
  32.  
  33. fun card_value c =
  34.     case c of
  35.        (_,Jack)  => 10
  36.      | (_,Queen) => 10
  37.      | (_,King) => 10
  38.      | (_,Ace) => 11
  39.      | (_, Num n)=> n;
  40.  
  41. fun remove_card (cl: card list, c : card) =
  42.   case cl of
  43.       []=>[]
  44.       | xs::ys => if c = xs then remove_card(ys,c)
  45.                   else xs::remove_card(ys,c);
  46.  
  47. fun all_same_color cl =
  48.     case cl of
  49.        [] => true
  50.        | hd::nk::tl => (case card_color hd of
  51.                           Black => (card_color nk = Black) andalso (all_same_color (nk::tl))
  52.                         | Red => (card_color nk = Red) andalso (all_same_color (nk::tl)))
  53.       | hd::[] => true
  54.  
  55.  
  56. fun sum_cards [] = 0
  57.   | sum_cards (c as hd::tl) = card_value hd + sum_cards tl;
  58.  
  59.  
  60. fun score ([], _) = 0
  61.   | score (held_cards as hd::tail, goal) =
  62.     let fun preliminaryScore (sum, goal) =
  63.       if sum > goal then 3*(sum-goal)
  64.       else (goal - sum);
  65.     in
  66.       if all_same_color held_cards then floor (real( preliminaryScore(sum_cards held_cards, goal))/ real 2)
  67.       else preliminaryScore(sum_cards held_cards, goal)
  68.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement