Thread: Card Displayer

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Card Displayer

    I wrote this code that displays a card that could be used in a card game in the future.

    I got the stuff on making colors here:

    Wonderful Tutorial

    Code:
    #include <iostream.h>
    #include <cstdlib>
    #include <windows.h>
    #include <string>
    #include <ctime>
    
    #define FW  FOREGROUND_RED |\
                FOREGROUND_GREEN |\
                FOREGROUND_BLUE
    #define BW  BACKGROUND_RED |\
                BACKGROUND_GREEN |\
                BACKGROUND_BLUE |\
                BACKGROUND_INTENSITY
    #define FR  FOREGROUND_RED | \
                FOREGROUND_INTENSITY
    #define BB 0
    
    using namespace std;
    
    void Black (string cardtype);
    
    void DrawCard (string cardtype, string cardnum);
    
    string CardType ();
    
    string CardNum ();
    
    int main()
    {
    string cardtype;
    string cardnum;
    cardtype = CardType();
    cout<<"\n\n\n";
    cardnum = CardNum();
    DrawCard(cardtype, cardnum);
    cin.get();
    
    return 0;
    }
    
    void Black (string cardtype)
    {
          HANDLE hOut;
    
          hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
          SetConsoleTextAttribute(hOut,
                                FW | BB);
    
          cout << "           ";
    
          if (cardtype == "S" || cardtype == "C") {
    
          SetConsoleTextAttribute(hOut,
                                BB);
    
          SetConsoleTextAttribute(hOut,
                                BW);
          }
    
          else {
    
          SetConsoleTextAttribute(hOut,
                           FR | BW);
          }
    }
    
    void DrawCard(string cardtype, string cardnum)
    {
    Black(cardtype);
    cout << "       " << endl;
    Black(cardtype);
    cout << "       " << endl;
    Black(cardtype);
    cout << "   " << cardnum << "   " << endl;
    Black(cardtype);
    cout << "       " << endl;
    Black(cardtype);
    cout << "   " << cardtype << "   " << endl;
    Black(cardtype);
    cout << "       " << endl;
    Black(cardtype);
    cout << "       " << endl;
    }
    
    string CardType ()
    {
    int max = 4;
    int min = 1;
    int temp;
    string card;
    
    srand(time(NULL));
    temp = (rand() % (max - min + 1) + min);
    
    switch (temp) {
    case 1:
         card = "D";
         break;
    case 2:
         card = "H";
         break;
    case 3:
         card = "S";
         break;
    case 4:
         card = "C";
         break;
    }
    
    return card;
    }
    
    string CardNum ()
    {
    int max2 = 13;
    int min2 = 1;
    int temp2;
    string card2;
    
    srand(time(NULL));
    temp2 = (rand() % (max2 - min2 + 1) + min2);
    
    switch (temp2) {
    case 1:
         card2 = "A";
         break;
    case 2:
         card2 = "2";
         break;
    case 3:
         card2 = "3";
         break;
    case 4:
         card2 = "4";
         break;
    case 5:
         card2 = "5";
         break;
    case 6:
         card2 = "6";
         break;
    case 7:
         card2 = "7";
         break;
    case 8:
         card2 = "8";
         break;
    case 9:
         card2 = "9";
         break;
    case 10:
         card2 = "T";
         break;
    case 11:
         card2 = "J";
         break;
    case 12:
         card2 = "Q";
         break;
    case 13:
         card2 = "K";
         break;
    }
    
    return card2;
    }
    What do you think?
    Last edited by bumfluff; 04-13-2006 at 02:22 AM. Reason: mistake

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Anyone have an opinion on it?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It won't compile on VC++ 2003 --> <iostream.h>

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    It's very basic, I've made a few changes:
    Should show card symbols instead of H,D,C,S
    Should use 10 instead of T
    srand should only be called once. Move to main()
    Now to make this usable, you need a way to draw more than one card on a line, I'll leave that for you to do.
    Also, after every draw you should return the colors back to the default so that any text output is "normal".

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>
    #include <string>
    #include <ctime>
    
    #define FW  FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
    #define BW  BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |BACKGROUND_INTENSITY
    #define FR  FOREGROUND_RED | FOREGROUND_INTENSITY
    #define BB  0
    
    using namespace std;
    
    void Black (string cardtype);
    
    void DrawCard (string cardtype, string cardnum);
    
    string CardType ();
    
    string CardNum ();
    
    int main()
    {
        srand(time(NULL));  // This should only be called once so moved to main()
    
        string cardtype;
        string cardnum;
        cardtype = CardType();
        cout<<"\n\n\n";
        cardnum = CardNum();
        DrawCard(cardtype, "10");
        cin.get();
    
        return 0;
    }
    
    void Black (string cardtype)
    {
        HANDLE hOut;
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hOut, FW | BB);
    
        cout << "           ";
    
        if (cardtype[0]>4) 
        {
            SetConsoleTextAttribute(hOut, BB);
            SetConsoleTextAttribute(hOut, BW);
        }
    
        else 
        {
            SetConsoleTextAttribute(hOut,FR | BW);
        }
    }
    
    void DrawCard(string cardtype, string cardnum)
    {
        Black(cardtype);
        cout << "       " << endl;
        Black(cardtype);
        cout << "       " << endl;
        Black(cardtype);
        // handle the 2 digit 10
        if(cardnum == "10" ) cout << "  " << cardnum << "   " << endl;
        else cout << "   " << cardnum << "   " << endl;
        Black(cardtype);
        cout << "       " << endl;
        Black(cardtype);
        cout << "   " << cardtype << "   " << endl;
        Black(cardtype);
        cout << "       " << endl;
        Black(cardtype);
        cout << "       " << endl;
    }
    
    string CardType ()
    {
        int max = 4;
        int min = 1;
        int temp;
        string card;
    
        temp = (rand() % (max - min + 1) + min);
    
        switch (temp) 
        {
        case 1:
            card = (char)4; // Diamond
            break;
        case 2:
            card = (char)3; //Heart
            break;
        case 3:
            card = (char)6; //Spade
            break;
        case 4:
            card = (char)5; //Club
            break;
        }
    
        return card;
    }
    
    string CardNum ()
    {
        int max2 = 13;
        int min2 = 1;
        int temp2;
        string card2;
    
        srand(time(NULL));
        temp2 = (rand() % (max2 - min2 + 1) + min2);
    
        switch (temp2) 
        {
        case 1:
            card2 = "A";
            break;
        case 2:
            card2 = "2";
            break;
        case 3:
            card2 = "3";
            break;
        case 4:
            card2 = "4";
            break;
        case 5:
            card2 = "5";
            break;
        case 6:
            card2 = "6";
            break;
        case 7:
            card2 = "7";
            break;
        case 8:
            card2 = "8";
            break;
        case 9:
            card2 = "9";
            break;
        case 10:
            card2 = "10"; // Real cards show 10, not T
            break;
        case 11:
            card2 = "J";
            break;
        case 12:
            card2 = "Q";
            break;
        case 13:
            card2 = "K";
            break;
        }
    
        return card2;
    }
    Last edited by Darryl; 04-13-2006 at 10:14 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I only use T because otherwise the width of the card would be too big as 10 is two characters.

    Of course it is basic...I'm a fauking NOOB!

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by bumfluff
    I only use T because otherwise the width of the card would be too big as 10 is two characters.
    Yes, i know why you used the T, as my code shows, you need to adjust for the 10

    Quote Originally Posted by bumfluff
    Of course it is basic...I'm a fauking NOOB!
    You said you wanted to be able to use it in a future game, by me saying it's basic, that's my nice way of saying it's crap, it's not ready to be used in a card game. I gave a few suggestions to help get it to that point.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How could I draw the different symbols?

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by bumfluff
    How could I draw the different symbols?
    Did you look at the code I posted? I made all the changes I suggested except the one I said you would have to do: Ability to draw more than one card on a line

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    So it actually has symbols for a diamond etc?

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by bumfluff
    So it actually has symbols for a diamond etc?
    Yes, it's not portable but since you're using window specific code anyway it doesn't matter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  4. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  5. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM