Has to be a smarter way of doing this

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Has to be a smarter way of doing this

 
0
  #1
Dec 10th, 2008
This code Associates each number with cards, so 1 = ace of hearts/spades/diamonds/clubs, 2 = 2 of clubs/spades..... and so on so here is the code
  1.  
  2. void DeckToCards(int Deck[]){
  3.  
  4. //Sets each element into Cards;
  5. for(int i=0;i<52;i++)
  6. {
  7. switch(Deck[i]){
  8.  
  9. case 1:{ if(Deck[i]==1 && i<14)//Clubs
  10. char Aces_C[]= " A (of clubs)";
  11. else if(Deck[i]==1 && i>13 && i<27)//Spades
  12. char Aces_S[]= " A (of spades) ";
  13. else if(Deck[i] && i>26 && i<40)//Hearts
  14. char Aces_H[]=" A (of hearts) ";
  15. else if(i>=40)//Diamonds
  16. char Aces_D[]=" A (of diamonds) ";
  17. }
  18.  
  19. case 2:{ if(Deck[i]==2 && i<14) // 2 of Clubs
  20. char 2_C[] = " 2 (of clubs";
  21. else if(Deck[i]==2 && i>13 && i<27)// 2 of Spades
  22. char 2_S[]= " 2 ( of spades)";
  23. else if(Deck[i]==2 && i>26 && i<40)//Hearts
  24. char 2_H[]= " 2 (of hearts)";
  25. else if(i>=40)//Diamonds
  26. char 2_D[]=" 2 (of diamonds)
  27.  
  28. }
  29. case 3:
  30.  
  31. }
  32. }
  33.  

Is there any other smarter way of going about this. This code is part of a blackjack game by the way.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Has to be a smarter way of doing this

 
0
  #2
Dec 10th, 2008
1) The case statements need at break; statement at the end of the case to prevent other cases from being executed
  1. case 1:
  2. ...
  3. break;
  4. case 2:
  5. ...
  6. break;
  7. // etc

2) You don't need to check the value of Deck[i] within the case statements because that is already done in the switch statement.

3) You can't declare character arrays within if statements like that because they will disappear (go out of scope) as soon as the if statement terminates. Declare character arrays outside the switch statement so that they are in scope for the entire function.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,107
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 142
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: Has to be a smarter way of doing this

 
0
  #3
Dec 10th, 2008
Originally Posted by Ancient Dragon View Post
1) The case statements need at break; statement at the end of the case to prevent other cases from being executed
  1. case 1:
  2. ...
  3. break;
  4. case 2:
  5. ...
  6. break;
  7. // etc

2) You don't need to check the value of Deck[i] within the case statements because that is already done in the switch statement.

3) You can't declare character arrays within if statements like that because they will disappear (go out of scope) as soon as the if statement terminates. Declare character arrays outside the switch statement so that they are in scope for the entire function.

Thanks I get that but, is there another way this can be executed, without doing this long and tedious algorithm?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Has to be a smarter way of doing this

 
0
  #4
Dec 10th, 2008
you could use <map> to map the loop counter i with a string. A simple example, uses an array of maps to represent the 4 kinds of cards.

  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. using namespace std;
  5.  
  6. map<int, string> deck[4];
  7. int main()
  8. {
  9. deck[0][1] = "Ace of Spaces";
  10. deck[1][1] = "Ace of Clubs";
  11. deck[2][1] = "Ace of Diamonds";
  12. deck[3][1] = "Ace of Hearts";
  13.  
  14. string kind = deck[2][1];
  15. cout << kind << "\n";
  16. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Has to be a smarter way of doing this

 
0
  #5
Dec 10th, 2008
or you could make a card class, e.g.
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Card{
  5. private:
  6. int cardNumber; // 1 - 52
  7.  
  8. public:
  9. Card(int _cardNumber = 1) : cardNumber(_cardNumber) {}
  10.  
  11. void setCardNumber(int _cardNumber){
  12. cardNumber = _cardNumber;
  13. }
  14.  
  15. void setCard(int cardType, int suitType){ // cardType e {1..13}, suitType e {1..4}
  16. cardNumber = (cardType-1) + (suitType-1)*13 + 1;
  17. }
  18.  
  19. int getCardType(){ // returns 1 - 13
  20. return (cardNumber-1) % 13 + 1; // get card type 1-13 (ignore suit)
  21. }
  22.  
  23. int getSuitType(){ // returns 1 - 4
  24. return (cardNumber-1)/13 + 1;
  25. }
  26.  
  27. int getValue(){ // returns 1-10
  28. int valIgnoreSuit = getCardType();
  29.  
  30. if(valIgnoreSuit >= 10)
  31. return 10;
  32.  
  33. return valIgnoreSuit;
  34. }
  35.  
  36. int getAltValue(){ // returns 2-11 (ace can alternately be have a value of 11)
  37. int val = getValue();
  38.  
  39. if(val == 1)
  40. return 11;
  41.  
  42. return val;
  43. }
  44.  
  45. string getCardName(){
  46. static const char *cardName[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Unknown"};
  47. int val = getCardType();
  48.  
  49. return cardName[val-1];
  50. }
  51.  
  52. string getSuitName(){
  53. static const char *suitName[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
  54. int val = getSuitType();
  55.  
  56. return suitName[val-1];
  57. }
  58. };
  59.  
  60. int main(){
  61. Card deck[52];
  62.  
  63. for(int i = 1; i <= 52; i++){
  64. deck[i].setCardNumber(i);
  65. cout << deck[i].getCardName() << " of " << deck[i].getSuitName() << endl;
  66. }
  67. cin.get();
  68. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC