I'm creating a program to Deal, Sort, Display and determine cards for a poker game.

A word for word of my program is here...

Write a program that will operate on a deck of 52 cards. Your program will:
1. Deal them into four random hands of 5 cards each
a. The four hands need to be kept in an array for later use.
2. Sort each hand so that it shows the cards in sequence from two as the lowest to ace as the highest.
3. Display the cards in each hand using the card face (2, 10, King, etc.) and the suit (Spades, Hearts, etc.)
4. Display what you determined the hand to be.
5. Each time the program is run, a different sets of hands is to be dealt.

I think I understand number four, but I don't understand how to deal/shuffle the deck. Nor do I understand structs.

Here's a skeleton of what I have so far.

#include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
// & means location
// * value of location

struct Cards {

	enum Value	{Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
	Value V;
	int Suite;	
	enum Suite {Hearts, Diamonds, Clubs, Spades};
};

struct Hand {
	Cards [5];

};

struct Deck {
	Cards [52];

};

char *	personsName [20]; //this means location with *
int		numNames = 0;
char *	Name;
char *	search;
bool	Continue;

int main ()
{
	do	{
		for(int x = 0; x < 20; x++){
			cout << "Enter a name, type END to stop: ";
			Name = readString ();

			if(strcmp (Name, "END") != 0){
				personsName [x] = Name;
				numNames++;
			}
			else{
				Continue = (strcmp (Name, "END") != 0);
				break;
			}

		}
		cout << "Pre-sorted Names" << endl;
		printNames(personsName,numNames);

		bubbleSort(personsName,numNames);

		cout << "Post-sorted Names" << endl;
		printNames(personsName,numNames);

		cout << "What would you like to find?" << endl;
		search = readString();
		cout << "The name is at index " << binarySearch(personsName,search,numNames) << endl;

	} while (Continue);	
	delete [] * personsName;
	return 0;
}

Now as you can see, this is an old program that I'm recycling for use.

On a side note, how can I throw all my methods into another file and call them from there. Should I make a new .cpp file, put my search function in that and how would I declare that? As in, where would I declare the .h file, in main or in the .cpp - and where do I declare the .cpp?

Thank you for any advice or just tips on how to do this.
My problem is that I don't know how to even begin to start this/use with structs.

I have a sorting method, and a search method already set up.

Here is an example of how to use a struct:

#include <iostream>

struct Card
{
  int id;
};

int main (int argc, char *argv[])
{
	Card c;
    c.id = 3;

	return 0;
}

It is exactly like a class, but the default member level is public.

I think here your struct should be called Card instead of Cards - then when you have an array of a Card object, that is a bunch of "cards".

Dave

Creating a user defined type to represent a Card, a Deck and a Hand makes sense. You can either declare and define each user defined class within the cpp file containing main(), before main(), or, each user defined type could be declared in it's own header file and the methods of each user defined type could be defined in a corresponding cpp file by the same name. In the latter style the header file for each user defined class can be included in the cpp file containg the main() function for your program.

Are you allowed to use algorithm's from the Standard Template Library like random_shuffle() and sort() or are you supposed to create your own algorithms to do that? How you set up your user defined types might differ a little if you can use the predefined algorithms as opposed to if you have to write them yourself.

According to #3 in your instructions each Card needs to have two identifiers, a face and a suit. It would be helpful if each Card is able to display itself. Being able to assign one Card's values to another would be helpful, too.

When you can display a Card, then I'd work on a Deck class. The Deck constructor should create each of the Cards in a Deck. The Deck should be able to be shuffled. If you want to be able to display the Deck so you can be confident it is shuffled, fine.

Each Hand has 5 Cards and a description. Per your instructions each Hand should be able to describe itself in terms of both the Cards in it and the description. The description of the Hand can only be deternmined after each Hand has been dealt. Per your instructions, the Cards in the Hand should be sorted by the face member value.

To deal you need a Deck and 4 Hands. One Card is dealt to each Hand each round, meaning there are five rounds per deal. That means, for each round, each hand is assigned the next Card in the Deck, starting from the first Card in the Deck.

A rough protocol for the program could look something like:

main()
declare a Deck
declare 4 Hands
start a loop
shuffle Deck
deal 4 Hands
sort the Cards in each Hand
determine description of each Hand
display each Card in each Hand
display description of each Hand
repeat loop as desired
end main()

I'm sorry Lerner, I think you're talking about Object Oriented design which is a bit over our heads in this class. We're using procedural(?) or something methods since it's a beginning/Intro C++ class. What you're stating is probably what I'll learn next semester? As for using things in the standard library, I don't think that would be a problem/it doesn't specify.

I've used srand() and such before so I think we'll be ok if we used other pre-made methods?

>>We're using procedural(?) or something methods since it's a beginning/Intro C++ class.

Not a problem. Take the functionality/methods/functions of the classes/struct/user defined type, whatever you want to call it, out of the user defined types I described and make them free standing procedures/actions/protocols/algorithms. You can probably get by with just the Card struct under those circumstances and declaring the Card struct within the progam cpp should be adequate. Here's one version of how it could be started. There are others, too.

#include <iostream>
#include <string>
using namespace std;

const string faceNames[] = //{"2", "3", "4", "5", etc, "King", "Ace"};
const string suitNames[] = { "Clubs", "Diamonds", "Hearts", "Spades"}
struct Card
{
    int face; //2-13
    int suit; //1-4
};

void displayCard(const Card c)
{
   cout << faceNames[c.face - 2] << " of " << suitNames[c.suit - 1];
}

int main()
{
   Card deck[52];
   Card hands[4][5]; //4 hands, 5 cards each

    //assign meaningful values to Cards in the deck
    int index = 0;
    for(int s = 1; s < 5; ++s) //1-4
    {
       for(int f = 2; f < 14; ++f) //2-13
      {
         deck[index].face  =  f;
         deck[index].suit = s;
         ++index;
       }
    }

    //display deck
    for(int i = 0; i < 52; ++i)
    {
        displayCard(deck[i]);
        cout << endl;
     }
   
     bool more = true;
     while (more)
     {
         //shuffle deck
         //deal hands
         //sort hands
         //determine description of hands
         //display hands as needed
         //determine whether to go again or stop
    }
    return 0;
}

WARNING: the above code has not been compiled/tested, so minor errors could exist, though I am confident of the underlying protocols.

You can modify the above to take C style strings if you aren't allowed to use STL string objects. Otherwise everything else is in "procedural" rather than Object Oriented mode.

Next, I would come up with a shuffle algorithm, followed by an algorithm to deal the hands, then sort the hands, determine the description of the hands, and last, but not least, display the information about the hands as required in the instructions.

If you have a problem coming up with the various protocols post the relevant information including pertinent code, error messages, problems coming up with algorithms, etc.

Have fun!

Thanks for your reply! I'm off to work soon, but later tonight I'll use the information you gave me and try this all out!

I'm trying to link my Main to my "Logic.cpp" and "Logic.h" files which contain sorting and search methods - how would I do this?

This part of code is causing my program to blow up in my face, I don't understand why. Unhandled exception at 0x6993191c (msvcp90d.dll) in beckerPhil_Lab13.exe: 0xC0000005: Access violation reading location 0x9a966b28.

I've never had that before.

cout << faceNames[c.f - 2] << " of " << suitNames[c.s - 1];

Here's my full program

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

#include "Logic.h"

using namespace std;


const string faceNames[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
const string suitNames[] = { "Clubs", "Diamonds", "Hearts", "Spades"};

struct Card

{
	int f; //2-13
	int s; //1-4
};

void CardSort(int * C [], int arraySize);
void displayCard(const Card c);
void shuffle(Card D[]);

int main()
{
	Card deck[52];
	Card hands[4][5]; //4 hands, 5 cards each
	//assign meaningful values to Cards in the deck
	srand(time(0));  // initialize seed "randomly"

	//display deck
	for(int i = 0; i < 52; ++i)
	{
		displayCard(deck[i]);
		cout << endl;
	}

	bool more = true;
	while (more)
	{
		//shuffle deck*

		//deal hands
		
		//sort
		//bubbleSort(hands,5);

		//determine description of hands
		//display hands as needed
		//determine whether to go again or stop
	}

	return 0;

}

void displayCard(const Card c)

{
	cout << faceNames[c.f - 2] << " of " << suitNames[c.s - 1];
}

void shuffle(Card D []){
	for (int i=0; i<52; i++) {
		for (int fv = 2; fv<14;fv++){
			D[i].f = D[fv].f;  // fill the array in order
				for(int sv = 1; sv<5; sv++)
					D[i].s = D[sv].s;
		}
    }
      
        //--- Shuffle elements by randomly exchanging each with one other.
        for (int i=0; i<(52-1); i++) {
            int r = i + (rand() % (52-i)); // Random remaining position.
            Card temp = D[i]; 
			D[i] = D[r]; 
			D[r] = temp;
		}
}

void CardSort(Card H [], int arraySize){
	int		NumChars = arraySize;
	int		i;
	bool	Sorted;
	Card	Temp;
	int		NumberOfCompares;

	NumberOfCompares = NumChars - 1;
	do	{	//	outer loop - keeps doing the inner loop until nothing changes
		Sorted = true;
		for (i = 0; i < NumberOfCompares; i++)	//	inner loop	- compares each pair of values
			if (H[i].f > (H[i + 1].f)) //char * strcpy ( char * destination, const char * source );
			{
				Temp = H [i];
				H [i] = H [i + 1];
				H [i + 1] = Temp;
				Sorted = false;
			}
			else;
			NumberOfCompares--;
	} while (!Sorted);
}

I don't really expect the other stuff to work; but it does compile, when running however it crashes.

By putting an output statement like this:

void displayCard(const Card c)

{
  cout << "c.f -2 : " << c.f -2 << " , " << " c.s - 1 : " << c.s -1 << endl;
	cout << faceNames[c.f - 2] << " of " << suitNames[c.s - 1];
}

You can see that your indices are out of range:

c.f -2 : 122 ,  c.s - 1 : 2
*** Process aborted. Segmentation fault ***

Also, you should not use these 1 letter variable names ("f" and "s"). It makes the code very hard to read.

Dave

When you declare deck the Cards in deck aren't given for meaningful values for face or suit. The default constructor for the Card type could give them a default value like zero's if you want. Otherwise be sure to give the Cards in deck meaningful values for face and suit, like I showed previously, before you try to use the values in a output statement.

I strongly encourage you to learn learn how to debug your code. davidora demonstrated one approach, using output statements to evaluate variables as they are being changed/tested/displayed. Another approach is to use a debugging software to moniitor variable values as the program progresses. Both approaches are very successful. There is a third approach, but I discourage it so I won't mention it by name.

void shuffle(Card D []){
	for (int i=0; i<52; i++) {
		for (int fv = 2; fv<14;fv++){
			D[i].f = D[fv].f;  // fill the array in order
				for(int sv = 1; sv<5; sv++)
					D[i].s = D[sv].s;
		}
    }

If you look at that code there, I just failed to initialize it correctly. I was giving things values to values that don't exist. How dumb of me. Thanks for ya'lls help I should be able to get this finished by tonight.

Alright, so I'm having a problem with my "DisplayHand" method, I'm using a 2D array to store cards as "hands", basically column 1-4 are the players, and the five spots should be cards; right?

Soo...
Player 1; Card1, Card2, Card3, Card4, Card 5;
Player 2; Card1, Card2, Card3, Card4, Card5;
Player 3...
Player 4...

I'm totally confused on how to print these at the moment. I can't use the printCard method because that's only for 1d arrays right?

I also have no idea how to deal the deck, I'm probably going to use a a counter to tell me what card what I've dealt though. and when it's over, i'll have a redo and we'll recall shuffle/deal/and reset the counter.

So, how would I do this? This is what I have so far.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

#include "Logic.h"

using namespace std;


const string faceNames[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
const string suitNames[] = { "Hearts", "Diamonds", "Clubs", "Spades"};

struct Card

{
	int face; //2-13
	int suit; //0-3; ASCII CODE 3 - 7
};

void CardSort(Card H [], int arraySize);
void displayCard(Card c);
void shuffle(Card * D);
void displayDeck(Card Deck []);
void displayHand(Card Hand,int whichHand);

int main()
{
	Card deck[52];
	Card hands[3][4]; //4 hands, 5 cards each
	//assign meaningful values to Cards in the deck
	srand(time(0));  // initialize seed "randomly"

	shuffle(deck);

	displayDeck(deck);


	displayDeck(deck);

	bool more = true;
	while (more)
	{
		//shuffle deck*

		//deal hands
		
		//sort*
		//bubbleSort(hands,5);

		//determine description of hands
		//display hands as needed
		//determine whether to go again or stop
	}

	return 0;

}

void displayDeck(Card Deck []){
	for(int i = 0; i < 52; i++){
		displayCard(Deck[i]);
	}
}

void displayHand(Card aHand [] [5],int whichHand){
	int handNum = whichHand;
	for(int i = 0; i < 5; i++){
		cout << faceNames[aHand.face - 2] << " of " << suitNames[aHand.suit] << endl;
	}
}

void dealDeck(Hand player1){
	
}

void shuffle(Card * D){
	int i = 0;
		for (int fv = 2; fv<15;fv++){
			for(int sv = 0; sv<4; sv++){
					D[i].face = fv;  // fill the array in order
					D[i].suit = sv;
					i++;
					cout << i << endl;
		}
    }
      
        //--- Shuffle elements by randomly exchanging each with one other.
        for (int i=0; i<(52-1); i++) {
            int r = i + (rand() % (52-i)); // Random remaining position.
            Card temp = D[i]; 
			D[i] = D[r]; 
			D[r] = temp;
		}
		
}

void displayCard(Card c)

{
	cout << faceNames[c.face - 2] << " of " << suitNames[c.suit] << endl;
}



void CardSort(Card H [], int arraySize){
	int		sizeOfArray = arraySize;
	int		i;
	bool	Sorted;
	Card	Temp;
	int		NumberOfCompares;

	NumberOfCompares = sizeOfArray - 1;
	do	{	//	outer loop - keeps doing the inner loop until nothing changes
		Sorted = true;
		for (i = 0; i < NumberOfCompares; i++)	//	inner loop	- compares each pair of values
			if (H[i].face > (H[i + 1].face)) //char * strcpy ( char * destination, const char * source );
			{
				Temp = H [i];
				H [i] = H [i + 1];
				H [i + 1] = Temp;
				Sorted = false;
			}
			else;
			NumberOfCompares--;
	} while (!Sorted);
}

Take the code to initialize the cards in the deck out of shuffle() and place it before you start the loop, thus giving the Card objects values only once in the program rather than every time you call shuffle(). The basic shuffle protocol----repeatedly get a random number (or two) to access the indexes of deck and swap two cards in deck based on the random index(es) seems good.

The following calls to your displayCard() function will display the values of a Card using three different syntaxes to refer to a Card being passed to the function, assuming of course that i, j and the member variables of the Card have been initialized :
int i, j;
Card card;
Card deck[52];
Card hands[4][5];
displayCard(card);
displayCard(deck);
displayCard(hands[j]);

To deal you can do basically what you would do if you were dealing 5 cards to 4 four players, passing each player one card during each round of dealing.

int deckIndex = 0; //start with first Card in the deck, the one with index of 0
for 5 Cards //controls which Card in the hand; ranges 0-4
  for each player //controls which hand in hands; ranges 0-3
     assign Card in deck with current deckIndex to current Card in current hand
     go to next Card in deck by increasing deckIndex by one
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.