| | |
Has to be a smarter way of doing this
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
Is there any other smarter way of going about this. This code is part of a blackjack game by the way.
C++ Syntax (Toggle Plain Text)
void DeckToCards(int Deck[]){ //Sets each element into Cards; for(int i=0;i<52;i++) { switch(Deck[i]){ case 1:{ if(Deck[i]==1 && i<14)//Clubs char Aces_C[]= " A (of clubs)"; else if(Deck[i]==1 && i>13 && i<27)//Spades char Aces_S[]= " A (of spades) "; else if(Deck[i] && i>26 && i<40)//Hearts char Aces_H[]=" A (of hearts) "; else if(i>=40)//Diamonds char Aces_D[]=" A (of diamonds) "; } case 2:{ if(Deck[i]==2 && i<14) // 2 of Clubs char 2_C[] = " 2 (of clubs"; else if(Deck[i]==2 && i>13 && i<27)// 2 of Spades char 2_S[]= " 2 ( of spades)"; else if(Deck[i]==2 && i>26 && i<40)//Hearts char 2_H[]= " 2 (of hearts)"; else if(i>=40)//Diamonds char 2_D[]=" 2 (of diamonds) } case 3: } }
Is there any other smarter way of going about this. This code is part of a blackjack game by the way.
1) The case statements need at break; statement at the end of the case to prevent other cases from being executed
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.
C++ Syntax (Toggle Plain Text)
case 1: ... break; case 2: ... break; // 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.
•
•
•
•
1) The case statements need at break; statement at the end of the case to prevent other cases from being executed
C++ Syntax (Toggle Plain Text)
case 1: ... break; case 2: ... break; // 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?
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <map> using namespace std; map<int, string> deck[4]; int main() { deck[0][1] = "Ace of Spaces"; deck[1][1] = "Ace of Clubs"; deck[2][1] = "Ace of Diamonds"; deck[3][1] = "Ace of Hearts"; string kind = deck[2][1]; cout << kind << "\n"; }
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.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
or you could make a card class, e.g.
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; class Card{ private: int cardNumber; // 1 - 52 public: Card(int _cardNumber = 1) : cardNumber(_cardNumber) {} void setCardNumber(int _cardNumber){ cardNumber = _cardNumber; } void setCard(int cardType, int suitType){ // cardType e {1..13}, suitType e {1..4} cardNumber = (cardType-1) + (suitType-1)*13 + 1; } int getCardType(){ // returns 1 - 13 return (cardNumber-1) % 13 + 1; // get card type 1-13 (ignore suit) } int getSuitType(){ // returns 1 - 4 return (cardNumber-1)/13 + 1; } int getValue(){ // returns 1-10 int valIgnoreSuit = getCardType(); if(valIgnoreSuit >= 10) return 10; return valIgnoreSuit; } int getAltValue(){ // returns 2-11 (ace can alternately be have a value of 11) int val = getValue(); if(val == 1) return 11; return val; } string getCardName(){ static const char *cardName[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Unknown"}; int val = getCardType(); return cardName[val-1]; } string getSuitName(){ static const char *suitName[] = {"Hearts", "Diamonds", "Spades", "Clubs"}; int val = getSuitType(); return suitName[val-1]; } }; int main(){ Card deck[52]; for(int i = 1; i <= 52; i++){ deck[i].setCardNumber(i); cout << deck[i].getCardName() << " of " << deck[i].getSuitName() << endl; } cin.get(); }
![]() |
Similar Threads
- school sux (Geeks' Lounge)
- How to make computer smarter in Tic Tac Toe? (C)
- HP vectra VL400 MT power supply (Troubleshooting Dead Machines)
- knoppix or else (Getting Started and Choosing a Distro)
- You know you're getting older (Geeks' Lounge)
- What is BRIDGE.DLL (Viruses, Spyware and other Nasties)
- Can Alice come out and play? (Geeks' Lounge)
- MSN Problems.. (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Extremely new to c++ programming, any programs that can give me a tutorial how to c++
- Next Thread: Need help with Tic Tac Toe
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






