#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DECK 52 //Cards in a deck
#define HEARTS 0x48 //Hearts in hex
#define DIAMONDS 0x44 //Diamond in hex
#define CLUBS 0x43 //Clubs in hex
#define SPADES 0x53 //Spade in hex
int draw_card(void);
void shuffle(void);
int rnd(int range);
void seedrnd(void);
/*
* The cards array is stuck out here in no-man's land
* to make it available to both the shuffle and
* draw_card functions
*/
int cards[DECK]; //Deck of Cards
int main()
{
int x=DECK;
int card;
char c,s;
int T= 0xA;
puts("After shuffling");
shuffle();
/*
* Sift through the entire deck, decrementing
* variable x from 52 down to zero.
*/
while(x--)
{
card = draw_card();
switch((card % 13 ) + 1)
{
case(1):
c = 'A';
break;
case(10):
c = 'T' ; /* I would like to display 10 here instead of T*/
break;
case(11):
c = 'J';
break;
case(12):
c = 'Q';
break;
case(13):
c = 'K';
break;
default:
c = (card % 13 )+'1';
}
/*
* The final printf displays the values
*/
if(card>=0 && card<=13)
s = HEARTS;
else if(card>=14 && card<=26)
s = DIAMONDS;
else if(card>=27 && card<=40)
s = CLUBS;
else
s = SPADES;
printf("% c % c\t",c,s);
if(!(x%4))
putchar('\n');
}
}
/*
* The draw_card routine returns a random
* number from 1 to 52 for each card in the deck
*/
int draw_card(void)
{
int card;
do
{
card=rnd(DECK); //draw card
}
while(cards[card]); //is card drawn?
cards[card]=1; //mark it as drawn
return(card+1); //make it 1 to 52
}
/*
* The shuffle routine does two things:
* It initializes the randomizer and
* It initializes the cards[] array
*/
void shuffle(void)
{
int x;
seedrnd();
for(x=0;x<DECK;x++)
cards[x] = 0;
}
/* Generate a random value */
int rnd(int range)
{
int r;
r=rand()%range; //spit up random num.
return(r);
}
/* Seed the randomizer */
void seedrnd(void)
{
srand((unsigned)time(NULL));
}
3 replies to this topic
#1
Posted 02 June 2010 - 12:08 AM
Hello, I have a program that randomly shuffles cards. Within the switch/case statements I am trying to display 'T' as "10". But I have tried several different ways to define T and it still will only print T instead of 10. A couple of things I have tried is a printf statement after T. I have tried to define T within the case statement, but nothing seems to work. When I try to compile the program with 10 within the ' ' for case (10), it gives me a multivariable error. Below id my code and I will note the place I would like to change the variable T to 10. The program compiles fine save for the T issue. Any help would be great! Thanks!
|
|
|
#2
Posted 02 June 2010 - 01:08 AM
It's been a while since I messed around with anything, but you could always use another if statement for the printf function, where if c = A-I (i.e. 2 through 10) then it could print out not the char but a new int.
Just an idea, not sure how efficient it is.
Just an idea, not sure how efficient it is.
#3
Posted 02 June 2010 - 01:12 AM
You can't display '10' by using a single character variable but you can set it to value 10 which is a newline character.
You either use a int variable or store it in a character array.
Warrior
You either use a int variable or store it in a character array.
Warrior
#4
Posted 02 June 2010 - 02:12 AM
What the other guy said. (:
Maybe you want a function like:
Maybe you want a function like:
char* cardstr( char* dst, int card )
{
assert( 0 != dst );
assert( 0 <= card && 51 >= card );
static
const char land[4] = { 0x48, 0x44, 0x43, 0x53 };
const int value = ( card % 13 ) + 1;
char* result = dst;
*dst++ = land[13 <= card ? (int)card / 13 : 0];
switch ( value )
{
case 1:
strcpy( dst, "A\0" );
break;
case 11:
strcpy( dst, "J\0" );
break;
case 12:
strcpy( dst, "Q\0" );
break;
case 13:
strcpy( dst, "K\0" );
break;
default:
itoa( value, dst, 10 );
break;
} // switch
return result;
}
int main( int argc, char* argv[] )
{
char str[4] = {0};
for ( int i = 51; 0 <= i; --i )
printf( "%s\n", cardstr( str, i ));
return 0;
}
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









