May 13, 2008

Array Problem

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Array Problem

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

 

 

 


Reply

osknockout
smile.gif You haven't defined any ListItemtype. - That is to say 'ListItemtype' doesn't exist as a type, so you can't specify 'Card' as type 'ListItemtype'. Just specify it, you probably just forgot to encode it. Nice operator overloading btw.

Reply

rajibbd
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];

Reply

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

Reply

larryf
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

QUOTE(alexviii @ Feb 3 2007, 02:55 AM) *
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.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:


Searching Video's for array, problem
advertisement



Array Problem



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE