联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2020-10-23 10:25

Programming Task #4: It’s in the Cards!

Version 1.0: 8 October 2020

Academic Integrity

As a reminder of the course honor policy, all work submitted for this programming task must be

yours alone. Refer to the General Information about Programming Tasks section of Blackboard for more

details about what is permitted and not permitted.

Learning Targets

This programming task relates directly to the following Learning Target(s):

? F.3: I can develop computational solutions to simple word problems using Haskell conditional equations

and common Prelude (and Data.Char) functions and operators. (core)

? R.2: I can write correct and concise code that uses recursion over lists and/or numbers. (core)

? P.3: I can use pattern matching and algebraic datatypes to write data-directed functions/programs.

It also relates indirectly to the following Learning Target:

? P.4: I can convert between the following list-processing approaches: recursion with patterns, list

comprehensions, and higher-order functions (map and filter).

Before You Start

Grab a copy of the file Cards.hs this file, and place it in the same directory where you will write your code.

Make sure you save it with this name and capitalization. However, do not add your code to this file:

instead, include an import Cards directive at the top of your file.

Background

This assignment involves a Haskell representations of standard playing cards and packs of these cards. The

standard (Anglo-American-French) pack of playing cards consists of 52 cards organized into four suits: Clubs,

Diamonds, Hearts, and Spades. Each suit has 13 cards: an Ace, a Two, a Three, . . . , a Nine, a Ten, a Jack, a

Queen, and a King. We represent suits and then cards by the following types:

data Suit = Clubs | Diamonds | Hearts | Spades

deriving (Eq, Ord, Enum, Show)

data Card = Cd Suit Int

deriving (Eq)

The intention is that the Int value should be between 1 and 13 inclusive (for Aces through Kings). For

example, Cd Spades 1 represents the Ace of Spades, Cd Clubs 5 represents the Five of Clubs, and Cd

Hearts 12 represents the Queen of Hearts. We represent packs (i.e., collection of cards) with values of the

following type:

1

type Pack = [Card]

These definitions (along with a few more definitions and predefined Packs) are in the Cards.hs module.

Your Problems

In writing your functions, follow these conventions:

? Some functions may be easier to write recursively, and others may be easier to write with list comprehensions.

Unless otherwise stated, you may use whichever strategy you prefer.

? Make use of previously defined functions if it makes sense to do so. It’s also often wise to make use of

built-in functions we’ve talked about. where possible.

Problem 1: Go Fish

In a turn of the game of Go Fish, the player whose turn it is chooses the number of a card she holds (e.g.,

if she holds the six of Clubs, she might choose 6) and asks the other players if they have any cards of that

number (“Does anyone have any sixes?”). All the other players give her all of their cards of that number.

However, if none of the players have cards of that number, then the player has to go fish (i.e., take the top

card from the deck). In this problem, you will develop a function to carry out a single go-fish turn for two

players.

1. Write a Haskell function

haveAny :: Int -> Pack -> Bool

such that haveAny n hand evaluates to True exactly when there is a Card in hand with value n.

Examples:

? haveAny 6 [Cd Clubs 2, Cd Spades 6, Cd Hearts 12] evaluates to True

? haveAny 8 [Cd Clubs 2, Cd Spades 6, Cd Hearts 12] evaluates to True

2. Write a Haskell function

pickOut :: Int -> Pack -> (Pack,Pack)

such that pickout n hand evaluates to a pair (h1,h2), where h1 is the list of all the cards in hand

with value n and h2 is the list of all the cards in hand whose value is different from n.

Examples:

? pickOut 6 [Cd Clubs 2, Cd Spades 6, Cd Hearts 12] evaluates to ([Cd Spades 6],[Cd

Clubs 2, Cd Hearts 12])

? pickOut 3 [Cd Clubs 2, Cd Spades 6, Cd Hearts 12] evaluates to ([],[Cd Clubs 2, Cd

Spades 6, Cd Hearts 12])

3. The starter file Cards.hs contains the following type synonym:

type State = (Pack,Pack,Pack)

A triple (p1hand,p2hand,deck) of type State represents the state of a two-player Go Fish game,

where p1hand is the hand of the first player, p2hand is the hand of the second player, and deck is the

list of cards remaining in the deck.

Write a Haskell function

turn :: Int -> State -> State

2

such that turn n st returns the state of the game obtained when Player 1 (beginning in state st) asks

for all cards with value n. There are two special cases where the state doesn’t change:

? If Player 1 cheats by asking for value n without holding any cards with n, then nothing changes in

the game: the new state is the old state st.

? If Player 1 needs to go fish and the deck is empty, then nothing changes: the new state is the old

state st.

Here are some examples:

? Player 1 successfully gets sixes from Player 2:

turn 6 ([Cd Spades 6,Cd Hearts 12], [Cd Hearts 6,Cd Diamonds 1,Cd Clubs 6], deck)

evaluates to:

([Cd Hearts 6,Cd Clubs 6,Cd Spades 6,Cd Hearts 12], [Cd Diamonds 1],deck)

? Player 1 has to go fish from the deck:

