can anyone provide some input on this pointer problem?
| burnout (11) | |||||||
| Can someone provide some input on this assignment. I am not really any good with pointers. This is a simulation of a card game. It involves two classes: Card (for a single card) and Deck (for a deck of cards -- an array of Card's). Steps to complete the assignment are: 1. Write two methods "operator==" and "operator<" in class Card (defined in "card.h", "card.cpp"). 2. Write three methods "Shuffle", "compCards", "printDeck" in class Deck (defined in "deck.h", "deck.cpp"). 1. Class Card * In "card.h", a Card has two data members: suit and number. The values of those members are exactly what you think they mean (assuming you have played a card game before).
Write/add the full definition of the two method in the implementation file "card.cpp". * bool operator==(const Card& c2) const o This method compares this card and the parameter card (c2), and returns true if the two cards are the same value-wise, that is, the two cards have the same values for both suit and num. Otherwise the method returns false. * bool operator<(const Card& c2) const o This method compares this card and the parameter card (c2), and returns true if this card is STRICTLY weaker than c2, or false otherwise. As a note, if the two cards are the same, the method returns false (of course). o Relative order of strength of the cards is determined by both suit and number. As is standard, + 'C' weaker than 'D' weaker than 'H' weaker than 'S' (i.e., any Club is weaker than any Diamond which in turn is weaker than any Heart, and so on). + Within a suit, '2' weaker than '3' weaker than ... '9' weaker than 'T' weaker than 'J' weaker than 'Q' weaker than 'K' weaker than 'A'. o One way is to compare the ASCII values of the suite or num chars. For example, a char 'C' in ASCII is decimal 67, 'D' is 68, '9' is 57, 'T' is 84 and 'A' is 65. Appendix 3 in the textbook shows the ASCII table. You can use logical operators such as < to compare the ASCII values of chars. o Another way is to utilize the char arrays 'suits' and 'nums' defined (globally) above the class (although this way is less efficient than the above). 2. Class Deck * In "deck.h", a class Deck includes an array of 52 Card objects and some methods.
Notice 'deck' is the data -- an array of Card's. And the length of 'deck' is indicated by 'NUMCARDS'. Do not worry about 'static'. In "deck.cpp", you write the three methods. Note that (4) and (5) could be made to be a const method (actually should), but here the 'const' is dropped to simplify things. * void Shuffle() o In this function, you may use the index ([]) notation, if you wish. o This method shuffles the deck. To do so, you randomly select 2 cards and swap them, and you do this procedure 52 times. o To select a card randomly, you can call the library function "rand" (as suggested in the textbook) in the following way. Note #include <cstdlib> (in which "rand" is defined) is already done at the beginning of the file. int idx = rand() % NUMCARDS; // gives a number between 0 and NUMCARDS-1 inclusive * int compCards() const o In this function, you may ONLY use the pointer (*) notation. You'll receive no credit if you used the index [] notation. o This method compares two randomly selected cards (say rc1 and rc2), and returns 1 if rc1 is (strictly) stronger than rc2, or 2 if rc2 is (strictly) stronger than rc1, or 0 if two cards are the same. This method does the comparison only once. o You can select two cards randomly in the same way as Shuffle(). Again, don't worry if the two indices end up the same -- that means the comparison is a draw. o Utilize the operator== and operator< methods defined in the Card class to determine the relative order/strength of the cards. For instance, Card1 is strictly stronger than card2 if card2 < card1. o After the winner is identified, display to cout the cards of both players and the winner in the form shown below. * void printDeck() const o In this function, you may ONLY use the pointer (*) notation. You'll receive no credit if you used the index [] notation. o This method prints to the terminal (cout) all cards in the deck, with 13 cards in each row. Output of an example run of the program is shown below. o To traverse the deck array, you use a pointer to Card. Then through the pointer, you call the "printCard()" method in each Card object currently pointed by the pointer and print the card. Use the arrow (->) notation to call the method in a Card object.
| |||||||
| Zaita (1562) | |||
| http://www.cplusplus.com/forum/articles/1624/ | |||
| burnout (11) | |||
| thank you for that link, sorry i did not know. | |||
| Zaita (1562) | |||
| You should write your question like: blah blah blah
Some Codeblah blah blah
Some More CodeMy problem is / I need help with X etc.. You have not really told us what you want/need help with? Something with pointers? | |||
| burnout (11) | |||
| ok, thanks. Now hopefully someone will help. | |||
| Zaita (1562) | |||
| What is your issue? What are you stuck on? You haven't asked a question. You've asked for input. And my input is, that the assignment looks just like every other basic C++ assignment. :P But I am sure thats not going to be very useful to you. | |||
| burnout (11) | |||||
| oh, sorry. What i need help is where it says write this method. card.cpp
and in deck.cpp
| |||||
| Zaita (1562) | |||
| Ok. You do realize we don't do peoples homework here right? If you were to start writing these methods yourself, but came across something you were stuck with/couldn't solve. Then you could happily post your code for us to look over and provide advice. But doing other people's homework for them isn't productive. You don't learn how to code, you cheat, you end up being a sub-par programmer who writes crap software when you finally graduate that programmers who did learn how to write code have to fix. http://www.cplusplus.com/forum/articles/1295/ Don't post homework questions Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions. | |||
| Faldrax (322) | |||
| You might want to try looking at http://www.cplusplus.com/doc/tutorial/classes2.html for information on operator overloading (which is what the assignment is trying to test you on). | |||
| Zaita (1562) | |||
| I don't get why people come and ask for an entire solution to their homework. Especially at university level. I would never have thought of doing it (and I only graduated 4yrs ago). I was doing Programming because I wanted to, so It was in my best interests to learn properly. | |||
| Grey Wolf (639) | |||
| I get the impression that some courses have a mandatory programing module, and the person doing the course has no interest in programming but must pass the module to 'keep there grades up'. | |||
This topic is archived - New replies not allowed.
