-
Blackjack
Got an error with me' code.... was wondering if someone could take a look at the method behind my madness...
Here is the code so far.. This code is a prototype for a function that I hope to use in my blackjack program. Hopefully, this will allow me to do something like...
cout << deck[1][5] to display "7 of Diamonds" for example.
Code:
#include<cstring> //provides strcat()
#include<string> //provides string class
using namespace std;
int main()
{
string deck[3][12]; //create a deck of cards, 4 suits, 13 cards per suit (2 thru Ace)
string suite, face_value, card;
for (int i = 0; i < 4; i++) //Use the outter FOR loop to determine the suite of deck[suite][face_value]
{
switch(i)
{
case 0: suite = "Hearts";
break;
case 1: suite = "Diamonds";
break;
case 2: suite = "Clubs";
break;
case 3: suite = "Spades";
break;
}//end switch
for (int j = 0; j < 13; j++) //The Inner FOR loop will assign card face values
{
int k = j; //this allows me to add "2" to the j counter without influencing the FOR loop.
if (j < 9) //elements [0] thru [8] can be populated with numbers 2 thru 10 (numeric card face values)
face_value = (k + 2); //this allow element zero to contain face value "2"
else
{
switch(j)
{
case 9: face_value = "Jack";
break;
case 10: face_value = "Queen";
break;
case 11: face_value = "King";
break;
case 12: face_value = "Ace";
break;
}//end switch
}//end else
//This block will create a "card" to be assigned to the deck
card = face_value;
strcat(card, " of ");
card += suite;
deck[i][j] = card;
}//end INNER FOR
}//end OUTTER FOR
return 0;
}//end MAIN()
Here is the error I be gettin'... using MS visual C++
--------------------Configuration: bj - Win32 Debug--------------------
Compiling...
shuffletest.cpp
C:\MySource\shuffletest.cpp(67) : error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
shuffletest.obj - 1 error(s), 0 warning(s)
Please help if you can :)
-
face_value = (k + 2);
Do I have to cast (k+2) to a string before assigning to 'face_value'?
If so.. how would I do this...??
:confused:
-
string deck[3][12];
That is a 3 by 12 2D array. You want a 4 by 13 so you want this:
string deck[4][13];
Valid elements range from 0-3 and 0-12 respectively.
Code:
You don't need k.
switch(j)
{
case 0:
cout << "Ace";
break;
case 1:
face_value = "1";
break
//etc;
case 10:
face_value = "Jack";
break;
etc;
}
will do. You may be able to assign an int directly to a string, but I tend to doubt it. You could change the int to a C style string using one of the following:
sprintf();
stringstream;
itoa(); //non-standard
and then assign the C style string to face_value, but for my money I'd just do the 13 level switch.
-
Yeah, I don't think assigning an int to a string will work, probably better off using the big switch statement or something like this:
Code:
string values[13]={"Ace", "Two", etc etc}
face_value=values[j];
The error is from the fact that you are supplying the strcat function with an argument defined as a string when it takes a char array instead.
-
You are right.. I tried to use strcat( ) with a string instead of a character array..
This is all I had to do to fix me' problem:
Code:
//This block will create a "card" to be assigned to the deck
card = face_value;
card += " of ";
card += suite;
I was looking forward to using cstring library functions.. but I guess it won't work this time around. :)
and I forgot that the '/0' null character is assigned after the last used element of string arrays.. so good call by elad. :)
Thanks to elad and PJYelton :cool:
I haven't casted (k+2) to a 'string' yet.. so far everything compiles fine
--------------------Configuration: bj - Win32 Debug--------------------
Compiling...
shuffletest.cpp
shuffletest.obj - 0 error(s), 0 warning(s)
-
Yikes... got the dreaded, "segmentation error"... Hard to debug.. :(
Just trying to cout the deck[][] to see if it was initialized properly.
Code:
#include<cstring> //provides strcat()
#include<string> //provides string class
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string deck[4][13]; //create a deck of cards, 4 suits, 13 cards per suit
string suite, face_value, card;
for (int i = 0; i < 4; i++) //Use the outter FOR loop to determine the suite of deck[suite][face_value]
{
switch(i)
{
case 0: suite = "Hearts";
break;
case 1: suite = "Diamonds";
break;
case 2: suite = "Clubs";
break;
case 3: suite = "Spades";
break;
}//end switch
for (int j = 0; j < 13; j++) //The Inner FOR loop will assign card face values
{
int k = j; //this allows me to add 2 to the j counter without influencing the for loop.
if (j < 9) //elements [0] thru [8] can be populated with numbers 2 thru 10 (numeric card face values)
face_value = (k + 2); //this allow element zero to contain face value "2"
else
{
switch(j)
{
case 9: face_value = "Jack";
break;
case 10: face_value = "Queen";
break;
case 11: face_value = "King";
break;
case 12: face_value = "Ace";
break;
}//end switch
}//end else
//This block will create a "card" to be assigned to the deck
card = face_value;
card += " of ";
card += suite;
deck[i][j] = card;
}//end INNER FOR
}//end OUTTER FOR
system("cls");
for (i = 0; i < 4; i++) //MS visual c++ wants "i" redefined, bloodshed doesn't
for (int j = 0; j < 53; j++)
cout << endl << deck[i][j] << endl;
return 0;
}//end MAIN()
-
>>for (int j = 0; j < 53; j++)
I think that might be a typo :)
-
You guys are awesome....
I found out.. that although you can cast strings to integers ( atoi() for example).... there is nothing to cast from integers to strings... ( I tried card = static_cast<string>(k+2) but that didn't jive) so... went with declaring everything as a string... and using the 13 level switch case....
Here it is...
Tada... :)
Code:
#include<string> //provides string class
#include<iostream> //provides 'cin' and 'cout'
#include<cstdlib> //provides system( )
using namespace std;
int main()
{
string deck[4][13]; //create a deck of cards, 4 suits, 13 cards per suit
string suite, face_value, card;
for (int i = 0; i < 4; i++) //Use the outter FOR loop to determine the suite of deck[suite][face_value]
{
switch(i)
{
case 0: suite = "Hearts";
break;
case 1: suite = "Diamonds";
break;
case 2: suite = "Clubs";
break;
case 3: suite = "Spades";
break;
default: cout << "\n\nsuite loop overflow\n";
}//end switch
for (int j = 0; j < 13; j++) //The Inner FOR loop will assign card face values
{
switch(j)
{
case 0: face_value = "2";
break;
case 1: face_value = "3";
break;
case 2: face_value = "4";
break;
case 3: face_value = "5";
break;
case 4: face_value = "6";
break;
case 5: face_value = "7";
break;
case 6: face_value = "8";
break;
case 7: face_value = "9";
break;
case 8: face_value = "10";
break;
case 9: face_value = "Jack";
break;
case 10: face_value = "Queen";
break;
case 11: face_value = "King";
break;
case 12: face_value = "Ace";
break;
default: cout << "\n\nface_value loop overflow\n";
}//end switch
//This block will create a "card" to be assigned to the deck
card = face_value;
card += " of ";
card += suite;
deck[i][j] = card;
}//end INNER FOR
}//end OUTTER FOR
system("cls");
//Display the deck
for (int p = 0; p < 4; p++)
for (int q = 0; q < 13 ; q++)
cout << endl << deck[p][q] << endl;
return 0;
}//end MAIN()
thanks again everyone :cool:
-
Just curious how do you plan to shuffle your deck?
-
Actually you can convert (technically not a cast) an integer to a std::string and a std::string to an integer. Check out the faq, I know it's in there and it involves using streams.
-
I'm glad you asked...
I thought about this.. and came up with two ways to approach this problem:
The first way.. would use a one dimensional array deck[52] for example. The advantage with using a single dimensional array would be that I could shuffle the deck.. and draw from the top of the stack just like you would in real life. :)
so I was thinking something like...
Code:
void shuffle()
{
for (int i = 0; i < 52: i++)
deck[i] = deck[rand()%52];
}
This algorithm by itself is not perfect (you could assign many dupicates of the same card to the deck for example) To get around this.. you make a camparison of "card_drawn" to "cards_in_play". If there is a duplication, simply make another function call to shuffle(). :)
With using a 2D deck however, there is no luxury of shuffling the deck to remove a card from the top of the stack... so the way you get around this.. is to draw random cards from deck[][]... kinda like how a magician would ask you to, "pick a card.. any card... " :cool:
Code:
string get_card()
{
int suite = 0,
face_value = 0;
suite = rand()%4;
face_value = rand()%13;
string card_drawn = deck[suite][face_value];
return card_drawn;
}
Again.. with this method, there is a possibility to draw duplicate cards.. so simply make a comparison if (card_drawn == cards_in_play) then get_card().
Maybe there are better ways to go about this... so if you got any better suggestions.. just let me know... and maybe others can learn as well :)
-
I prefer the one-dimensional deck with a shuffle function. I made a Deck class way back in the day and used it for several different card games (including blackjack). If I find it and you want to take a peek, I can send it to you.
-
:)
that would be very cool :cool:
email addy is in me' profile if you want to send it my way :)
-
Code:
int main()
{
int deck[52];
/* assign inital values to deck */
for (int count=0; count<100; count++)
swap(deck, random(52), random(52));
for (int count=0; count<sizeof deck / sizeof deck[0]; count++)
std::cout<<deck[count]<<endl;
}
void swap(int *arr, int x, int y)
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
Note: There is also a swap function in <algorithm> and you should really use that one instead.
By doing it this way you can ensure that you won't have duplicates entires in the array.
-
Another similar way to shuffle a deck which gives a perfectly random deck is:
Code:
for (int x=0; x<52; x++)
swap (deck[x], deck[random()%52]);
The advantage to this is that you don't need to worry about what number (100 in thantos's example) is the best number to use.