turn 12 ([Cd Spades 6,Cd Hearts 12], [Cd Hearts 6,Cd Diamonds 1,Cd Clubs 6],[Cd

Hearts 9, rest-of-deck])

evaluates to:

([Cd Hearts 9,Cd Spades 6,Cd Hearts 12],[Cd Hearts 6, Cd Diamonds 1, Cd Clubs

6], [ rest-of-deck])

Problem 2: Blackjack

In the card game Blackjack, a hand of cards is scored by the following rules:

? Each card has an associated number of points:

– Face cards (Jack, Queen, King) are worth 10 points.

– Numeric cards (Two through Ten) are worth their number of points (e.g., a Six is worth 6 points).

– Aces are worth etierh 1 point or 11 points (the player gets to choose).

? Given a hand of cards, you sum up the points for all of the cards. If the sum is 21 or lower, that sum is

the score; if the sum is greater than 21, the score is 0.

? Your goal is to have the highest score possible. Thus, if you have an Ace in your hand, you will choose

its value accordingly.

Here are some examples:

? A hand containing two Kings and a Five has 25 points, so the hand’s score is 0.

? A hand containing a Queen and two Fours has 18 points, so the hand’s score is 18.

? A hand containing an Ace, a Jack, and a Five has either 26 points (which would be a score of 0) or 16

points (which would be a score of 16): a smart player would choose the score of 16.

Write a Haskell function

score :: Pack -> Int

such that score hand returns the best possible score of hand.

Two hints: (1) No matter how many Aces your hand has, you will never use more than one of them as an

11-point card (why?). (2) Your haveAny function is useful here.

3

Problem 3: N-Way Deals

In this problem, you will work out a way to deal a pack of cards to multiple players.

1. Write a Haskell function

distribute :: [a] -> [[a]] -> [[a]]

such that distribute xs yss evaluates to the result of cons-ing the i-th element of xs onto the front

of the i-th element of yss. Depending on the relative lengths of xs and yss, we have to be careful:

? If xs is shorter than yss (let’s use n as the length of xs), then the first n elements of yss get an

item cons-ed onto them and the other elements remain unchanged.

? If xs is longer than yss, then the extra values in xs are not used.

Here are some examples:

? distribute [6,2,8] [[10,100],[30,300],[70]] evaluates to [[6,10,100],[2,30,300],[8,70]]

? distribute [6,2,8] [[10,100],[30,300],[70],[90]] evaluates to

[[6,10,100],[2,30,300],[8,70],[90]]

? distribute [6,2,8] [[10,100],[30,300]] evaluates to [[6,10,100],[2,30,300]]

2. Write a Haskell function

deal :: Int -> Pack -> [Pack]

such that deal n deck evaluates to the list obtained by dealing the cards in deck into n packs. In

dealing out cards, the first card of deck should go into the first output pack, the second card of deck

should go into the second output pack, and so on, in a round-robin fashion. (When n is zero or negative,

an empty list should be returned.)

Here are some examples:

? deal 2 [Cd Hearts 3,Cd Diamonds 10, Cd Diamonds 1, Cd Clubs 4, Cd Hearts 9,Cd

Spades 1] evaluates to [[Cd Hearts 3, Cd Diamonds 1, Cd Hearts 9], [Cd Diamonds 10,

Cd Clubs 4, Cd Spades 1]]

? deal 3 [Cd Hearts 3,Cd Diamonds 10, Cd Diamonds 1, Cd Clubs 4, Cd Hearts 9,Cd

Spades 1] evaluates to [[Cd Hearts 3,Cd Clubs 4], [Cd Diamonds 10,Cd Hearts 9]

[Cd Diamonds 1,Cd Spades 1]]

? deal 4 [Cd Hearts 3,Cd Diamonds 10, Cd Diamonds 1, Cd Clubs 4, Cd Hearts 9,Cd

Spades 1] evaluates to [[Cd Hearts 3,Cd Hearts 9],[Cd Diamonds 10,Cd Spades 1],

[Cd Diamonds 1], [Cd Clubs 4] ]

Hint: The actual amount of code you need to write for this function is pretty small: the challenge is to

identify a couple built-in list-processing functions (that we’ve talked about in lecture!) and figure out

how to put them together with distribute to accomplish what’s needed.

What to Submit

You should submit a single Haskell file that contains the following:

? A comment with your name and syr.edu email address at the top of the file.

? Functions should be given the stated names, have correct type signatures, and appear in the order

listed in this task writeup

? For each of the requested functions, you should provide:

4

– A brief comment that describes the purpose of the function (this can be taken from the task

description itself)

– The function definition, including the type signature

– Specific test cases that can be used to verify the correctness of your function (these tests should

be designed before and as you write your code, not as an afterthought)

How it Will be Graded

Programming tasks are graded using the EMRN rubric found in the syllabus. A grade of E or M requires

that all of the following criteria must be met:

? The code meets the submission requirements listed above.

? Your code is reasonably concise and works correctly on test data that we prepare separately.

? Your test cases are reasonably comprehensive (i.e., well chosen to uncover most errors).

5


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp