ML Assignment: due ?.
For this assignment, you are going to implement that same thing as
you did for
your Java and Scheme projects, the clock patience program. To help you along, we're
giving you the ML code that takes the input card list and
builds a data structure to hold the results of the deal. You can use
this code as a starting point, or write it yourself if you like.
The code is:
(***************************************************************
each card is represented as a list. For example,
the queen of spades is ["Q", "S"]
The hand itself is stored in 13 'stacks', from the
Ace stack to King. To make this searchable, the
name of the stack is stored with the stack. For
example ["5", ["K", "D"], ["2", "C"]] means that the stack associated
with 5 has a K of diamonds on top of a two of clubs.
So, an entire hand might look something like:
[["A", ["J", "C"], ["2", "H"], ["3", "S"],["4", "D"]], ["2", ["5", "D"],
["Q", "D"], ["7", "H"], ["2", "C"]] ..., ["K" ...]]
To run: sml
- use "patience.sml";
The cards are stacked onto the piles from left to right,
the rightmost card is top (st.push() adds elements from left to right)
To play:
- use "patience.sml"
- val piles = play();
***************************************************************)
fun sampleHand() : string list list =
[["T","S"],["Q","C"],["8","S"],["8","D"],["Q","H"],["2","D"],["3","H"],["K","H"],["9","H"],["2","H"],["T","H"],["K","S"],
["K","C"],["9","D"],["J","H"],["7","H"],["J","D"],["2","S"],["Q","S"],["T", "D"],["2","C"],["4","H"],["5","H"],["A","D"],
["4","D"],["5","D"],["6","D"],["4", "S"],["9","S"],["5","S"],["7","S"],["J","S"],["8","H"],["3","D"],["8","C"],["3","S"],
["4","C"],["6","S"],["9","C"],["A","S"],["7","C"],["A","H"],["6","H"],["K","D"],["J","C"],["7","D"],["A","C"],["5","C"],
["T","C"],["Q","D"],["6","C"],["3","C"]];
fun initialHand():(string * (string list list)) list =
[("A",[]),("2",[]),("3",[]),("4",[]),("5",[]),("6",[]),("7",[]),("8",[]),("9",[]),("T",[]),("J",[]),("Q",[]),("K",[])];
(* [ ("2", [["T","S"]]), ("2", [["T","S"]]), ("8",[[]])];
*)
fun addStack(rank:string, card: string list,hand:(string * (string list list)) list) =
if #1(hd(hand)) = rank then
(#1(hd(hand)), card::(#2(hd(hand))))::(tl(hand))
else
(hd(hand)) :: addStack(rank, card, tl(hand));
fun nextRank(rank:string):string = (* this function just facilitates dealing *)
if rank = "K" then "Q"
else if rank = "Q" then "J"
else if rank = "J" then "T"
else if rank = "T" then "9"
else if rank = "9" then "8"
else if rank = "8" then "7"
else if rank = "7" then "6"
else if rank = "6" then "5"
else if rank = "5" then "4"
else if rank = "4" then "3"
else if rank = "3" then "2"
else if rank = "2" then "A"
else "K";
fun deal(cards:string list list, rank:string):(string * (string list list)) list =
if cards = nil then
initialHand()
else addStack(rank, hd(cards), deal(tl(cards), nextRank(rank)));
fun play() =
deal(sampleHand(),"K");
fun getnth(lis:string list, n : int) : string =
if n = 0 then hd(lis)
else getnth (tl(lis), n-1);
(*
- use "patience.sml"
- val piles = play();
val piles =
[("A",[["K","C"],["5","D"],["9","C"],["3","C"]]),
("2",[["K","S"],["4","D"],["6","S"],["6","C"]]),
("3",[["T","H"],["A","D"],["4","C"],["Q","D"]]),
("4",[["2","H"],["5","H"],["3","S"],["T","C"]]),
("5",[["9","H"],["4","H"],["8","C"],["5","C"]]),
("6",[["K","H"],["2","C"],["3","D"],["A","C"]]),
("7",[["3","H"],["T","D"],["8","H"],["7","D"]]),
("8",[["2","D"],["Q","S"],["J","S"],["J","C"]]),
("9",[["Q","H"],["2","S"],["7","S"],["K","D"]]),
("T",[["8","D"],["J","D"],["5","S"],["6","H"]]),
("J",[["8","S"],["7","H"],["9","S"],["A","H"]]),
("Q",[["Q","C"],["J","H"],["4","S"],["7","C"]]),...]
: (string * string list list) list
*)