Thursday, July 9, 2009

C++ array question?

I'm trying to figure out how to store cards in a deck...the only thing I can think of is to store the cards in an array, with each card having a form (value)(suit) such as 6c for six of clubs, and 12h for queen of hearts.





So anyway, if I were to make a header function to shuffle the cards, could I do this:





void shuffle(array deck);





Can you use deck of type array as the parameter for the function, or will that not work?

C++ array question?
Absolutely.





Just use the [] delimeter to let the compiler know you're expecting an array pointer:





void shuffle(string[] deck);





Or whatever data type you're using to represent a card.





===





From a design standpoint, I think that using the "6c" format would be kind of problematic, because you then have to parse that token to get the pieces out. What I would suggest is using parallel arrays (there are more complicated structures such as structs, classes, and multidimensional arrays--but I'm assuming this is a beginning programming course so parallel arrays is what you're after).





In other words, keep two arrays of the same size, one that holds the number and one that holds the suit as an integer (1-4).





Consider this:





const int DECK_SIZE = 52;





int value[DECK_SIZE];


int suit[DECK_SIZE];





// function prototype


void shuffle(int[], int[]);





// store card 0 in the deck


value[0] = 12;


suit[0] = 3;





// function invocation:


shuffle(value, suit);





// function definition


void shuffle(int[] value, int[] suit) {


// shuffle here


}





Now you could store a value (1-13) in the value array, and a suit (1-4) in the suit array. If you want to be really fancy you could make an enumeration for the suit array, but that's up to you.
Reply:%26gt; I'm trying to figure out how to store cards in a deck...the only thing I can think of is to store the cards in an array,





Whatever data structures you use, somewhere one or more arrays will be involved.





%26gt; So anyway, if I were to make a header function to shuffle the cards, could I do this:





I'm going to pick on your terminology here (this isn't a minor point either). Functions are functions. What you put in a header file is called a declaration. What you put in source files is a definition. That's why you #include header files. You want all the declarations at the top of your source file, so you can use the functions.





%26gt; void shuffle(array deck);





What's the problem with that declaration there? See array deck? Well, we have int someint. char somechar. float somefloat. Now, what's array someArray? If you don't know how to use arrays: http://www.cplusplus.com/doc/tutorial/ar... and http://www.cprogramming.com/tutorial/les... .





What is the datatype of deck? There's no array datatype. It's a complex structure. So either Deck is a class, or you have an array of cards (which might be structures).





Also worth asking, are you familiar with the C++ STL? Have you heard of C++ Strings, vectors, map, and so on?





Finally, ask for help on either http://forums.devshed.com/ or http://cboard.cprogramming.com/ as there are actual professional programmers there who can give you technical accurate advice.
Reply:yes, it can work this way


and shuffle() will use call-by-reference to do this





your program should look like that





#include %26lt;iostream%26gt;


using namespace std;


//function prototypes


void shuffle(char *);





int main() {


//defining 4 dimensional array


char* deck[13][4][4] = {


{"Ac", "2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "10c", "Jc", "Qc", "Kc"},


{"Ad", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d", "Jd", "Qd", "Kd"},


{"Ah", "2h", "3h", "4h", "5h", "6h", "7h", "8h", "9h", "10h", "Jh", "Qh", "Kh"},


{"As", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "10s", "Js", "Qs", "Ks"}


};





//some lines of code


//some lines of code


//some lines of code


//some lines of code





//calling shuffe() function by ref


shuffle(deck[13][4][4]);


return 0;


}





void shuffle(char *deck) {


//some lines of code


//some lines of code


//some lines of code


//some lines of code


}





haha, copy and paste that i hope it works for you.


I'm using a 4D array, as you can see, passing it by reference to shuffle(char* )!





hope i helped!


please ask if you dont understand my prog! %26gt;.%26lt;


No comments:

Post a Comment