Blackjack help; struct's, vector's

I'm programming Blackjack, using struct's and hoping to implem

Page 1 of 1

6 Replies - 2017 Views - Last Post: 06 September 2008 - 10:08 AM Rate Topic: -----

#1 Thistleknot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 08

Blackjack help; struct's, vector's

Post icon  Posted 06 September 2008 - 09:17 AM

I'm trying to build a simulator for blackjack.

Right now I'm stuck quite early on. I got the shuffle algorithm going.
I had a very basic dealcards() method working for me, it just dealt two cards.
But then I've tried setting up a struct to handle split's. So I created a hands struct.

This is where I'm stuck.

I can't seem to get either one working.
Either a vector of hands
"252 D:\downloads\dev\bj\blackJack.cpp 'class std::vector<hands, std::allocator<hands> >' has no member named 'hand' "
or an array of hands.
error message when compiled, "248 D:\downloads\dev\bj\blackJack.cpp no matching function for call to `hands::hands()' "

Any help would be greatly appreciated.
I'm using dev-c++ to compile my code

/* ========================================================================== */
/*																			*/
/*   BlackJack.cpp															*/
/*   (c) 2008 Joshua Laferriere											   */
/*																			*/
/*   Description															  */
/*   This code compiles with Dev-C++ and it produces a neato chart of hi-lo   */
/*   values.  It also creates a bjchart.csv file useable to create charts.	*/
/* ========================================================================== */


#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <vector>

using namespace std;

ofstream bjCharts; //used to print cards

struct cards
{
	int index;
	string faceValue;
	string suite;
};


struct card
{
	string faceValue;
	string suite;
	int value;
	int index;
	
	//default constructor prototype w/definition
	card(string fV="", string s="", int v=0, int i=0)
	{
	faceValue=fV;
	suite=s;
	value=v;
	index=i;
	}; 
	 //index of card used for hand, this value will be set from 1-4 within the hands struct.
};


struct hands //used for vector of hand (for splitting)
{
	card hand[4]; //5 cards
	int index; //# of hand.
	int sumTotal;
	int numCards;
	
	//default constructor prototype for hands
	hands (card h[4], int i=0, int sT=0, int nC=0)
	{
		for (int x=0; x<4; x++)
		{
			hand[x]=h[x];
			x++;
		}
		index = i;
		sumTotal=sT;
		numCards=nC;
	};
	
	//this does not init this correctly, I'm initializing a 0 array as if it were an array.
	/*
	void initIndex()
	{
		for(int x=0; x<5; x++)
		{
			index=x;
			x++;
		}
	}
	*/
};

//default constructor for hands

void initShoe(cards shoe[], int numDecks)
{
	int i=0; // index of shoe
	string suite;
	
	for(int b = 0; b<numDecks; b++) // 0 to numdecks
	{
		for(int s=0;s<4;s++)  // 0 to 3 suites
		{
			if(s==0){suite="Clubs";}
			if(s==1){suite="Hearts";}
			if(s==2){suite="Spades";}
			if(s==3){suite="Diamonds";}
			
			for(int a=0;a<13;a++) // 0-12 number of cards per suite
			{
				shoe[i].index=i;
				
				//cout << "/n" << "(s)suite: " << suite;
				shoe[i].suite=suite;
				
				//cout << "\n" << shoe[i].suite;
				
				if(a==0){shoe[i].faceValue="A";}
				if(a==1){shoe[i].faceValue="2";}
				if(a==2){shoe[i].faceValue="3";}
				if(a==3){shoe[i].faceValue="4";}
				if(a==4){shoe[i].faceValue="5";}
				if(a==5){shoe[i].faceValue="6";}
				if(a==6){shoe[i].faceValue="7";}
				if(a==7){shoe[i].faceValue="8";}
				if(a==8){shoe[i].faceValue="9";}
				if(a==9){shoe[i].faceValue="10";}
				if(a==10){shoe[i].faceValue="J";}
				if(a==11){shoe[i].faceValue="Q";}
				if(a==12){shoe[i].faceValue="K";}
				//cout << "\n" << "(b)Deck: " << b << " " << "(s)Suite: " << suite << " " << "(a)Card: " << a;

				//cout << "\n" << shoe[i].faceValue;
				//cout << "\n" << shoe[i].suite << " ";
				//cout << "\n";

				i++;
			}
		}
	}
}

