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 ) . '.

'; echo '

Computer chose ' . colour_seq_to_HTML( \$c_colour_seq ) . '.

'; echo \$output; echo '

User has ' . to_amount( \$u_num_tricks, 'trick' ) . ' and computer has ' . to_amount( \$c_num_tricks, 'trick' ) . '. Therefore, ' . (\$u_num_tricks > \$c_num_tricks ? ' user wins.' : ( \$u_num_tricks < \$c_num_tricks ? ' computer wins.' : ' it\'s a draw.')) . '

'; } // MAIN PROGRAM // Get the user's colour sequence. // (We should validate it, but I omitted this here to make things shorter) \$u_colour_seq = array(\$_GET['colour1'], \$_GET['colour2'], \$_GET['colour3']); // Get computer's colour sequence \$c_colour_seq = choose_colour_seq( 3, \$u_colour_seq ); // Create and shuffle a pack of cards \$cards = create_cards( \$SUITS, \$VALUES ); shuffle_cards( \$cards ); // Initialize game state \$turned_up_cards = array(); \$u_num_tricks = 0; \$c_num_tricks = 0; \$output = ''; // Turn up each card in turn foreach ( \$cards as \$card ) { \$output .= card_to_HTML( \$card ); \$turned_up_cards[] = \$card; // Determine who, if anyone, wins a trick if ( is_colour_suffix( \$u_colour_seq, \$turned_up_cards ) ) { \$output .= 'User wins trick'; \$u_num_tricks++; \$turned_up_cards = array(); } elseif ( is_colour_suffix( \$c_colour_seq, \$turned_up_cards ) ) { \$output .= 'Computer wins trick'; \$c_num_tricks++; \$turned_up_cards = array(); } } // Output game play and outcome output_game( \$u_colour_seq, \$c_colour_seq, \$output, \$u_num_tricks, \$c_num_tricks ); ?>