CS1109 Lab 16

Put your work for this lab into a new folder public_html\cs1109\lab16.

William Penney's Card Game

It's time for you to design a larger program for yourself. A significant proportion of the marks will go towards the quality of your design.

The key is to break the program up into manageable functions. Define and test each of them individually. Only when each of them is perfect should you try to write the main program.

You are given the form: game.html; take a copy of it; do not edit it. You must write game.php. You may use any built-in functions that you wish, but do not use the built-in shuffle function.

The game is played with a regular pack of 52 cards: Ace to King of Hearts, Diamonds, Spades and Clubs. Obviously, half these cards are red (Hearts and Diamonds), and half are black (Spades and Clubs).

At the start of the game, the user uses the radio buttons to supply a sequence of three colours, e.g. s/he might choose red, red, red or s/he might choose red, black, black, and so on.

The computer also chooses a sequence of three colours. It is required that this sequence is different from the one that the user has chosen.

Next, the pack of cards is created and shuffled.

Visit each card in the pack in turn, noting its colour. As soon as the user's sequence of colours or the computer's sequence of colours occurs, that player wins a 'trick'.

All cards that have been inspected up to that point are ignored, and the game resumes with the remaining cards, again looking to match either the user's colours or the computer's colours.

This continues until all 52 cards have been visited. The winner is whoever has the most 'tricks', or the game is a draw if they have the same number of 'tricks'.

At this point, your program will output all the cards, 'tricks' and the final outcome.

Here is an example of the output of the program:

User chose red black red.

Computer chose black black black.

5♣ 7♦ 4♥ 5♥ K♠ A♦ User wins trick 2♥ A♠ 8♠ 8♦ 9♦ 9♠ 9♣ 5♦ 2♣ 4♣ 6♦ 6♥ 3♦ K♥ 10♠ K♦ User wins trick Q♦ A♥ 10♥ 4♦ J♦ 3♣ 2♠ 8♣ Computer wins trick J♣ 10♦ 6♣ 7♥ User wins trick K♣ 10♣ Q♥ 8♥ 3♠ 7♠ 2♦ 4♠ 6♠ J♠ Computer wins trick 7♣ Q♣ Q♠ Computer wins trick J♥ 5♠ A♣ 9♥ 3♥

User has 3 tricks and computer has 3 tricks. Therefore, it's a draw.

It may help to include a stylesheet as I did. If so, call it game.css.

If you cannot complete the program then (a) do not plagiarise, and (b) at the least, remove any compile-time errors and use comments to sketch out the functions that you wanted to write (but couldn't) — there may be some partial credit if you do.

Deadline: 1pm, Tuesday 26th February.

If you have named your files and folders correctly, your work will be collected automatically at that time by my software.

Challenge exercise

There's no challenge exercise this week. Spend the time perfecting your solution to the main exercise.