-
Optimization of code
Hey I got a question about optimizing a chunk of code, I already tried to make it smaller, but now I am out of ideas for a moment and I thought I'd see how others might do this. So all I want to do is when I pass a function a number I want it to give it a suit and the number as a word, (e.g. 4 => four).
Here's the code:
Code:
void show_card(int num)
{
char suit[100];
char number[100];
//char *display;
if(num < 13)
{
strcpy(suit,"of Spades");
}
else if(num >= 13 && num < 26)
{
strcpy(suit,"of Clubs");
}
else if(num >= 26 && num < 39)
{
strcpy(suit, "of Diamonds");
}
else
{
strcpy(suit,"of Hearts");
}
if(num == 0 || num == 13 || num == 26 || num == 39)
{
strcpy(number, "Two ");
}
else if(num == 1 || num == 14 || num == 27 || num == 40)
{
strcpy(number, "Three ");
}
else if(num == 2 || num == 15 || num == 28 || num == 41)
{
strcpy(number, "Four ");
}
else if(num == 3 || num == 16 || num == 29 || num == 42)
{
strcpy(number, "Five ");
}
else if(num == 4 || num == 17 || num == 30 || num == 43)
{
strcpy(number, "Six ");
}
else if(num == 5 || num == 18 || num == 31 || num == 44)
{
strcpy(number, "Seven ");
}
else if(num == 6 || num == 19 || num == 32 || num == 45)
{
strcpy(number, "Eight ");
}
else if(num == 7 || num == 20 || num == 33 || num == 46)
{
strcpy(number, "Nine ");
}
else if(num == 8 || num == 21 || num == 34 || num == 47)
{
strcpy(number, "Ten ");
}
else if(num == 9 || num == 22 || num == 35 || num == 48)
{ strcpy(number, "Jack ");
}
else if(num == 10 || num == 23 || num == 36 || num == 49)
{
strcpy(number, "Queen ");
}
else if(num == 11 || num == 24 || num == 37 || num == 50)
{
strcpy(number, "King ");
}
else if(num == 12 || num == 25 || num == 38 || num == 51)
{
strcpy(number, "Ace ");
}
cout << " " << number << suit << " ";
}
my apologies for how big it is, but if anyone can help with any tricks or what have you it'd be great, thanks!
:)
-
Your code is very neat as it is.
Maybe try using a switch?
-
Code:
char suits[][15] = { "of Spades", "of Clubs", "of Diamonds", "of Hearts" };
char numbers[][15] = { "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Jack ", "Queen ", "King ", "Ace " };
void show_card(int num)
{
char suit[100];
char number[100];
strcpy(suit, suits[num / 13]);
strcpy(number, numbers[num % 13]);
cout << " " << number << suit << " ";
}
-
could be much neater.
try something like this:
Code:
char *suits[] =
{
"Spades",
"Hearts",
"Diamonds",
"Clubs"
};
char *cards[] =
{
"Ace",
"Two",
"Three",
"Four",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"
};
then something like
Code:
char card[40];
sprintf (card, "%s of %s ", cards[num % 13], suits[num / 13]);
which will produce the same results you're getting now (if you rearrange the suit list to match yours).
-
Excellet! Thanks a bunch, its always good to get new interpretations of code, and different ways of looking at it. That'll help a lot! :)
-
ummm..... i just realized, before you use mine, add in the "Five" that i forgotor :D