By the time you have completed this lab, you should be able to
.
) and
the pointer-to-structure operator (->
)This part is worth 10 points (out of 50 total Lab08 points). It is important to us, and you can only earn the points by attending your lab session on Wednesday, March 7, and only if you arrive in time to complete it. The forms will be distributed no later than 5 minutes past the hour that begins your lab session. Don't be late! |
This lab's first pilot should log in, create ~/cs16/lab08/ and make that your current directory.
struct
at the ch promptStart ch. Then define the following structure to represent a playing card:
ch> struct Card { char suit; int face; };
When not using ch, we usually define structs over several lines like this:
struct Card { char suit; int face; }; // btw, forgetting the semicolon at the end is a common error
An object of this type will hold a char variable named suit to represent the card's suit (assumed to be 'C' for Clubs, 'D' for Diamonds, 'H' for Hearts and 'S' for Spades), and an int variable named face to represent its face value (2-10 for two through ten, 11 for jack, 12 for queen, 13 for king and 14 for ace).
Create a Card object named c, and set its fields to represent the ace of Spades. Then type the object's name by itself to see how ch displays structs:
ch> Card c; ch> c.suit = 'S'; S ch> c.face = 14; 14 ch> c .suit = S .face = 14
Create a pointer to a Card object named cp, set it to the address of c:
ch> Card *cp = &c;
Now cp can be used as another way to access the fields of c. To do so you must
first dereference the pointer, *cp, and then apply the dot operator to access the
field. Since the dot operator "binds more tightly" (has higher precedence) than
the dereference operator, the first part has to be inside parentheses, and because
that syntax is so awkward, the language authors provided '->
' as a shortcut
pointer operator. Practice both at the ch prompt: use the awkward syntax to display
c's suit, and use the pointer operator to display c's face:
ch> (*cp).suit S ch> cp->face 14
Create another struct card object named c2, but also use an "initializer list" to set this card's fields to represent the queen of Hearts:
ch> Card c2 = {'H', 12}; ch> c2 .suit = H .face = 12
Notice that you cannot use relational operators to compare entire structures like Card objects. Instead you must compare individual fields:
ch> c2 < c ERROR: invalid operands for less than operator < ch> c2.face < c.face 1
It is appropriate to use relational operators for comparing pointers to structures though: both == and != any time, and the other operators when the pointers both refer to the same array. (a) Create another Card pointer named cp2, and assign the address of c2 to it. (b) Then use == to compare cp to cp2. (c) Aim cp2 at c. (d) Compare cp to cp2 again:
ch> Card *cp2 = &c2; ch> cp2 == cp 0 ch> cp2 = &c; 0x92db4f0 ch> cp2 == cp 1
Understand these results before proceeding to Step 3.
First: switch roles between pilot and navigator if you did not already do that.
Exit ch.
Study the completed parts of this cards.cpp skeleton.
Download a copy of the skeleton program to your lab08 directory.
After all of printCard, printCards and cmpCards are completed, your results should match the following:
-bash-4.1$ make cards g++ cards.cpp -o cards -bash-4.1$ ./cards printing third card: seven of Diamonds printing first two cards: ace of Spades queen of Hearts printing all cards, unsorted: ace of Spades queen of Hearts seven of Diamonds seven of Clubs jack of Spades printing all cards, sorted: seven of Clubs seven of Diamonds jack of Spades queen of Hearts ace of Spades
Open cards.cpp in an editor, and make the following changes. But don't do them all at once - instead test each part before continuing to the next part. You can even compile/run the original version - it just doesn't do anything:
-bash-4.1$ ./cards printing third card: printing first two cards: printing all cards, unsorted: printing all cards, sorted:
Remember to save the file and test after completing each function. Don't wait to test them all at once.
You can use the following command from the CSIL prompt:
~submit/submit -p 966 cards.cppOr you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser - submit cards.cpp for Lab08. A perfect score is 40/40 points. All parts must be work exactly right to earn any points from the submit.cs test.
If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.
Each student must accomplish the following to earn full credit [50 total points] for this lab:
The deadline for submitting your work for credit is tonight by 11:59 pm.
If you finish before the end of your lab period, consider helping other students who might be struggling. You may also work on PA6 or the optional challenges below.
Optional Extra Challenge
- Instead of an array with just five cards, why not make one that holds all 52 cards of a standard deck? You can declare
Card deck[52];
and then fill up the deck with four loops (for the four suits) that creates Card objects with face values ranging from 2 through 14 for each suit.- Randomly shuffle a deck of 52 Cards. An algorithm to do so would start by selecting a random integer in the range 0-51, and then swap the Card in the randomly selected position with the Card in the last position (deck[51]). In the next iteration, select a random integer in the range 0-50, and swap the Card in that position with the one in the second-to-last place (deck[50]), and so on. You can set up card games by selecting "hands" from this shuffled deck.
- Poker hands typically consist of five cards each. Write a series of functions that return true if a hand meets certain conditions. For example, a function such as
bool hasPair(Card hand[5])
would return true if the hand has at least two cards with the same face value. Another function might bebool isFlush(Card hand[5])
that would return true if all five cards share the same suit. Write and test as many such functions as you have time to do.- If you wrote a complete set of functions to test poker hands, and did the other challenges too, then what are you waiting for? Start writing that poker game you've been thinking about! After carefully planning it of course.
Prepared by Michael Costanzo.