### C language Engineering Programming

Overview

For this assignment, you will be writing a program to play the card game Intensity.

The Rules of Intensity

Intensity is an unusual card game which is thought to have originated in post-WWII buffalo hunting camps in Australia’s Northern territory. The name intensity is thought to derive from numbers in groups of ten being important.

Cards

It is played with a deck of 40 cards.

The cards are numbered 10 to 49.

No two cards have the same number.

11 cards have special significance. They are the cards numbered 30 to 39, usually called the calves, and the card numbered 47, usually called the buffalo.

Game Play

Intensity is played by four players sitting around a circular table. These players are numbered clockwise: 0, 1, 2 and 3. The deck is divided randomly among the 4 players. So each player starts with a hand of 10 cards.

In the first stage of play, each player selects three cards from their hand to be passed to the player on their left (clockwise). They, of course, then receive three cards from the player on their right. So player 0 passes to player 1, player 1 to player 2, player 2 to player 3 and player 3 to player 0.

The four players simultaneously select the three cards to be passed on. Each player must pass on 3 cards, before receiving the three cards from the player on their right. Hence, you must select the three cards you will pass to the player on your left before you know which three cards you have been given by the player on your right.

Stage 2: Playing Cards

The second stage of play consists of a series of 10 rounds. The goal of the game is to avoid penalty points during these ten rounds.

round begins with a designated player selecting a card from their hand and playing it. Then proceeding clockwise, the other players, in turn, each select a card from their hand and play it.

Hence for the first round, each player will have ten cards in their hand to select from, for the second round they will have nine cards in their hand and so on. For the tenth and last round, each player will have only one card and hence, no choice in the card they play.

Playing a Card

The first digit of the first card played in a round is important. The subsequent players must play a card of the same first digit as the first card if possible.

For example, if the first card played in a round is the 17, the other 3 players must play a card with a first digit of ‘1‘ if they have one in their hand.

If one of the players does not have a card with a first digit of ‘1‘ in their hand they may play a card of another first digit. The following players are still required to play a card with a first digit of ‘1‘ card if they have one.

Winning a Round

A round is won by the person who plays the card of the same first digit as the first card in the round with the largest number. For example, if the first card in a round is the 24, the card with the largest number with first digit ‘2’ will win the round. If no other card with the first digit ‘2’ played the 24 wins.

Player 0 plays the first card of the first round. Subsequently the winner of each round plays the first card of the next round.

If, for example, player 2 won the last round, then player 2 would play the first card of the next round and then players 3, 0 and 1 would play in that order.

Buffalo and Calves

One restriction to the above rules is that a player is not allowed to play a calf (a card in the range 30..39) as the first card in a round unless a calf has been played in any of the previous rounds or they have only calves in their hand. This restriction does not apply to playing a calf as the second, third or fourth card in a round.

In other words, if no cards in the range 30..39 have been previously played, a card in the range 30..39 can not be played as the first card of a round unless the player has no choice.

There is no restriction on when the buffalo can be played as the first card.

Penalty Points

The aim of Intensity is to avoid penalty points. A player scores penalty points if certain cards occur in rounds they have won. Each calf is worth one penalty point and the buffalo (47) is worth 7 penalty points.

Hence, generally players try to avoid winning rounds which contain calves and they particularly try to avoid winning rounds which contain the buffalo.

If a round doesn’t contain a calf or the buffalo it doesn’t affect the score.

When all 10 rounds are played the penalty points are calculated for each player. The winner of the game is the player with the fewest penalty points.

If a player attempts to play an illegal card they receive five penalty points. The referee will instead select a legal card from their hand at random. This same applies to discards.

To fully understand these rules, it might be worth watching this tutor-made video which runs through an example game of intensity.

Implementation

Intensity in C

Your task in this assignment is to write a C program which plays Intensity well.

Your program will need to take in relevant information about the state of the game, use that information to determine which move it should make, and then make that move.

Your program will be given this information as a series of integers on standard input, which describe the state of the game and indicate what it is being asked to do.

There are three sub-tasks that your program will be asked to do, over the course of a game of Intensity:

2. Select the cards to discard
3. Select which card to play

Your program will be run multiple times during the game: once for each “decision” that it must make.

For example, your program will be run once to select which 3 cards it wishes to pass on. This occurs once at the start of the hand. When it is your turn to play, your program will be asked which card it wishes to play. This will happen 10 times during the playing of a hand.

Intensity in C – Printing Your Player Name

If the first integer on standard input is 0, your program is being asked to print the name of your player. Your program will be run once in this way at the start of the game.

Your program should print your chosen name as a single line. The name can be anything you like – but please keep it tasteful – definitely nothing offensive or obscene. There is a limit of 32 characters.

A suffix will be added if needed to make your name unique.

For example:

dcc intensity.c -o intensity

./intensity

0

Dennis Ritchie

Intensity in C – Selecting Discards

