Lab 16
------
This was the card game. It was marked out of 20. The first 10 marks
were for the extent to which your program worked correctly and had the
right output. The second 10 marks were for your design - the lab sheet
explicitly told you to split it up into functions.
There is an infinite number of solutions, but below is one that I threw
together....
Cards on the table
';
}
// Card suits
$SUITS = array(
'♥', '♦', '♠', '♣');
// Card values
$VALUES = array(
'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K');
// Create card
function create_card( $suit, $value )
{
return array('suit' => $suit, 'value' => $value);
}
// Returns a card's colour
function get_colour( $card )
{
$SUITS_COLOURS = array(
'♥' => 'red', '♦' => 'red',
'♠' => 'black', '♣' => 'black');
return $SUITS_COLOURS[ $card['suit'] ];
}
// Returns an HTML representation of a card
function card_to_HTML( $card )
{
return "
{$card['value']}{$card['suit']}
";
}
// Creates a pack of cards
function create_cards( $suits, $values )
{
$cards = array();
foreach ( $suits as $suit )
{
foreach ( $values as $value )
{
$cards[] = create_card( $suit, $value );
}
}
return $cards;
}
// Shuffles a pack of cards by swapping the cards in randomly-chosen positions
function shuffle_cards( &$cards )
{
$n = count( $cards );
for ( $i = 0; $i < $n; $i++ )
{
$j = rand(0, $n - 1);
$tmp = $cards[$i];
$cards[$i] = $cards[$j];
$cards[$j] = $tmp;
}
}
// Returns an HTML representation of a colour sequence
function colour_seq_to_HTML( $colour_seq )
{
$html = '';
foreach ( $colour_seq as $colour )
{
$html .= "{$colour} ";
}
return trim( $html );
}
// Choose a sequence of $n colours which must be a different sequence from
// $tabu_colour_seq
function choose_colour_seq( $n, &$tabu_colour_seq )
{
do
{
$colour_seq = array();
for ( $i = 0; $i < $n; $i++ )
{
$colour_seq[] = rand(0, 1) == 0 ? 'red' : 'black';
}
} while ( are_equal_colour_seqs( $colour_seq, $tabu_colour_seq ) );
return $colour_seq;
}
// Returns true if two sequences of colours are equal, false otherwise
function are_equal_colour_seqs( &$colour_seq_a, &$colour_seq_b )
{
$n = count( $colour_seq_a );
if ( $n != count( $colour_seq_b ) )
{
return false;
}
for ( $i = 0; $i < $n; $i++ )
{
if ( $colour_seq_a[$i] != $colour_seq_b[$i] )
{
return false;
}
}
return true;
}
// Returns true if the cards at the end of $cards have the same colours as in
// $colour_seq, false otherwise
function is_colour_suffix( &$colour_seq, &$cards )
{
$n = count( $colour_seq );
if ( count( $cards ) < $n )
{
return false;
}
$cards_suffix = array_slice( $cards, -$n );
for ( $i = 0; $i < $n; $i++ )
{
if ( $colour_seq[$i] != get_colour( $cards_suffix[$i] ) )
{
return false;
}
}
return true;
}
// Returns a singular or plural phrase, e.g. 1 trick, 2 tricks
function to_amount( $n, $noun )
{
return "{$n} {$noun}" . ($n == 1 ? '' : 's');
}
// Outputs game play and outcome
function output_game( $u_colour_seq, $c_colour_seq,
$output,
$u_num_tricks, $c_num_tricks )
{
echo '
User chose ' . colour_seq_to_HTML( $u_colour_seq ) . '.