# 22 December 2016 - AM

# Fondamenti di Informatica

### Prof. Marco Gavanelli

### 22 December 2016 (AM)

## 1 Exercise (points 16)

BlackJack is a game that is played with 52 cards; each card has

- an integer number from 1 to 13
- a suite: string, that can be "cuori" (hearts), "quadri" (diamonds), "fiori" (clubs) or "picche" (spades).

Each card is worth a score:

- 1 (also called "ace") can count as 1 point or 11 points, according to the player's choice
- cards from 2 to 10 have their natural value (a 2 is worth 2 points, a 3 is worth 3 points, ... a 10 is worth 10 points)
- cards from 11 to 13 are worth 10 points each.

Each player asks how many cards as he wants.

If his total score is 22 or more, he "busted" and he loses the game.

The player with the highest score wins; thus the maximum possible score is 21.

Two files are given; one binary file `banco.bin` contains the cards of player 1, while the binary file `giocatore.bin` contains the cards of player 2.

Write a C program that computes which player wins the game, considering that in case the two players have the same score, player 1 wins.

For example, if the file `banco.bin` contains:

1 | picche |

8 | quadri |

and the file `giocatore.bin` contains:

13 | fiori |

7 | quadri |

1 | cuori |

the program should print

```
```player 1 wins with 19 points

Organize the program as follows:

- in the
`main`

function,- invoke twice a function (or procedure) that reads a file (to be developed in point 2): the first time the function reads file
`banco.bin`, the second it reads file`giocatore.bin`; - invoke a procedure/function (to be developed in point 3) that uses the arrays read in point 2 and computes which player wins and with which score
- finally, print the winning player and the score computed at point 3

- invoke twice a function (or procedure) that reads a file (to be developed in point 2): the first time the function reads file
- write a procedure of function that reads a file into an array of structures; this function should also print the content of the array on the screen.
- write a procedure or function that takes as parameters
- the two arrays that have been read (by invoking in the
`main`

program twice the function developed on point 2)

`main`

- an integer that represents the winning player (either 1 or 2)
- the score of the winner

- the two arrays that have been read (by invoking in the
- write a function that, given an array of cards (plus, possibly, other parameters), provides the total score of a player. In case there are some aces, the function should count it in the best way for the player (either as 1 or as 11 points). Remember that it is most convenient to get as close as possible to 21, without ever exceeding 21. Note that if there are two or more aces, it does not make sense to have two aces count as 11, because the player would score more than 22. This means that at most one ace counts as 11. Moreover, an ace can count as 11 only if the total of the other cards is lower than or equal to 10.

It is strictly necessary to organize the program into procedures and functions; it is highly advisable to add further functions beside those strictly necessary required in the text.

## 2 Exercise (points 4)

Consider now the game of simplified Poker.

In simplified Poker each player has 5 cards; the player that has more cards holding the same number wins.

In case of parity, player 1 wins.

For example, if player 1 has the following cards:

2 | picche |

3 | fiori |

2 | fiori |

3 | cuori |

3 | quadri |

and the second has:

4 | fiori |

12 | cuori |

12 | quadri |

12 | picche |

12 | fiori |

then player 2 wins, since player 1 has three cards with the same number (3 fiori, 3 cuori, 3 quadri), while the second has four (12 cuori, 12 quadri, 12 picche e 12 fiori).

Modify the program so that it provides the winner of simplified Poker (instead of BlackJack).

The cards of the first player are in binary file `dice1.bin`, while those of the second are in file `dice2.bin.`