11 Replies - 784 Views - Last Post: 19 April 2014 - 04:31 PM Rate Topic: -----

#1 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

pass value from object to an array.

Posted 16 April 2014 - 07:17 PM

I am trying to write a poker program. I created a class to create a deck of cards..and then shuffle the deck..Now I am trying to pass value from the deck to an array to deal a hand..so 5 cards to player1[] array.. I tried doing it directly such as.
	for (int j = 0; j < 5; j++) {  
     getCard=deck[j].toString();
         }

but I would get error such as [Error] no match for 'operator=' in 'player1[j] = deck[j].Card::toString()'

So then I tried using a pointer..

 char *getCard;
 getCard = new char;

	for (int j = 0; j < 5; j++) {  
     getCard=deck[j].toString();
         }



but I would get this error [Error] void value not ignored as it ought to be

So how can I pass a value from the object deck..to an array? or should I be doing it another way?..I tried to think of a way to do it via a function but really got hung up there..

here is full code
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;

const char* FaceString[13] = {"Ace", "2", "3", "4",
                              "5", "6", "7", "8",
                              "9", "10", "Jack", "Queen", "King"};

const char* SuitString[4] = {"Spades", "Hearts", "Diamonds", "Clubs"};
string player1[5];

class Card
{
public:
    int face;
    int suit;

    void toString() {
        cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
    }

};

