Arrays

If I had two arrays:
1
2
string sASuits[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
int iARanks[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

1 through 13 representing the Ace, 2 - 10, Jack, Queen, and King.
How could I create the arrays so that all 52 cards combinations are represented.
Last edited on
Here would be how i would do it. this should represent all 52 cards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct suit
{ 
       string name;
       int sARanks[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};     

};

int main()
{
      string sASuits[] = {"Clubs", "Diamonds", "Hearts", "Spades"}
      suit deck [4];
      for (int i; i < 4; i++)
      {
            deck[i].name = sASuits[i];
       
      }

      for (int i; i < 4; i++)
      {
            cout << deck[i].name << endl;
           for (int j; j < 13; j++)
           {
             cout << deck[i].sARanks[j] << endl;
            }
       }

 
}



i wrote this code in the forums instead of in code gui so sorry for the spacing issues. and i am not sure if this will compile i just did quick check for errors.
Last edited on
I see what you're doing.
Could there be a way put the results in one array so I could split it?
Last edited on
That looks like a full solution to me, and we don't like full solutions on these forums... it's nice that you did the work and all, however...

@Da0omph: You could create a string array and read into that using ostringstream. I might be over-thinking the solution but that's how I'd do it.
http://cplusplus.com/reference/iostream/ostringstream/str/

-Albatross
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

const int number_of_cards = 52;

void shuffle(int list[], int size) {
	srand(time(0));
	for (int i = 0; i < size; i++) {
		int index = rand() % number_of_cards;
		int temp = list[i];
		list[i] = list[index];
		list[index] = temp;
	}
	
}

int main() {
	using namespace std;
	
	int deck[number_of_cards];
	string suits[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
	string ranks[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack" "Queen", "King"};
	//int value[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
	
	//for(int i = 0; i < number_of_cards; i++)
		//deck[i] = i;
	//shuffle(deck, number_of_cards);
	
	for (int x = 0; x < 13; x++) {
		for (int i = 0; i < 4; i++) {
			cout << ranks[x] << " of " << suits[i] << endl;
		}
		cout << endl;
	}
	return 0;
}

Full solution or not, I'm still crazily confused. I just need a starting point.
I need someway to split results, to resemble two hands.
Last edited on
Why not use a struct to define a card and then just have the deck be an array of cards. You can create a deck array and an array for each hand. You can certainly change std::deque to c-arrays if you want to but I prefer using a std sequence container (dynamic array) myself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct card
{
    std::string suit;
    unsigned int value; // or you can do a std::string here as well if you prefer
};

// a deck is an array of 52 cards.  You'll have to figure out how to build it yourself
std::deque<card> theDeck(52);  // if you want to use vector, the dynamic array

// make a loop or something to insert each card into the deck.   Write your own loops or research
// functors and for_each

// a hand is however many cards you wish
std::deque<card> hand1(7); 
std::deque<card> hand2(7); 
std::deque<card> hand3(7); 
std::deque<card> hand4(7); 

//If you want to shuffle the deck, do this.
std::random_shuffle(theDeck.begin(), theDeck.end());


Now that you have a randomly shuffled deck it shouldn't take too much to figure out how to deal 7 cards into each hand. If you wanted to truly represent a game then you have to learn how to "move" cards from the deck to the hand and back. A card cannot be in the deck and in someone's hand simultaneously. You might also need another array of cards representing a discard pile depending on the game. deque has member functions such as front() and pop_front for accessing or deleting the first element.
http://cplusplus.com/reference/stl/deque/


Last edited on
Topic archived. No new replies allowed.