Advertisement

Confused about Blackjack Code

Started by March 04, 2002 11:20 PM
4 comments, last by gone_postal 22 years, 6 months ago
    
#include "stdafx.h"

#include <iostream>
//just placing the cards to the deck...

int Deck[53];
int i;
while (i < 14)
{
	Deck[i]=i+1;
	i++;
}

while (i < 27)
{
	Deck[i]=(i-13);
	i++;
}

while (i < 40)
{
	Deck[i]=(i-26);
	i++;
}


while (i < 53)
{
	Deck[i]=(i-39);
	i++;
}

/*
Deck[0] = 1;
Deck[1] = 2;
Deck[2] = 3;
Deck[4] = 4;
Deck[5] = 5;
Deck[6] = 6;
Deck[7] = 7;
Deck[8] = 8;
Deck[9] = 9;
Deck[10] = 10;
Deck[11] = 10;
Deck[12] = 10;
Deck[13] = 10;
Deck[14] = 1;
Deck[15] = 2;
Deck[16] = 3;
Deck[17] = 4;
Deck[18] = 5;
Deck[19] = 6;
Deck[20] = 7;
Deck[21] = 8;
Deck[22] = 9;
Deck[23] = 10;
Deck[24] = 10;
Deck[25] = 10;
Deck[26] = 10;
Deck[27] = 1;
Deck[28] = 2;
Deck[29] = 3;
Deck[30] = 4;
Deck[31] = 5;
Deck[32] = 6;
Deck[33] = 7;
Deck[34] = 8;
Deck[35] = 9;
Deck[36] = 10;
Deck[37] = 10;
Deck[38] = 10;
Deck[39] = 10;
Deck[40] = 1;
Deck[41] = 2;
Deck[42] = 3;
Deck[43] = 4;
Deck[44] = 5;
Deck[45] = 6;
Deck[46] = 7;
Deck[47] = 8;
Deck[48] = 9;
Deck[49] = 10;
Deck[50] = 10;
Deck[51] = 10;
Deck[52] = 10;
*/
//shuffling deck

int i1, i2, tS;
int t = 0;
while (t < 52)
{
	++t
	Deck[t] = t;
}
int z = 0;
while (z < NUM_SWAPS)
{  
	++z;
	i1 = (int)(((rand ()/RAND_MAX) * 51.0f) + 1.0f);
	i2 = (int)(((rand ()/RAND_MAX) * 51.0f) + 1.0f);  tS = Deck[i1];
	Deck[i1] = Deck[i2];  Deck[i2] = tS;
}

int deal();
int oDeal();
int draw();
int oDraw();

using namespace std;

int main(int argc, char* argv[])
{
	int sumOfCards;
	int sumOfOCards;
	int choice;
	sumOfCards=deal();  //   dealing cards for both players

	sumOfOCards=oDeal();//

	cout << "What do you want to do now:\n(1)Draw another card\n(2)Stay where you're at\n";
	cin >> choice;//player chooses whether to draw or stay

	switch (choice)
	{
	case 1:
		sumOfCards=sumOfCards+draw();
		break;
	case 2:
		break;
	}
	cout << "\nThe computer is deciding what to do now.\n";//"ai" decides what to do

	if (sumOfOCards<=15)
	{
		sumOfOCards=sumOfOCards+oDraw();
	}
	if (sumOfCards>21)//lose and win conditions

	{
		cout << "Sorry, but you went over 21.  You lose.\n";
	}
	if (sumOfCards==21)
	{
		cout << "Good job, you got exactly 21.  You win.\n";
	}
	if (sumOfOCards>21)
	{
		cout << "The computer went over 21.  You win!\n";
	}
	if (sumOfOCards==21)
	{
		cout << "The computer got exactly 21.  You lose.\n";
	}
	if (sumOfCards>sumOfOCards)
	{
		cout << "You got more points than the computer.  You win!\n";
	}
	if (sumOfCards<sumOfOCards)
	{
		cout << "The computer got more points than you.  You lose.\n";
	}
	return 0;
}

int deal()
{
	cout << "You were dealt the cards with values: " << Deck[0] << "and " << Deck[1] << ".";
	cout << "\n";
	int total;
	total = Deck[0]+Deck[1];
	return total;
}

int oDeal()
{
	cout << "The computer has been dealt for.\n";
	int oTotal;
	oTotal = Deck[2]+Deck[3];
	return oTotal;
}

int draw()
{
	cout << "You got the card: " << Deck[4] << ".\n";
	return Deck[4];
}

int oDraw()
{
	cout << "The computer drew.\n";
	return Deck[5];
}
  
Why doesn't this work? I get: error C2447: missing function header (old-style formal list?) and error C2143: syntax error : missing ';' before 'while' I'm really confused, so any help would be appreciated. Thanks! Edited by - gone_postal on March 4, 2002 12:24:50 AM
You cannot write code (i.e. your while loops) outside of a function.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Heres my new code:
  #include "stdafx.h"#include <iostream>#include <algorithm>int Deck [52];//just placing the cards to the deck...int deal();int oDeal();int draw();int oDraw();using namespace std;int main(int argc, char* argv[]){	int i;	i=0;	while (i < 14)	{		Deck[i]=i+1;		i++;	};	while (i < 27)	{		Deck[i]=(i-13);		i++;	};		while (i < 40)	{		Deck[i]=(i-26);		i++;	};			while (i < 53)	{		Deck[i]=(i-39);		i++;	};	int i1, i2, tS;	int t = 0;	while (t < 52)	{		++t;		Deck[t] = t;	};bug right here	for (i=0; i<52; ++i)----------->>>		std::swap (Deck , Deck [rand () % 52]);		int sumOfCards;	int sumOfOCards;	int choice;	sumOfCards=deal();  //   dealing cards for both players	sumOfOCards=oDeal();//	cout << "What do you want to do now:\n(1)Draw another card\n(2)Stay where you''re at\n";	cin >> choice;//player chooses whether to draw or stay	switch (choice)	{	case 1:		sumOfCards=sumOfCards+draw();		break;	case 2:		break;	}	cout << "\nThe computer is deciding what to do now.\n";//"ai" decides what to do	if (sumOfOCards<=15)	{		sumOfOCards=sumOfOCards+oDraw();	}	if (sumOfCards>21)//lose and win conditions	{		cout << "Sorry, but you went over 21.  You lose.\n";	}	if (sumOfCards==21)	{		cout << "Good job, you got exactly 21.  You win.\n";	}	if (sumOfOCards>21)	{		cout << "The computer went over 21.  You win!\n";	}	if (sumOfOCards==21)	{		cout << "The computer got exactly 21.  You lose.\n";	}	if (sumOfCards>sumOfOCards)	{		cout << "You got more points than the computer.  You win!\n";	}	if (sumOfCards<sumOfOCards)	{		cout << "The computer got more points than you.  You lose.\n";	}	return 0;}int deal(){	cout << "You were dealt the cards with values: " << Deck[0] << " and " << Deck[1] << ".";	cout << "\n";	int total;	total = Deck[0]+Deck[1];	return total;}int oDeal(){	cout << "The computer has been dealt for.\n";	int oTotal;	oTotal = Deck[2]+Deck[3];	return oTotal;}int draw(){	cout << "You got the card: " << Deck[4] << ".\n";	return Deck[4];}int oDraw(){	cout << "The computer drew.\n";	return Deck[5];}  

now it gives me "error C2782: ''void __cdecl std::swap(_Ty &,_Ty &)'' : template parameter ''_Ty'' is ambiguous" right where i marked... dang, i cant seem 2 fix this right! :D
Deck and Deck[index] are not the same type, you cannot swap them.

And why don't use the std::random_shuffle algorithm ?

#include <algorithm>
std::random_shuffle( Deck, Deck+52 );

And your deck is shuffled...

Edit: by the way, why not do
  int i;for( i=0; i<13; ++i ){  Deck[i] = i+1;  Deck[i+13] = i+1;  Deck[i+26] = i+1;  Deck[i+39] = i+1;}Edited by - Fruny on March 5, 2002 3:01:51 AM  
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Well, I did the random_shuffle idea, and for once it stopped giving me "you have been dealt cards 1 and 1". Now, though, it only gives me cards 5 and 7! I don''t understand why it would do that... any help?
Jeez, I feel like I''m leeching ideas off this site because I''m not good enough to do this myself... I''m really sorry if it seems that way, I just haven''t learned this yet.
Have you initialized the random number generator ?

Add a srand( time( NULL ) ); at the beginning of your program (and #include <ctime> as well as <cstdlib>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement