Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create an _ _ _ atomic vector _ _ _ ca lled ` deck ` . The _ _ _ object _ _ _ `

Create an ___atomic vector___ ca
lled `deck`. The ___object___`deck` should store 48___elements___ of type ___integer___. Each ___element___ stored in the ___object___`deck` represents a card of the deck and is represented by its value. For example, the ___vector___`c(1L,2L,3L,4L)` represents the cards ace, two, three and four. The order of the cards are not important.
```{r Question_1, echo=TRUE}
# Complete the following code
```
```{r Question_1_test, echo=TRUE, eval=FALSE}
# TEST CODE
# DO NOT EDIT
if (length(deck)!=48) stop("The object deck does not contain 48 elements")
if (typeof(deck)!= "integer") stop("The object deck does not contain integer values")
if (sum(deck)!=300) stop("The values of the cards contained in the deck are incorrect")
if (!all(sapply(deck, is.element, set = c(1:10)))){
stop("The values of the cards contained in the deck are incorrect")}
all(sapply(deck, is.element, set =1:10)) #are all values TRUE?; is.element(x, y) is the same as x %in% y
```
***
Given a deck of cards, we require a method to draw a card from the deck.
___Question 2___(2)
Write a ___function___ called `draw_a_card` with one ___argument___`deck`. The ___function___`draw_a_card` should ___return___ the last ___element___ of the value passed to the `deck` argument
```{r Question_2, echo=TRUE}
# Complete the following code
```
```{r Question_2_test, echo=TRUE, eval=FALSE}
# TEST CODE
# DO NOT EDIT
if (draw_a_card(c(1000,100,10))!=10){
stop("The function does not return the last element")}
if (length(draw_a_card)!=1) stop("The function returns more than one value")
```
***
While the ___function___ defined in ___question 2___ will allow you to draw or select a card from the ___object___`deck`, the ___function___ will always ___return___ the same card. In a normal game of blackjack, the deck will first be shuffled before a card is drawn. We will simplify the functionality of drawing a card, by simply selecting a random card from the values passed to the `deck`___argument___ of the ___function___`draw_a_card` each time the ___function___`draw_a_card` is ___called___.
___Question 3___(2)
Modify the ___function___`draw_a_card` created in ___question 2___, to ___return___ one random card from the values passed to the `deck`___argument___. Use the ___function___`sample()` inside the ___function___`draw_a_card` to ___return___ a random card from the deck.
```{r Question_3, echo=TRUE}
# Complete the following code
```
```{r Question_3_test, echo=TRUE, eval=FALSE}
# TEST CODE
# DO NOT EDIT
set.seed(2)
if (draw_a_card(c(1,2))!= draw_a_card(c(1,2))) stop("Random selection is not performed") #two random draws with the same seed should result in the same draw
```
***
Blackjack is played between a player and a dealer. When the game starts both the player and dealer is dealt two cards each.
___Question 4___(2)
Use the ___function___`draw_a_card`, developed in ___question 3___, to assign two cards to the object `player_hand`. The object `player_hand` should be an atomic vector that stores values as ___integers___.
```{r Question_4, echo=TRUE}
# Complete the following code
```
```{r Question_4_test, echo=TRUE, eval=FALSE}
# TEST CODE
# DO NOT EDIT
if (typeof(player_hand)!= "integer"){
stop("The object player_hand does not contain integer values")}
if (length(player_hand)!=2) stop("The object player_hand does not contain 2 elements")
```
The ___object___`dealer_hand` can be defined in the same way as the ___object___`player_hand`. For now, we will skip defining the ___object___`dealer_hand` as the logic is similar to the ___object___`player_hand`.
***
# Determining the winner
In blackjack, a player wins the round if the score of the player is greater than the score of the dealer given that the score of the player does not exceed 21. The score of the player and the score of the dealer is computed in the exact same way. The score of a hand can be computed as follows:
- If the total value (sum of values) of cards in a hand exceeds 21, known as a bust, assign a score of 0 to the hand
- If a hand has an ace and the total value of the cards in hand is 11 or less (with the ace counting 1), count an ace as the value 11 and not a 1 when computing the total value of cards
- If a hand contains exactly two cards and the total value of cards in a hand is equal to 21, known as a "blackjack", assign a value of 21.5 to the hand
___Question 5___(4)
Create a ___function___ called `score_hand` with a single ___argument___`hand`. The ___function___`score_hand` should compute the score of a hand based on the logic described above. The ___function___ should ___return___ the score as a single ___numeric___ value
```{r Question_5, echo=TRUE}
# Complete the following code
```
```{r Question_5_test, echo=TRUE, eval=FALSE}
# TEST CODE
# DO NOT EDIT
test_cards <- list(c(10,1), c(10,5,6), c(10,1,1),
c(7,6,1,5), c(3,6,1,1),

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

Why is the statement of cash flows a useful document?

Answered: 1 week ago