void printShoe(const cards shoe[], int sizeOfShoe) //I was thinking of passing this by reference, but in C++, array's are passed by reference automatically.  There is no local copy made.
{
	int value = 0;
	int addedValue = 0;
	int lowestValue=0;
	int highestValue=0;
	
	bjCharts.open("bjChart.csv", fstream::app);  //append
	   
	//cout << "\nSize of Shoe: " << sizeOfShoe;
	
	//this below code is to output a chart for viewing purposes

	for(int i=0; i<sizeOfShoe; i++)
	{
		if( (shoe[i].faceValue=="A") || (shoe[i].faceValue=="10") || (shoe[i].faceValue=="J") || (shoe[i].faceValue=="Q") || (shoe[i].faceValue=="K") )
		{
			addedValue = (-1);
		}
		if( (shoe[i].faceValue=="2") || (shoe[i].faceValue=="3") || (shoe[i].faceValue=="4") || (shoe[i].faceValue=="5") || (shoe[i].faceValue=="6") )
		{
			addedValue = 1;
		}
		if( (shoe[i].faceValue=="7") || (shoe[i].faceValue=="8") || (shoe[i].faceValue=="9") )
		{
			addedValue = 0;
		}
		
		//card count value
		value = value + addedValue;
		//cout << "\nCard Count: " << value << "/n";
		
		bjCharts << i << "," << value <<  "/n";
		
		if(value<0)
		{
			for(int x = 0; x < ( ( ( ( (sizeOfShoe/52) *4) +6) ) + value); x++) //precede with blanks
			{
				cout << " ";
			}
			for(int x=0; x<(value*(-1));x++) //fill with x
			{
				cout << "x";
			}
			cout << "0";
			cout << "\n";
		}
		else
		if(value>0) // if value is greater than 0
		{
			for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
			{
				cout << " ";
			}
			cout << "0";
			for(int x=0; x<value; x++)
			{
				cout << "x";   
			}
			cout << "\n";
		}
		else
		if(value==0)
		{
			for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
			{
				cout << " ";
			}
			cout << "0";
			cout << "\n";
		}
	
	//keep track of lowest and highest values
	/*
	if(value<lowestValue)
	{
		system("pause");
		cout << "\nLowest Value:" << value << "\n";
		lowestValue=value;
	}
	
	if(value>highestValue)
	{
		system("pause");
		cout << "\nHighest Value:" << value << "\n";
		highestValue=value;
	}
	*/
	
	}
	bjCharts.close();	
}

void shuffleShoe(cards shoe[], int sizeOfShoe)
{
	//--- Shuffle elements by randomly exchanging each with one other.
	for (int i=0; i<(sizeOfShoe-1); i++) //i is each card incrementally
	{
		int r = i + (rand() % (sizeOfShoe-i)); // Random remaining position.

		cards temp = shoe[i]; //temp card is value of i
		shoe[i] = shoe[r]; //move i to random position
		shoe[r] = temp; //old random position is temp value
		//it does this every card.  In affect, the deck is being shuffled twice
	}
}

void dealCards(cards shoe[], int sizeOfShoe)
{
	
	//note: can't use hand[0], confuses it with the hand[4] that's defined within hands struct
	
	vector<hands> ha[0];
	hands hb[0];
	
	cout << hb[0].hand[0].faceValue;
	
	cout << ha[0].hand[0].faceValue;

	/*
	for (int a=0; a<sizeOfShoe; a++)
	{
		//hand 1
		for (int x=0; x<5; x++)
		{

			

			cout << "\n" << hand[0].hand[x].faceValue;
			cout << "\nI'm here #2";			
			
			system("pause");
			

		}
	}
	*/
	
	
	
	
/*
	int sumOfCards=0;
	int numberOfCards=0;
	int i=0; //shoeIndex
	int cIndex=0; //cardIndex
	string choice="";
	
	cards hand1[5];

	for(int a=0; a<5; a++) //initialize hand to 0;
	{
		hand1[a].faceValue="";
		hand1[a].index=a;
		hand1[a].suite="";
	}
	
	
	do
	{
		
		hand1[a].faceValue=shoe[i].faceValue;
		hand1[a].suite=shoe[i].suite;
		
		cout "\n(h)it? (s)tand?";
		cin >> choice;
		i++;
		cIndex++;			
	}while(sumOfCards<21&&numCards<5&&(choice!="s");
	
	

	
	for(int i=0;i<sizeOfShoe;i++)
	{
		deal1=shoe[i];
		deal2=shoe[i+1];
		cout << deal1.faceValue << " " << deal2.faceValue << " ";
		system("pause");
	}
*/

system("pause");
}
	   

int main() 
{
	system("del bjChart.csv");
	/* initialize random seed: */
	srand ( time(NULL) );
	
	int numDecks;

	cout << "# of decks? \n";
	cin >> numDecks;
	cout << "\n";
	
	int sizeOfShoe = numDecks*52;

	//initialize shoe
	cards shoe[numDecks*52];
	
	//set values
	initShoe(shoe, numDecks);
	
	//print before shuffle
	//printShoe(shoe, sizeOfShoe);
	
	int numRuns = 1;
	
	cout << "\nNumber of runs?";
	cin >> numRuns;
	
	for(int r=0;r<numRuns;r++)
	{

		shuffleShoe(shoe, sizeOfShoe);
		//print after shuffle
		
		printShoe(shoe, sizeOfShoe);
	}
	
	dealCards(shoe, sizeOfShoe);
	system("pause");

}




Is This A Good Question/Topic? 0
  • +

Replies To: Blackjack help; struct's, vector's

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 09:31 AM

You don't have a default constructor for the hands struct. You think you're using this:
//default constructor prototype for hands
hands (card h[4], int i=0, int sT=0, int nC=0)

but as you can see, there's a required first argument you're not passing, so the compiler can't find a valid constructor.
Was This Post Helpful? 1
  • +
  • -

#3 Thistleknot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 09:35 AM

View PostJackOfAllTrades, on 6 Sep, 2008 - 09:31 AM, said:

You don't have a default constructor for the hands struct. You think you're using this:
//default constructor prototype for hands
hands (card h[4], int i=0, int sT=0, int nC=0)

but as you can see, there's a required first argument you're not passing, so the compiler can't find a valid constructor.


thanks, is the card h[4] argument the argument your referring to. I didn't know how to initialize the card h[4] to 0 or null values, so I defined a default constructor for card to take care of this for me. Assuming that if I did so, card h[4] would be called and the default constructor used in card.
Was This Post Helpful? 0
  • +
  • -

#4 Thistleknot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 09:45 AM

View PostJackOfAllTrades, on 6 Sep, 2008 - 09:31 AM, said:

You don't have a default constructor for the hands struct. You think you're using this:
//default constructor prototype for hands
hands (card h[4], int i=0, int sT=0, int nC=0)

but as you can see, there's a required first argument you're not passing, so the compiler can't find a valid constructor.


should I use something like this

	hands (card h[4]={("", "", 0, 0), ("", "", 0, 0),("", "", 0, 0),("", "", 0, 0),("", "", 0, 0)}, int i=0, int sT=0, int nC=0)


Was This Post Helpful? 0
  • +
  • -

#5 Thistleknot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 09:56 AM

View PostJackOfAllTrades, on 6 Sep, 2008 - 09:31 AM, said:

You don't have a default constructor for the hands struct. You think you're using this:
//default constructor prototype for hands
hands (card h[4], int i=0, int sT=0, int nC=0)

but as you can see, there's a required first argument you're not passing, so the compiler can't find a valid constructor.


WAIT. I get the exact same error with the vector when I comment out the constructor's (much to my appeasement). However, the standard hb[] array works fine.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 10:05 AM

OK, I did a few tweaks for you.

First, we need to get rid of some "magic numbers", so I added these #defines to the top of your code:
#define MAX_CARD_COUNT 5
#define SUIT_COUNT 4
#define CARDS_PER_SUIT 13



Next, fixed your constructors.
NOTE: If it's 5 cards, you must have an array of length 5, not 4; you're confusing array length and indexing:
struct hands //used for vector of hand (for splitting)
{

   card hand[MAX_CARD_COUNT]; //5 cards
   int index; //# of hand.
   int sumTotal;
   int numCards;

   //default constructor prototype for hands
   hands (int i=0, int sT=0, int nC=0) {
      index = i;
      sumTotal=sT;
      numCards=nC;
   }

   hands (card h[MAX_CARD_COUNT], int i=0, int sT=0, int nC=0) {
      for (int x=0; x < MAX_CARD_COUNT; x++) {
         hand[x]=h[x];
      }

      index = i;
      sumTotal=sT;
      numCards=nC;
   }
};



Changed the for loops in initShoe to not use the magic numbers:
for(int s=0;s<SUIT_COUNT;s++)  // 0 to 3 suites

for(int a=0;a<CARDS_PER_SUIT;a++)