If the first integer on standard input is 1, your program is being asked to select the 3 cards it wishes to discard. Your program will be run once in this way at the start of the game.

The first integer (1) will be followed by 10 integers which are the cards in your hand. These will all be numbers in the range 10..49. They will be in sorted (increasing) order.

You should read these 10 integers into an array, and then choose which 3 you wish to discard.

You program should then indicate the 3 cards it wishes to discard by printing a single line containing 3 integers. Each integer should be one of the cards in your hand and hence must be in the range 10..49.

For example:

dcc intensity.c -o intensity

./intensity

1

13 19 24 25 29 32 39 44 46 49

29 39 49

In the above example the player has these cards in its hand: [13, 19, 24, 25, 29, 32, 39, 44, 46, 49]

It chooses to discard this 3 cards: [29, 39, 49].

Intensity in C – Playing A Card

If the first integer on standard input is 2, your program is being asked to play a card. Your program will be run ten times in this way during a game.

The first integer (2) will be followed by 3 more integers:

1. how many cards are in your hand. This will be in the range 1..10.
2. how many cards have already been played this round . This will be in the range 0..3.
3. your table position. This will be in the range 0..3.

Next will be the cards in your hand. This will be 1..10 integers in the range 10..49 They will be in sorted (increasing) order.

You should use scanf to read these into an array. Note you have already read a value telling you how many cards are in your hand so you know how many values to read

Cards Played So Far This Round

Next will the cards that have been played so far this round. This will be 0..3 integers in the range 10..49 They will be in the order they were played – the first will be the first card played in this round.

You should also use scanf read to these into an array. Note you have already read a value telling you how many cards have been played this round so know how many values to read.

Cards Played In Previous Rounds

Next will be all the cards played in previous rounds. This will be 0..36 integers in the range 10..49. They will be in the order they were played – the first will be the first card played in the game.

Again you should also use scanf read to these into an array. You can calculate how many values to expect from the number of cards left in your hand. For example if there are 7 cards in your hand there have been 3 previous rounds so there have 3*4 = 12 cards played in previous round

You can work out which player played each of the previous cards, because you know the player in table position 0 played first in the first round, and you know the player who won the first round played first in the second round, and so on.

Next will be the cards your player discarded at the start of the game. This will be 3 integers in the range 10..49. They will be in sorted (increasing) order. These will only be of interest if you are implementing a clever strategy.

Last will be the cards your player received in the discard round at the start of the game. This will be 3 integers in the range 10..49 They will be in sorted (increasing) order. These will only be of interest if you are implementing a clever strategy.

Playing A Card

Your program should then indicate the card it wishes to play by printing a single line containing an integer. This integer should be one of the cards in your hand and hence must be in the range 10..49.

For example:

dcc intensity.c -o intensity

./intensity

2

9 2 0

11 15 18 23 33 34 47 48 49

24 26

16 13 19 12

37 38 39

34 47 48

23

In the above example the player has 9 cards in its hand: [11, 15, 18, 23, 33, 34, 47, 48, 49]

Two cards played have been played this round: [24, 26].

There has been 1 previous round in which the cards played were: [16, 13, 19, 12]

The player is sitting at table position 0.

The program played the only card that is legal to play in this situation, the 23.

Intensity in C – Unit tests

If the first integer on standard input is 3, your program is being asked to run the unit tests you have added to run_unit_tests

Playing Intensity

The Intensity Referee

You do not have to write C to deal the cards, keep score or manage the playing of the game.

You are given a referee program which does this.

It takes as arguments, the names of 1 or more C programs which play the game. It compiles the programs and runs them giving them input as described above.

If there are less than 4 programs, the referee provides players (named LuluMorganAmy and Rat) who make random legal plays.

Run it like this:

1511 intensity_referee intensity.c

Version: 0.2

dcc -o intensity intensity_random.c

dcc –valgrind -o intensity-valgrind intensity_random.c

Deal:

Table position 0: COMP1511 student: [10, 22, 23, 24, 28, 29, 35, 38, 41, 42]

Table position 1: Lulu            : [11, 12, 15, 17, 21, 25, 27, 43, 44, 47]

Table position 2: Amy             : [20, 26, 30, 32, 33, 36, 37, 39, 40, 45]

Table position 3: Morgan          : [13, 14, 16, 18, 19, 31, 34, 46, 48, 49]

Interactive Intensity

An interactive Intensity player is available. It allows the (human) user to interactively choose the plays for one player.

Playing against Lulu, Amy and Morgan is not challenging but it may improve your understanding of the game.

Run it like this:

1511 intensity_referee -i

Version: 0.2

Hand: [11, 12, 18, 19, 26, 28, 32, 33, 35, 43]

Enter cards to discard: 32 33 35

Round 0: Morgan             plays the 10

Round 0: Amy                plays the 15

Round 0: Lulu               plays the 16

Interactive Player turn to play.

Hand: [11, 12, 18, 19, 21, 22, 26, 28, 39, 43]

Enter card to play: