codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  new here, can anyone help me with a problem involving pointer?  burnout at 17:43 on Tuesday, May 27, 2008
 

hi, i'm kind of new to c++ but i'm stuck on a problem involving pointers and i am not really good with programming. If anyone can help it would be greatly appreciated.


This assignment 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").
3. Compile/build the application code "playCard.cpp" (completely filled).
4. Run the program (where you are prompted to enter one number), and check the correctness of your methods.

IMPORTANT NOTE:

* In either class (Card or Deck), DO NOT add any member variables. Adding more member functions (as support functions) are OK.

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

// Global constants
const int NUMSUITS = 4;
const int NUMNUMS = 13;
const char suits[NUMSUITS] = {'C', 'D', 'H', 'S'};
const char nums[NUMNUMS] = {'2', '3', '4', '5', '6', '7', '8', '9',
'T', 'J', 'Q', 'K', 'A'};
// Class Card
class Card
{
public:
// member functions
Card(char s = 'C', char n = '2');
bool operator==(const Card& c2) const; // WRITE THIS METHOD (1)
bool operator<(const Card& c2) const; // WRITE THIS METHOD (2)
void printCard() const;

private:
char suit; // 'C' or 'D' or 'H' or 'S'
char num; // '2' thru '9', 'T', 'J', 'Q', 'K' or 'A'
};

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

For instance, C2 weaker than C8 weaker than CA weaker than D2.

Note: There are many ways to do this comparison. Any will do.
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. For example,

char ch1, ch2;
// Assume char values are assigned to ch1 and ch2.
if (ch1 < ch2) // compiler at their ASCII values and compare them
...

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.

class Deck
{
public:
Deck();
void Shuffle(); // WRITE THIS METHOD (3)
int compCards(); // WRITE THIS METHOD (4)
void printDeck(); // WRITE THIS METHOD (5)

private:
static const int NUMCARDS // an int constant (52 here)
= NUMSUITS * NUMNUMS;
Card deck[NUMCARDS]; // deck of 52 Card's
};

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

Don't worry if the two indices end up the same. The swapping procedure (below) should not break because of that.
o To swap two cards, look at the example shown in the class "marApp.cpp" from week 6.
* 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.

Sample output:

**** Fresh Deck ****
C2 C3 C4 C5 C6 C7 C8 C9 CT CJ CQ CK CA
D2 D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK DA
H2 H3 H4 H5 H6 H7 H8 H9 HT HJ HQ HK HA
S2 S3 S4 S5 S6 S7 S8 S9 ST SJ SQ SK SA


**** After shuffling ****
H8 C3 D9 D3 HA C9 S9 S4 H3 CK H4 H5 CA
SJ C5 D4 D5 D6 HJ H9 DK ST DJ C4 C8 S6
HQ C2 S2 H7 C6 C7 S7 DT D2 HK CQ D8 H6
S5 CT SQ DA SK S3 D7 S8 DQ CJ H2 HT SA


??? How many times do you want to play? 20

player1: CA, player2: H6, WINNER is player2
player1: D4, player2: D2, WINNER is player1
player1: CA, player2: S8, WINNER is player2
player1: S3, player2: CA, WINNER is player1
player1: CQ, player2: CQ, DRAW!
player1: S5, player2: H2, WINNER is player1
player1: H4, player2: SK, WINNER is player2
player1: H3, player2: H7, WINNER is player2
player1: DQ, player2: H3, WINNER is player2
player1: C3, player2: DA, WINNER is player2
player1: HT, player2: S5, WINNER is player2
player1: H5, player2: HK, WINNER is player2
player1: SK, player2: SJ, WINNER is player1
player1: SA, player2: C9, WINNER is player1
player1: HT, player2: HA, WINNER is player2
player1: D4, player2: C2, WINNER is player1
player1: H7, player2: HA, WINNER is player2
player1: DK, player2: CQ, WINNER is player1
player1: S4, player2: D8, WINNER is player1
player1: H8, player2: D2, WINNER is player1


====== FINAL STATISTICS ======
player1: 45.0 %
player2: 50.0 %
draw: 5.0 %










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: different image displayed based on screen resolution
•  new here, can anyone help me with a problem involving pointer?
•  Ask for suggestions on blog template :)
•  Re: Get the image name
•  A new simple way to make a image slider- Javascript Code
•  Re: convert minutes into hours and minutes
•  how to create forum on my own in java is it possible
•  Re: mkdirs
•  Help with recursion { recursive helper function }


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008