Changed dealCards. NOTE: You were using arrays of length 0 here...uh, don't do that.
void dealCards(cards shoe[], int sizeOfShoe)
{
   vector<hands> ha;

   hands hb;

   cout << hb.hand[0].faceValue;

   cout << ha[0].hand[0].faceValue;



This at least compiles so that you can continue your testing. I won't vouch for how it works though.
Was This Post Helpful? 0
  • +
  • -

#7 Thistleknot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 08

Re: Blackjack help; struct's, vector's

Posted 06 September 2008 - 10:08 AM

View PostThistleknot, on 6 Sep, 2008 - 09:56 AM, said:

View PostJackOfAllTrades, on 6 Sep, 2008 - 09:31 AM, said:

You don't have a default constructor for the hands struct. You think you're using this:
//default constructor prototype for hands
hands (card h[4], int i=0, int sT=0, int nC=0)

but as you can see, there's a required first argument you're not passing, so the compiler can't find a valid constructor.


WAIT. I get the exact same error with the vector when I comment out the constructor's (much to my appeasement). However, the standard hb[] array works fine.


commenting out the default constructors, I'm able to use a simple array to accomplish dealing 5 cards
/* ========================================================================== */
/*																			*/
/*   BlackJack.cpp															*/
/*   (c) 2008 Joshua Laferriere											   */
/*																			*/
/*   Description															  */
/*   This code compiles with Dev-C++ and it produces a neato chart of hi-lo   */
/*   values.  It also creates a bjchart.csv file useable to create charts.	*/
/* ========================================================================== */


#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <vector>

using namespace std;

ofstream bjCharts; //used to print cards

struct cards
{
	int index;
	string faceValue;
	string suite;
};


struct card
{
	string faceValue;
	string suite;
	int value;
	//index of card used for hand, this value will be set from 1-4 within the hands struct.
	int index;
	
	//default constructor prototype w/definition
	/*
	card(string fV="", string s="", int v=0, int i=0)
	{
	faceValue=fV;
	suite=s;
	value=v;
	index=i;
	}; 
	*/

};


struct hands //used for vector of hand (for splitting)
{
	card cArd[5]; //5 cards
	int index; //# of hand.
	int sumTotal;
	int numCards;
	
	//default constructor prototype for hands
	/*
	hands (card h[4], int i=0, int sT=0, int nC=0)
	{
		for (int x=0; x<4; x++)
		{
			hand[x]=h[x];
			x++;
		}
		index = i;
		sumTotal=sT;
		numCards=nC;
	};
	*/
};

//default constructor for hands

void initShoe(cards shoe[], int numDecks)
{
	int i=0; // index of shoe
	string suite;
	
	for(int b = 0; b<numDecks; b++) // 0 to numdecks
	{
		for(int s=0;s<4;s++)  // 0 to 3 suites
		{
			if(s==0){suite="Clubs";}
			if(s==1){suite="Hearts";}
			if(s==2){suite="Spades";}
			if(s==3){suite="Diamonds";}
			
			for(int a=0;a<13;a++) // 0-12 number of cards per suite
			{
				shoe[i].index=i;
				
				//cout << "/n" << "(s)suite: " << suite;
				shoe[i].suite=suite;
				
				//cout << "\n" << shoe[i].suite;
				
				if(a==0){shoe[i].faceValue="A";}
				if(a==1){shoe[i].faceValue="2";}
				if(a==2){shoe[i].faceValue="3";}
				if(a==3){shoe[i].faceValue="4";}
				if(a==4){shoe[i].faceValue="5";}
				if(a==5){shoe[i].faceValue="6";}
				if(a==6){shoe[i].faceValue="7";}
				if(a==7){shoe[i].faceValue="8";}
				if(a==8){shoe[i].faceValue="9";}
				if(a==9){shoe[i].faceValue="10";}
				if(a==10){shoe[i].faceValue="J";}
				if(a==11){shoe[i].faceValue="Q";}
				if(a==12){shoe[i].faceValue="K";}
				//cout << "\n" << "(b)Deck: " << b << " " << "(s)Suite: " << suite << " " << "(a)Card: " << a;

				//cout << "\n" << shoe[i].faceValue;
				//cout << "\n" << shoe[i].suite << " ";
				//cout << "\n";

				i++;
			}
		}
	}
}

void printShoe(const cards shoe[], int sizeOfShoe) //I was thinking of passing this by reference, but in C++, array's are passed by reference automatically.  There is no local copy made.
{
	int value = 0;
	int addedValue = 0;
	int lowestValue=0;
	int highestValue=0;
	
	bjCharts.open("bjChart.csv", fstream::app);  //append
	   
	//cout << "\nSize of Shoe: " << sizeOfShoe;
	
	//this below code is to output a chart for viewing purposes

	for(int i=0; i<sizeOfShoe; i++)
	{
		if( (shoe[i].faceValue=="A") || (shoe[i].faceValue=="10") || (shoe[i].faceValue=="J") || (shoe[i].faceValue=="Q") || (shoe[i].faceValue=="K") )
		{
			addedValue = (-1);
		}
		if( (shoe[i].faceValue=="2") || (shoe[i].faceValue=="3") || (shoe[i].faceValue=="4") || (shoe[i].faceValue=="5") || (shoe[i].faceValue=="6") )
		{
			addedValue = 1;
		}
		if( (shoe[i].faceValue=="7") || (shoe[i].faceValue=="8") || (shoe[i].faceValue=="9") )
		{
			addedValue = 0;
		}
		
		//card count value
		value = value + addedValue;
		//cout << "\nCard Count: " << value << "/n";
		
		bjCharts << i << "," << value <<  "/n";
		
		if(value<0)
		{
			for(int x = 0; x < ( ( ( ( (sizeOfShoe/52) *4) +6) ) + value); x++) //precede with blanks
			{
				cout << " ";
			}
			for(int x=0; x<(value*(-1));x++) //fill with x
			{
				cout << "x";
			}
			cout << "0";
			cout << "\n";
		}
		else
		if(value>0) // if value is greater than 0
		{
			for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
			{
				cout << " ";
			}
			cout << "0";
			for(int x=0; x<value; x++)
			{
				cout << "x";   
			}
			cout << "\n";
		}
		else
		if(value==0)
		{
			for(int x=0; x<( ( (sizeOfShoe/52) *4) +6); x++)
			{
				cout << " ";
			}
			cout << "0";
			cout << "\n";
		}
	
	//keep track of lowest and highest values
	/*
	if(value<lowestValue)
	{
		system("pause");
		cout << "\nLowest Value:" << value << "\n";
		lowestValue=value;
	}
	
	if(value>highestValue)
	{
		system("pause");
		cout << "\nHighest Value:" << value << "\n";
		highestValue=value;
	}
	*/
	
	}
	bjCharts.close();	
}

void shuffleShoe(cards shoe[], int sizeOfShoe)
{
	//--- Shuffle elements by randomly exchanging each with one other.
	for (int i=0; i<(sizeOfShoe-1); i++) //i is each card incrementally
	{
		int r = i + (rand() % (sizeOfShoe-i)); // Random remaining position.

		cards temp = shoe[i]; //temp card is value of i
		shoe[i] = shoe[r]; //move i to random position
		shoe[r] = temp; //old random position is temp value
		//it does this every card.  In affect, the deck is being shuffled twice
	}
}

void dealCards(cards shoe[], int sizeOfShoe)
{
	
	//note: can't use hand[0], confuses it with the hand[4] that's defined within hands struct
	
	//having problem using vectors.  Doesn't find members of hands struct within vector
	//vector<hands> ha[1];
	
	//used for hand, up to four hands
	hands ha[4];
	
	//go through deck
	
	int x=0;
	
	for (int a=0; a<sizeOfShoe; a++)
	{
		if(x==5){break;}
		//hand 1

		ha[0].cArd[x].faceValue=shoe[a].faceValue;
		cout << "\nCard: " << ha[0].cArd[x].faceValue;
		x++;
			
		system("pause");
		
	}

/*
	int sumOfCards=0;
	int numberOfCards=0;
	int i=0; //shoeIndex
	int cIndex=0; //cardIndex
	string choice="";
	
	cards hand1[5];

	for(int a=0; a<5; a++) //initialize hand to 0;
	{
		hand1[a].faceValue="";
		hand1[a].index=a;
		hand1[a].suite="";
	}
	
	
	do
	{
		
		hand1[a].faceValue=shoe[i].faceValue;
		hand1[a].suite=shoe[i].suite;
		
		cout "\n(h)it? (s)tand?";
		cin >> choice;
		i++;
		cIndex++;			
	}while(sumOfCards<21&&numCards<5&&(choice!="s");
	
	

	
	for(int i=0;i<sizeOfShoe;i++)
	{
		deal1=shoe[i];
		deal2=shoe[i+1];
		cout << deal1.faceValue << " " << deal2.faceValue << " ";
		system("pause");
	}
*/

system("pause");
}
	   

int main() 
{
	system("del bjChart.csv");
	/* initialize random seed: */
	srand ( time(NULL) );
	
	int numDecks;

	cout << "# of decks? \n";
	cin >> numDecks;
	cout << "\n";
	
	int sizeOfShoe = numDecks*52;

	//initialize shoe
	cards shoe[numDecks*52];
	
	//set values
	initShoe(shoe, numDecks);
	
	//print before shuffle
	//printShoe(shoe, sizeOfShoe);
	
	int numRuns = 1;
	
	cout << "\nNumber of runs?";
	cin >> numRuns;
	
	for(int r=0;r<numRuns;r++)
	{

		shuffleShoe(shoe, sizeOfShoe);
		//print after shuffle
		
		printShoe(shoe, sizeOfShoe);
	}
	
	dealCards(shoe, sizeOfShoe);
	system("pause");

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1