Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,063 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,587 people online right now. Registration is fast and FREE... Join Now!




bridge program

 
Reply to this topicStart new topic

bridge program, shuffle, deal, and sort bridge( cards) hands

mrkwlortie
13 Apr, 2008 - 08:32 AM
Post #1

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 11

I have a start to my program but I am struggling with several things. I need to write the setcard() logic to reveal cards in my output rather than error messages. I tried to cout << rankNames[rnk] but it did not work. I also need to think about logic for sorting and shuffling. As always, thanks a lot.

CODE

//
#include <iostream>
#include <string>

using namespace std;

string rankNames[] = { "Error", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
                          "Nine", "Ten", "Jack", "Queen", "King" };
string suitNames[] = { "Error", "Diamonds", "Clubs", "Hearts", "Spades"};

class Card
{    
        private:
        int rank;
        int suit;
        public:
        // Constructor for a Card
        Card(int rk, int st)
        {
                rank = rk;
                suit = st;
        }
      
        // A default Constructor is needed to declare an array of Cards
        Card()
        {
                rank = 0;
                suit = 0;
        }
      
        // A string that represents the card
        string toString()
        {
                return rankNames[rank] + " of " + suitNames[suit];
        }
      
        // Compare two cards.  Write this.
        bool greater(Card other) { }

        // Write this
        void setCard(int st, int rnk)
       {
          
       }
};

// Routine to write a set of cards
void printCards(Card deck[], int size)
{
        for (int i = 0; i < size; i++)
                cout << deck[i].toString() << endl;
}

// Write a routine to jumble an array of cards
void shuffle(Card deck[], int size) { }

// Write a routine to sort an array of cards
void sort(Card deck[], int size) { }

int main()
{
        Card deck[52];                // Creates 52 cards using default constructor
        // Create a regulation deck of cards
        int pos = 0;
        for (int suit = 1; suit <= 4; suit++)
                for (int rank = 1; rank <= 13; rank++, pos++)
                        deck[pos].setCard(suit, rank);

        // Has the deck been created?
        cout << "\nInitial deck\n";
        printCards(deck, 52);

        shuffle(deck, 52);
        cout << "\nShuffled deck\n";
        printCards(deck, 52);

        // Deal a hand
        // This is not regulation dealing.  Rather than getting the
        // first 13 cards, I should get every fourth card.
        Card hand[13];
        for (pos = 0; pos < 13; pos++)
                hand[pos] = deck[pos];

        // Display my hand
        cout << "\nMy hand as dealt\n";
        printCards(hand, 13);
      
        // Organize my hand
        sort(hand, 13);
        cout << "\nMy Hand after sorting\n";
        printCards(hand, 13);
        
        return 0;
}


*Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

mrkwlortie
RE: Bridge Program
13 Apr, 2008 - 12:27 PM
Post #2

New D.I.C Head
*

Joined: 10 Mar, 2008
Posts: 11

My bridge program isn't getting any replies so i thought id ask a simpler question

I have an array deck[52] which holds the values of a deck of cards. I need to set the value of each element of the array to a suit and rank using my setCard() function.

CODE

int pos = 0;
        for (int suit = 1; suit <= 4; suit++)
                for (int rank = 1; rank <= 13; rank++, pos++)
                        deck[pos].setCard(suit, rank);


this is my loop that takes deck[pos] and is supposed to use the setCard function to store the values of the cards. What do i need to do in my setCard function to set the values of the elements of the array ? Thanks
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Bridge Program
14 Apr, 2008 - 06:18 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
merged:

well basically it looks like you need to go something like:
CODE
        setCard(int st, int rk)
        {
                rank = rk;
                suit = st;
        }


the array contains a set of cards, when you go deck[i] you are accessing a particular card object, therefore you will be setting the values for just that card. The array does not complicate things at all.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month