Reputation: 548
I'm not sure if my title is worded properly so I will elaborate here.
What I need to do is have my vector take the information from a previously built Array, shuffle it, then spit it back out. What I figured I could do because I have a for loop in place to display the information of the deck is have the vector within the main function, however at the same time I have a feeling I need to create a separate function. However, I'm not sure what method would be most efficient. What I have thus far is this.
#include <iostream>
#include <string>
#include "prototypes.h"
using std::string;
using std::endl;
using std::cout;
const string CARDS[] =
{
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};
unsigned const DECK_SIZE = sizeof(CARDS) / sizeof(string);
int const SHUFFLE_COMMAND = 1;
int const SHOW_COMMAND = 2;
int const QUIT_COMMAND = 3;
const char MENU[] = "\nShowing Cards Program:\n1 -- Shuffle Cards\n2 -- Show Deck\n3 -- Quit\n: ";
const char IM_OUTTA_HERE[] = "Buh-bye!";
int main()
{
int command = 0;
do
{
command = askForInt(MENU, QUIT_COMMAND, SHUFFLE_COMMAND);
switch(command)
{
case SHUFFLE_COMMAND:
break;
case SHOW_COMMAND:
for(int i = 0; i < DECK_SIZE; i++)
{
cout << CARDS[i] << " ";
if(i == 12 || i == 25 || i == 38)
{
cout << endl;
}
}
break;
case QUIT_COMMAND:
break;
}
}while(command != QUIT_COMMAND);
cout << IM_OUTTA_HERE << endl;
return 0;
}
askForInt is in another file called Functions, all it does is receive the users input (1, 2 or 3)
EDIT: It also must be noted that I need to do this using pointers, as this is what I'm currently trying to learn.
Upvotes: 1
Views: 190
Reputation: 66234
Since you added the restriction this must all be done with pointers, that changes things considerably. I wrote the code below to be compatible back to C++98. Hopefully get something out of it.
Note: this is just the deck randomization portion of your goal. the rest is up to you, but has been stripped from the sample below.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const string CARDS[] =
{
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};
unsigned const DECK_SIZE = sizeof(CARDS) / sizeof(string);
int main()
{
// seed rng
srand((unsigned)time(nullptr));
// create a vector of const string ptrs. using pointer enumeration through the
// array, add the address of each string object to our deck.
vector<const string*> deck;
for (const string* p = CARDS; p != (CARDS + DECK_SIZE); ++p)
deck.push_back(p);
// shuffle the pointer vector
random_shuffle(deck.begin(), deck.end());
// display each deck through dereferencing.
for (vector<const string*>::const_iterator it=deck.cbegin(); it!= deck.cend(); ++it)
cout << (*it)->c_str() << ' ';
cout << endl;
return 0;
}
Output
4H TS 9S TH JC AH QS 5S 8S 2D QH 9C 2C 8C 2S JS 9H JH KC 7S 4D 9D KS QC 4S AS 3S 5C KH 7D KD 3D 8D TD 3H 2H 5D JD 8H AC 4C 6S 7C 3C 6H 5H TC 6C QD 6D 7H AD
If copying is a viable option, then the following, which requires C++11 or later compliance, makes that job trivial:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <ctime>
using namespace std;
const string CARDS[] =
{
"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD",
"2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH",
"2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
};
int main()
{
// seed rng
srand((unsigned)time(nullptr));
vector<string> deck(begin(CARDS), end(CARDS));
random_shuffle(deck.begin(), deck.end());
copy(deck.begin(), deck.end(), ostream_iterator<string>(cout," "));
cout << endl;
};
Output
3S 9C 4C 4S 3H 8D TC 7H TD 5S 3D QC QD 5H TS 4H 3C 7C 9D 6C TH 6S 9S 7S KS KC JC 7D JH 5C 8C 2H JD 9H QS AD JS 2C 4D 6D 8H 5D AS KD 2S 8S AH 6H AC QH 2D KH
Upvotes: 1
Reputation: 15870
This must be an assignment as 3 other people seem to be doing the same thing.
You don't need your card listing to be a std::string[]
. You can use std::array
which will give you the standard STL iterator methods while still being a simple array. Either way, you can load them into a vector using the std::vector
's iterator constructor:
// assuming data is loading into myarray and myarray_size is the size of the array
std::vector<std::string> cards(myarray, myarray + myarray_size);
You can access the vector's data directly (if needed) by using &cards[0]
which will give you a pointer to the first element in the array.
That said, if you use the STL templates, you will improve your code and learn the language much more efficiently.
Upvotes: 1