How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Scott Your Own Question

Scott
Scott, MIT Graduate
Category: Homework
Satisfied Customers: 3040
Experience:  MIT Graduate (Math, Programming, Science, and Music)
3546829
Type Your Homework Question Here...
Scott is online now
A new question is answered every 9 seconds

This problem involves a solitaire card game invented just

Customer Question

This problem involves a solitaire card game invented just for this question. You will write a program that tracks the progress of a game; writing a game player is a challenge problem. You can do parts (a)–(e) before understanding the game if you wish.A game is played with a card-list and a goal. The player has a list of held-cards, initially empty. The player makes a move by either drawing, which means removing the first card in the card-list from the card-list and adding it to the held-cards, or discarding, which means choosing one of the held-cards to remove. The game ends either when the player chooses to make no more moves or when the sum of the values of the held-cards is greater than the goal.The objective is to end the game with a low score (0 is best). Scoring works as follows: Let sum be the sum of the values of the held-cards. If sum is greater than goal, the preliminary score is five times (sum − goal), else the preliminary score is (goal − sum). The score is the preliminary score unless all the held-cards are the same color, in which case the score is the preliminary score divided by 2 (and rounded down as usual with integer division; use ML’s div operator).a) Write a function card_color, which takes a card and returns its color (spades and clubs are black, diamonds and hearts are red). Note: One case-expression is enough.
b) Write a function card_value, which takes a card and returns its value (numbered cards have their number as the value, aces are 11, everything else is 10). Note: One case-expression is enough.
c) Write a function remove_card, which takes a list of cards cs, a card c, and an exception e. It returns a list that has all the elements of cs except c. If c is in the list more than once, remove only the first one. If c is not in the list, raise the exception e. You can compare cards with =.
d) Write a function all_same_color, which takes a list of cards and returns true if all the cards in the list are the same color. Hint: An elegant solution uses nested pattern-matching.
e) Write a function sum_cards, which takes a list of cards and returns the sum of their values.
• For bonus points, write a function sum_cards_tr, which takes a list of cards and returns the sume of their values; however, it should make use of a locally defined helper function that is tail recursive.
f) Write a function score, which takes a card list (the held-cards) and an int (the goal) and computes the score as described above.
g) Write a function officiate, which “runs a game.” It takes a card list (the card-list) a move list (what the player “does” at each point), and an int (the goal) and returns the score at the end of the game after processing (some or all of) the moves in the move list in order. Use a locally defined recursive helper function that takes several arguments that together represent the current state of the game. As described above:
• The game starts with the held-cards being the empty list.
• The game ends if there are no more moves. (The player chose to stop since the move list is empty.)
• If the player discards some card c, play continues (i.e., make a recursive call) with the held-cards not having c and the card-list unchanged. If c is not in the held-cards, raise the IllegalMove exception.
• If the player draws and the card-list is (already) empty, the game is over. Else if drawing causes the sum of the held-cards to exceed the goal, the game is over (after drawing). Else play continues with a larger held-cards and a smaller card-list.
Sample solution for (g) is under 25 lines.
Submitted: 2 months ago.
Category: Homework
Customer: replied 2 months ago.
Complete assignment attached.
Customer: replied 2 months ago.
CSCI485 Assignment 3 Provided Code *)(* You may assume that Num is always used with values 2, 3, ..., 10
though it will not really come up *)
type suit = Clubs | Diamonds | Hearts | Spades;;
type rank = Jack | Queen | King | Ace | Num of int;;
type card = suit * rank;;type color = Red | Black;;
type move = Discard of card | Draw;;exception IllegalMove;;(* Put your solutions for Problem A through G here *)(* Problem A *)let card_color card =
match card with
| (Hearts, _) -> Red
| (Diamonds, _) -> Red
| (Spades, _) -> Black
| (Clubs, _) -> Black;;(* Put any test code that you write below here *)
Expert:  Bruce Wilner replied 2 months ago.

What language is this?

Can I provide the solution in Java, for you to translate? I don't perceive a whole lot of difference, except for the extremely silly ';;' notation.

Expert:  Bruce Wilner replied 2 months ago.

SWIFT?