|
|
|
|
![]() ![]() |
Feb 3 2007, 10:55 AM
Post
#1
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 70 Joined: 5-August 06 From: Rome (born only) Member No.: 27,852 |
I'm having trouble with an assignment for my data structures class.
I have to simulate a card player getting a a pile of 52 cards (a full deck). I need to print the cards so that they are printed on 4 lines (1 line for each suit) 13 cards to a line. (I.E. AC (Ace of clubs) KC QC etc.) The first two files where provided in class: Code: CODE /** @file Card.h * */ class Card { private: char rank; char suit; public: void printMe(); void setRank(char); void setSuit(char); char getRank(); char getSuit(); int getBridgeValue(); }; Code: CODE /** @file Card.cpp * */ #include "Card.h" #include <iostream> using namespace std; void Card::printMe() { cout << getRank() << getSuit(); } void Card::setRank(char whatrank) { rank = whatrank; } void Card::setSuit(char whatsuit) { suit = whatsuit; } char Card::getRank() { return rank; } char Card::getSuit() { return suit; } We must code ourselves at least a Pile.h, Pile.cpp Hand.h, Hand.cpp, pr1.cpp, and build.sh I have attempted to code the some of the Pile.h and Pile.cpp ising the required Array-Based Implentation of the ADT List. Here is my Pile.h file Code: CODE /** @ Pile.h * */ //#include "Card.h" const int MAX_LIST = 52; typedef Card ListItemtype; // List item data type //ListItemType items[MAX_LIST]; //Array of list items */ Card items[MAX_LIST]; //array of cards int size; // LOGICAL length of the list class Pile { private: Card items[MAX_LIST]; public: Pile(); // void createDeck(); /* void orderCards(); void dealCard(); void randomize(); void printMe(); */ }; I keep getting syntax errors on the typedef statement and the "Card items[MAX_LIST]; " Please help? P.S. If I seem inept I haven't coded in C++ in a year so while I know all the very basic things I've forgotten a lot too. |
|
|
|
Feb 7 2007, 01:45 AM
Post
#2
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 396 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
|
|
|
|
Jul 31 2007, 06:18 PM
Post
#3
|
|
|
Newbie [Level 3] ![]() ![]() ![]() Group: Members Posts: 47 Joined: 22-July 07 From: Dhaka, Bangladesh Member No.: 46,859 |
Why you declare "Card items[MAX_LIST]" as an Array type. You can use it as pointer type. What i know about programming is Declare any data in Globally is back dated idea. But any way Try to create a pointer type array of Card object. The code may be like this.
Card *items = new Card[MAX_LIST]; |
|
|
|
Sep 19 2007, 04:26 AM
Post
#4
|
|
|
Newbie ![]() Group: Members Posts: 7 Joined: 18-September 07 Member No.: 50,196 |
Using pointers is always a good thing, but to make sure ur pointers work well don't forget to allocate memory to them as most of the people don't do so and in some systems it creates a problem.
|
|
|
|
Jan 28 2008, 06:07 PM
Post
#5
|
|
|
Newbie ![]() Group: Members Posts: 3 Joined: 25-January 08 Member No.: 56,859 |
I did this for a Texas Hold'em game once... I found it pretty simple to represent the cards using a single integer. (You're only talking about 52 cards.. Come on..). So, for 4 suits, you need two bits. Then, value the cards from 1 to 14 using 4 bits. Now, you are only using 6 bits, even in byte you have left over data that you COULD use to hold simple flags like "Face Card" etc. Store your numbers in your array, and get the values like this:
#define SUIT_SPADES 0x0 #define SUIT_CLUBS 0x1 #define SUIT_HEARTS 0x2 #define SUIT_DIAMOND 0x3 Then, as each byte represents a card, you could even define the mask values as such. #define SUIT_MASK 0x30 #define CARD_MASK 0xF Then, just AND these with your card values. So, the Ace Of Spades would equal 1, the ace of clubs would equal 17, the ace of hearts would equal 33 and the Ace of Diamonds would be 49. So, what would the Queen of Hearts be? 44... (Starting at bit 6, the value is 2 for Hearts, and 12 for the Queen. (A,2,3,4,5,6,7,8,9,10,J,Q,K). Larry I'm having trouble with an assignment for my data structures class. I have to simulate a card player getting a a pile of 52 cards (a full deck). I need to print the cards so that they are printed on 4 lines (1 line for each suit) 13 cards to a line. (I.E. AC (Ace of clubs) KC QC etc.) The first two files where provided in class: Code: CODE /** @file Card.h * */ class Card { private: char rank; char suit; public: void printMe(); void setRank(char); void setSuit(char); char getRank(); char getSuit(); int getBridgeValue(); }; Code: CODE /** @file Card.cpp * */ #include "Card.h" #include <iostream> using namespace std; void Card::printMe() { cout << getRank() << getSuit(); } void Card::setRank(char whatrank) { rank = whatrank; } void Card::setSuit(char whatsuit) { suit = whatsuit; } char Card::getRank() { return rank; } char Card::getSuit() { return suit; } We must code ourselves at least a Pile.h, Pile.cpp Hand.h, Hand.cpp, pr1.cpp, and build.sh I have attempted to code the some of the Pile.h and Pile.cpp ising the required Array-Based Implentation of the ADT List. Here is my Pile.h file Code: CODE /** @ Pile.h * */ //#include "Card.h" const int MAX_LIST = 52; typedef Card ListItemtype; // List item data type //ListItemType items[MAX_LIST]; //Array of list items */ Card items[MAX_LIST]; //array of cards int size; // LOGICAL length of the list class Pile { private: Card items[MAX_LIST]; public: Pile(); // void createDeck(); /* void orderCards(); void dealCard(); void randomize(); void printMe(); */ }; I keep getting syntax errors on the typedef statement and the "Card items[MAX_LIST]; " Please help? P.S. If I seem inept I haven't coded in C++ in a year so while I know all the very basic things I've forgotten a lot too. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 25th July 2008 - 07:43 PM |