int main()
{    char *getCard;
	getCard = new char;
	
    // Create a deck of cards
    Card deck[52];

    for (int i = 0; i < 52; i++) {    
        deck[i].face = i % 13;
        deck[i].suit = i % 4;    
    }

    // Shuffle
    srand (unsigned(time(0)));
    random_shuffle(&deck[0], (&deck[0]+52));   

    // Display cards
    for (int j = 0; j < 52; j++) {  
        cout<< "Card_" << (j+1) << ": ";
        deck[j].toString();
    }

	for (int j = 0; j < 5; j++) {  
     getCard=deck[j].toString();
         }



    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: pass value from object to an array.

#2 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 984
  • Joined: 18-April 09

Re: pass value from object to an array.

Posted 17 April 2014 - 02:36 AM

Hi,

What I understand is that you wan't to get a random 5 cards from the 52 cards and give it to player 1 right ? So that player 1 now has 5 cards in his deck. I would do something like this:

because we are using C string FaceString SuitString so I would declare also player1[5] as so.

char* player1[5];


This is used to store the 5 cards of player 1. Then, I would create another function in the class say, toPlayer() which returns a player card from the 52 card deck. something like this:

char* toPlayer()
    {

        strcat(player1Card,FaceString[face]); // look up the strcat() function
        strcat(player1Card," of ");
        strcat(player1Card,SuitString[suit]);

        return player1Card; // make sure to declare this. this returns the 5 player cards
    }


Look up the strcat() function and make sure you declare player1Card:

char player1Card[100] = {0};


Now you can assign those cards to the player:

cout << "Player 1 deck contains:" << endl;
      for (int j = 0; j < 5; j++) 
        {
           player1[j]=deck[j].toPlayer(); // assign 5 cards to player 1.
           cout << player1[j] << endl;
        }


May not be the best way, but you get the idea.

This post has been edited by tarmizi_adam2005: 17 April 2014 - 02:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 17 April 2014 - 05:12 AM

View Posttarmizi_adam2005, on 17 April 2014 - 02:36 AM, said:

Hi,

What I understand is that you wan't to get a random 5 cards from the 52 cards and give it to player 1 right ? So that player 1 now has 5 cards in his deck. I would do something like this:

because we are using C string FaceString SuitString so I would declare also player1[5] as so.

char* player1[5];


This is used to store the 5 cards of player 1. Then, I would create another function in the class say, toPlayer() which returns a player card from the 52 card deck. something like this:

char* toPlayer()
    {

        strcat(player1Card,FaceString[face]); // look up the strcat() function
        strcat(player1Card," of ");
        strcat(player1Card,SuitString[suit]);

        return player1Card; // make sure to declare this. this returns the 5 player cards
    }


Look up the strcat() function and make sure you declare player1Card:

char player1Card[100] = {0};


Now you can assign those cards to the player:

cout << "Player 1 deck contains:" << endl;
      for (int j = 0; j < 5; j++) 
        {
           player1[j]=deck[j].toPlayer(); // assign 5 cards to player 1.
           cout << player1[j] << endl;
        }


May not be the best way, but you get the idea.



Wow thinks..but I have a question....What is strcat?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: pass value from object to an array.

Posted 17 April 2014 - 06:01 AM

If you followed the link, for that function, it would have shown you what that function does -- append a C string to another C string.

Unfortunately, although the approach is generally correct, the actual implementation given was poorly executed. The major issue is that some OO principles are broken including encapsulation and loose coupling. The Card class now requires that a global player1card character buffer be created, and it is forever tied to the player1card buffer. What happens when you add another player? How do you give him the cards?

The correct approach is to do the following:
Make toString() return a C++ string
You are writing C++ code. Take full advantage of it. So your toString() should look like:
std::string toString()
{
    string cardInfo;
    cardInfo = FaceString[face];
    cardInfo += " of ";
    cardInfo += SuitString[suit];
    return cardInfo;
}


This works better because the string is created on the fly and as needed. The class is now not tightly coupled to that global variable player1card. (You are still tightly coupled to the FaceString and SuitString arrays, though. That can be remedied by declaring them within the class or function.)

Make a Player class that holds cards and know how to print them
class Player
{
private:
    std::vector<Card> cards;
    // If you don't know vector's you should.
    // If you aren't allowed to use them, use an array and a count instead.

public:
    void AddCard(const Card & card)
    {
        // your code to add to cards vector or array
    }

    void PrintCards() const
    {
        // your code to iterate over the vector or array to print them
    }
};



Why go through the trouble of making a Player class? It is so that you can simply add new players by having a vector or array of players and you'll be good to go.
Was This Post Helpful? 1
  • +
  • -

#5 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 17 April 2014 - 07:20 AM

You are writing C++ code. Take full advantage of it. So your toString() should look like:
std::string toString()
{
    string cardInfo;
    cardInfo = FaceString[face];
    cardInfo += " of ";
    cardInfo += SuitString[suit];
    return cardInfo;
}


This works better because the string is created on the fly and as needed. The class is now not tightly coupled to that global variable player1card. (You are still tightly coupled to the FaceString and SuitString arrays, though. That can be remedied by declaring them within the class or function.)


Are you saying replace the void toString function with the code above.

void toString() {
cout<<FaceString[face]<<" of "<<SuitString[suit]<<endl;
}
Was This Post Helpful? 0
  • +
  • -

#6 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 17 April 2014 - 07:42 AM

View Postdariusd7, on 17 April 2014 - 05:12 AM, said:

View Posttarmizi_adam2005, on 17 April 2014 - 02:36 AM, said:

Hi,

What I understand is that you wan't to get a random 5 cards from the 52 cards and give it to player 1 right ? So that player 1 now has 5 cards in his deck. I would do something like this:

because we are using C string FaceString SuitString so I would declare also player1[5] as so.

char* player1[5];


This is used to store the 5 cards of player 1. Then, I would create another function in the class say, toPlayer() which returns a player card from the 52 card deck. something like this:

char* toPlayer()
    {

        strcat(player1Card,FaceString[face]); // look up the strcat() function
        strcat(player1Card," of ");
        strcat(player1Card,SuitString[suit]);

        return player1Card; // make sure to declare this. this returns the 5 player cards
    }


Look up the strcat() function and make sure you declare player1Card:

char player1Card[100] = {0};


Now you can assign those cards to the player:

cout << "Player 1 deck contains:" << endl;
      for (int j = 0; j < 5; j++) 
        {
           player1[j]=deck[j].toPlayer(); // assign 5 cards to player 1.
           cout << player1[j] << endl;
        }


May not be the best way, but you get the idea.



Wow thinks..but I have a question....What is strcat?

Ok I looked it up... I changed the code to what you suggested..but am getting the following output for the player1 printout see below.


Player 1 deck contains:
ⁿ( n of─├╠o╕ⁿ(
ⁿ( Spa─├╠
ⁿ( Hea─├╠
ⁿ( Hea─├╠
ⁿ( of D─├╠o╕ⁿ(

--------------------------------
Process exited with return value 0
Press any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#7 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 984
  • Joined: 18-April 09

Re: pass value from object to an array.

Posted 17 April 2014 - 07:52 AM

I think you should follow Skydiver's suggestions instead of mine :)
Was This Post Helpful? 0
  • +
  • -

#8 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 17 April 2014 - 08:03 AM

View Posttarmizi_adam2005, on 17 April 2014 - 07:52 AM, said:

I think you should follow Skydiver's suggestions instead of mine :)/>


I'm trying but they have not taught us vectors in class yet.. I'm trying to read and look at examples, but it's kind of confusing..Your way was clearer to me..just the results were well slightly off target.. i been working on this for days..and cant get past this one obstalce of passing value of cards into an array to represent a dealt hand.. Why is this such a hard thing to do.. Seems it should be easier..
Was This Post Helpful? 0
  • +
  • -

#9 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 287
  • View blog
  • Posts: 984
  • Joined: 18-April 09

Re: pass value from object to an array.

Posted 17 April 2014 - 08:15 AM

Player 1 deck contains:
ⁿ( n of─├╠o╕ⁿ(
ⁿ( Spa─├╠
ⁿ( Hea─├╠
ⁿ( Hea─├╠
ⁿ( of D─├╠o╕ⁿ(


Maybe you forgot to initialize this:

char player1Card[100] = {0};


in your Card class. This will be used for the strcat() function. My example was not a good design as suggested in post #4. So, you can learn vectors and follow suggestions in post #4. Good luck
Was This Post Helpful? 0
  • +
  • -

#10 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 17 April 2014 - 08:19 AM

Make a Player class that holds cards and know how to print them
class Player
{
private:
    std::vector<Card> cards;
    // If you don't know vector's you should.
    // If you aren't allowed to use them, use an array and a count instead.

public:
    void AddCard(const Card & card)
    {
        // your code to add to cards vector or array
    }

    void PrintCards() const
    {
        // your code to iterate over the vector or array to print them
    }
};



Why go through the trouble of making a Player class? It is so that you can simply add new players by having a vector or array of players and you'll be good to go.
[/quote]

This is what i cam up with for the player class..am I on the right track?
class Player
{

private:

    Card cards[5];

public:

    void AddCard(const Card & card)
    {
			for(i=1; i<=5; i++){
				cards[i].toString();				
			}
        // your code to add to cards vector or array
    }

    void PrintCards() const

    {
			for(i=1; i<=5; i++){
				cout<<cards[i].toString();				
			}
        // your code to iterate over the vector or array to print them
    }
};


This post has been edited by dariusd7: 17 April 2014 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: pass value from object to an array.

Posted 17 April 2014 - 03:44 PM

Remember that in C and C++ arrays start at zero.

You'll want your to keep track of how many cards are in the hand. So in pseudo code, some like:
class Player
{
private:
    Card cards[5];
    int  cardCount;

public:
    Player()
    {
        set cardCount to zero
    }

    void AddCard(newCard)
    {
        if cardCount == 5
            throw an error

        cards[cardCount] = newCard
        increment cardCount
    }

    void Print()
    {
        for(int i = 0; i < cardCount; i++)
            print out cards[i].toString()
    }
};


Was This Post Helpful? 0
  • +
  • -

#12 dariusd7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-October 13

Re: pass value from object to an array.

Posted 19 April 2014 - 04:31 PM

View PostSkydiver, on 17 April 2014 - 03:44 PM, said:

Remember that in C and C++ arrays start at zero.

You'll want your to keep track of how many cards are in the hand. So in pseudo code, some like:
class Player
{
private:
    Card cards[5];
    int  cardCount;

public:
    Player()
    {
        set cardCount to zero
    }

    void AddCard(newCard)
    {
        if cardCount == 5
            throw an error

        cards[cardCount] = newCard
        increment cardCount
    }

    void Print()
    {
        for(int i = 0; i < cardCount; i++)
            print out cards[i].toString()
    }
};


Thanks...skydiver
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1