Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- 1 • val card_color = fn : card -> color
- 2 • val card_value = fn : card -> int
- 3 • val remove_card = fn : card list * card * exn -> card list
- 4 • val all_same_color = fn : card list -> bool
- 5 • val sum_cards = fn : card list -> int
- 6 • val score = fn : card list * int -> int
- 7 • val officiate = fn : card list * move list * int -> int
- *)
- datatype suit = Clubs
- | Diamonds
- | Hearts
- | Spades;
- datatype rank = Jack
- | Queen
- | King
- | Ace
- | Num of int;
- type card = suit * rank;
- datatype color = Red | Black
- datatype move = Discard of card | Draw
- exception IllegalMove
- fun card_color c =
- case c of
- (Clubs, _) => Black
- | (Diamonds, _) => Red
- | (Hearts, _) => Red
- | (Spades, _) => Black;
- fun card_value c =
- case c of
- (_,Jack) => 10
- | (_,Queen) => 10
- | (_,King) => 10
- | (_,Ace) => 11
- | (_, Num n)=> n;
- fun remove_card (cl: card list, c : card) =
- case cl of
- []=>[]
- | xs::ys => if c = xs then remove_card(ys,c)
- else xs::remove_card(ys,c);
- fun all_same_color cl =
- case cl of
- [] => true
- | hd::nk::tl => (case card_color hd of
- Black => (card_color nk = Black) andalso (all_same_color (nk::tl))
- | Red => (card_color nk = Red) andalso (all_same_color (nk::tl)))
- | hd::[] => true
- fun sum_cards [] = 0
- | sum_cards (c as hd::tl) = card_value hd + sum_cards tl;
- fun score ([], _) = 0
- | score (held_cards as hd::tail, goal) =
- let fun preliminaryScore (sum, goal) =
- if sum > goal then 3*(sum-goal)
- else (goal - sum);
- in
- if all_same_color held_cards then floor (real( preliminaryScore(sum_cards held_cards, goal))/ real 2)
- else preliminaryScore(sum_cards held_cards, goal)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement