Teen Programmers Unite  
 

 

Return to forum top

C++ help please

Posted by jay_dee [send private reply] at May 27, 2002, 04:33:44 PM

i'm trying to make a simple card game(no graphics or anything) I know how to make a random number and store it in an array but what i want to know how to do is if the card value is 11, 12,13,14 i need to be able to cange what is stored in the array to a letter..i.e. j, q....i know how to use IF and all that but what my problem is the random number is a INT value and "j", "q" and CHAR values..so is there a way i can make this work?

please forgive any typeing errors..or if i'm not makeing any sense..i've been up for too long and have had way too much coffee. thanks,
jay

Posted by sphinX [send private reply] at May 27, 2002, 04:43:18 PM

Why not just use a char value for your input number? char is an 8bit number in most (if not all) implementations, and therefore (unsigned) can store 0-255. If you're only going up to 14, then why not? Then you could just use a select statement with the cases 11,12,13 and 14. Or alternatively, use an enumerated type, which you can find by looking up the keyword "enum" in your favourite reference.

Posted by infryq [send private reply] at May 27, 2002, 06:08:18 PM

basically, what sphinX said. but with more details:

char or int technically doesn't matter; char will take up less space(obviously) so it's a better idea. you can cast your int to a char if you didn't already know that was do-able.

I highly suggest, as sphinX mentioned, learning how to use enum.

if not, you should just be able to put in a switch/case statement to fill in your letters, and make it part of the method that randomizes the cards and sets the array for your hand. (just put it in between where you come up with a random number and where you set the array cell).

Posted by Psion [send private reply] at May 27, 2002, 06:21:08 PM

And for the interested, 'j', 'q', and other character literals actually ARE already of type int in C.

Posted by ScaryGuy [send private reply] at May 29, 2002, 01:31:25 AM

You could hold a whole deck of cards in one char, try bit manipulating.

Posted by sphinX [send private reply] at May 29, 2002, 05:59:33 AM

With the processing power of modern day machines, bit manipulation could become slower than just manipulating integers, considering the average 32-bit compiler compiles on a 32-bit machine and defines a 32-bit int, which makes processing faster within the machine. Not to mention the tedious nature of bit manipulation if you're not used to counting in binary.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.