0

Deck.h

#include "stdafx.h"
#ifndef DECK_H
#define DECK_H

class Deck
{
public:
	struct card{int symbol; int value;};
	int symbol;
	int value;
	card card_list[52];
	card deck[52];
	card community[5];
	card player1[2];
	card player2[2];
	card player1_hand[7];
	card player2_hand[7];
	void generate_deck();
	void shuffle_deck();
	void deal_cards();
	int main();
private:
	static int const diamond = 1;
	static int const club = 2;
	static int const heart = 3;
	static int const spade = 4;
};

#endif

Deck.cpp

//usual intro stuff
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <time.h>

#include "Deck.h"

using namespace std;

struct card{int symbol; int value;};

void Deck::generate_deck() //Initialize the deck from 1st card to the 52nd card
{
	for(int array_location=0;array_location<=12;array_location++)//all diamond cards
	{
		card_list[array_location].symbol = diamond;
		card_list[array_location].value = array_location+1;
	}

	for(int array_location=13;array_location<=25;array_location++)//all club cards
	{
		card_list[array_location].symbol = club;
		card_list[array_location].value = array_location-12;
	}

	for(int array_location=26;array_location<=38;array_location++)//all heart cards
	{
		card_list[array_location].symbol = heart;
		card_list[array_location].value = array_location-25;
	}

	for(int array_location=39;array_location<=51;array_location++)//all spade cards
	{
		card_list[array_location].symbol = spade;
		card_list[array_location].value = array_location-38;
	}
}

void Deck::shuffle_deck() //randomly shuffle the deck
{
	for(int array_location=0;array_location<=51;array_location++)
	{
		deck[array_location].symbol= 0;
		deck[array_location].value= 0;
	}
	
	for(int array_location=0;array_location<=51;array_location++)
	{
		int tempSymbol = 0;
		int tempValue = 0;
		int random = rand() % 52;
		
		while(card_list[random].value == 0 && card_list[random].symbol == 0 && array_location<=51)
		{
			random = rand() % 52;
		};
		
		tempSymbol = card_list[random].symbol;
		card_list[random].symbol = 0;
		deck[array_location].symbol = tempSymbol;
	
		tempValue = card_list[random].value;
		card_list[random].value = 0;
		deck[array_location].value = tempValue;
	}
}
	
void Deck::deal_cards() //Deal all the cards, set player1's card + community cards = player1's hand(same for player 2). Sort the order from the cards from smallest to largest
{
	int tempSymbol = 0; 
	int tempValue = 0;
	
	tempSymbol = deck[0].symbol;
	player1[0].symbol = tempSymbol;
	tempValue = deck[0].value;
	player1[0].value = tempValue;

	tempSymbol = deck[1].symbol;
	player2[0].symbol = tempSymbol;
	tempValue = deck[1].value;
	player2[0].value = tempValue;

	tempSymbol = deck[2].symbol;
	player1[1].symbol = tempSymbol;
	tempValue = deck[2].value;
	player1[1].value = tempValue;

	tempSymbol = deck[3].symbol;
	player2[1].symbol = tempSymbol;
	tempValue = deck[3].value;
	player2[1].value = tempValue;


	for(int array_locationA = 0; array_locationA <=5; array_locationA++)
	{
		int tempSymbol = 0; 
		int tempValue = 0;
		int array_locationB = array_locationA + 4;

		tempSymbol = deck[array_locationB].symbol;
		community[array_locationA].symbol = tempSymbol;
		tempValue = deck[array_locationB].value;
		community[array_locationA].value = tempValue;
	}

	for(int array_locationA = 0; array_locationA<5; array_locationA++)
	{
		int tempSymbol = 0; 
		int tempValue = 0;

		tempSymbol = community[array_locationA].symbol;
		player1_hand[array_locationA].symbol = tempSymbol;
		player2_hand[array_locationA].symbol = tempSymbol;
		tempValue = community[array_locationA].value;
		player1_hand[array_locationA].value = tempValue;
		player2_hand[array_locationA].value = tempValue;
	}

	for(int array_locationA = 0; array_locationA<2; array_locationA++)
	{
		int tempSymbol = 0; 
		int tempValue = 0;
		int array_locationB = array_locationA + 5;

		tempSymbol = player1[array_locationA].symbol;
		player1_hand[array_locationB].symbol = tempSymbol;
		tempValue = player1[array_locationA].value;
		player1_hand[array_locationB].value = tempValue;

		tempSymbol = player2[array_locationA].symbol;
		player2_hand[array_locationB].symbol = tempSymbol;
		tempValue = player2[array_locationA].value;
		player2_hand[array_locationB].value = tempValue;
	}	

	for(int array_location = 0; array_location<7; array_location++)
	{
		cout<<"\n"
		<<"Player 1 : "
		<<player1_hand[array_location].symbol
		<<"\t"
		<<player1_hand[array_location].value
		<<"\n"
		<<"Player 2 : "
		<<player2_hand[array_location].symbol
		<<"\t"
		<<player2_hand[array_location].value
		<<"\n";
	}	

	for(int loop = 0; loop<7; loop++)
	{
		bool swapped = false;
		if(player1_hand[loop].value > player1_hand[loop+1].value)
		{
			int temp = player1_hand[loop].value;
			player1_hand[loop].value = player1_hand[loop+1].value;
			player1_hand[loop+1].value = temp;
			swapped = true;
		}
	}

	for(int loop = 0; loop<7; loop++)
	{
		bool swapped = false;
		if(player2_hand[loop].value > player2_hand[loop+1].value)
		{
			int temp = player2_hand[loop].value;
			player2_hand[loop].value = player2_hand[loop+1].value;
			player2_hand[loop+1].value = temp;
			swapped = true;
		}
	}

	cout<<"\n\n\n After sorting"<<endl;

	for(int array_location = 0; array_location<7; array_location++)
	{
		cout<<"\n"
		<<"Player 1 : "
		<<player1_hand[array_location].symbol
		<<"\t"
		<<player1_hand[array_location].value
		<<"\n"
		<<"Player 2 : "
		<<player2_hand[array_location].symbol
		<<"\t"
		<<player2_hand[array_location].value
		<<"\n";
	}	
}

int Deck::main()
{
	cout<<card_list[2].value<<endl;
	cout<<"what the hell"<<endl;
	cout<<card_list[6].value<<endl;
	generate_deck();
	shuffle_deck();
	deal_cards();

	return 0;
}

The program compiles but crashes when I try running it.

However, if I performed everything without the header(with modifications to the class), it'll compile and run properly. Any ideas?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Clinton Portis
0

just my two cents..

I'm not sure if you can derive your main() function from Deck.. (i've never done it this way) this would be new to me:

int Deck::main()

I would segregate your program into the standard three sections: .h header file, .cpp main driver, and .cpp function definitions (making sure the .h header has the same file name as it's .cpp function definitions)

Then, in your int main() driver .cpp, be sure to #include<Deck.h>

(Deck.h will automaticallly be associated with Deck.cpp because they have the same file name, so no need to #include anything)

Edited by Clinton Portis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.