0

Im trying to shuffle my deck of cards using pointers. My loops just print out the default value, not cycling through the cards. how do i fix this. shuffle functiuon is at the bottom? I am trying to loop through and increment pointers while replacing my destination pointer with my source pointer. or the dereferenced value. what am i doing wrong?

#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;

//global constant(s)
const int maxCards = 52;

//Structs
struct card 
{
    char suit[8];
    char rank[6];
    int cvalue;
    char location;
};

struct player 
{
    char name[100];
    int total;
};

//Function List
char * strcopy(char destination[], const char source[]);
void shuffle(card* destinationP,card* sourceP);

//program
int main()
{

//begin seeding the time for randomize later
srand(time(NULL));

//declaration of variables
bool gameOn =true;
int choice;
char tempfName[100];
char templName[100];
int count =0;

//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];

//create pointer and set initial value
card * deckPointer = NULL;
card * shuffledDeckPointer= NULL;
player *peoplePointer = NULL;

//
deckPointer = &deck[0]; //assign address of deck to deckPointer
shuffledDeckPointer = &shuffledDeck[0]; 
peoplePointer = &people[0]; //assign address of people to peoplePointer

//sets default values for the card arrays
for(int i=0;i<52;i++) 
{
    strcopy(shuffledDeck[i].suit, "suit");
    strcopy(shuffledDeck[i].rank,"rank");
    shuffledDeck[i].cvalue = 0;
strcopy(deck[i].suit,"suit");
    strcopy(deck[i].rank,"rank");
    deck[i].cvalue = 0;
}

//set up card file to be read in
ifstream fin;
char finName[12];

//get file name from user
cout << "Enter file name...(cardFile.txt)" << endl;;
cin >> finName;


//open the file
fin.open(finName);


//check if cardFile.txt opens correctly
if(!fin.good())
{
    cout << "Error with card file" << endl;
    return 0;
}
else
{
    card *deckPointer = NULL;

    //prime fin
    //fin >> deck[i].suit;
    //fin >> deck[i].rank;
    //fin >> deck[i].cvalue;

    while(fin.good())
    {
    for(deckPointer = &deck[0]; deckPointer < &deck[maxCards];deckPointer++)
        {
            fin >> (*deckPointer).suit;
            fin >> (*deckPointer).rank;
            fin >> (*deckPointer).cvalue;
        }   
    }    
}            
cin.clear();

}

                //Functions

//copy string function
char * strcopy(char *destination, const char* source)
{
char *p = destination;
while(*p++ = *source++);
return destination;
}





//Shuffle function
void shuffle(card *destinationP,card *sourceP)
{


int randomNumber = 0;
int count=0;


for(int j=0;j<52;j++)
{

//choose a random number up to 52
randomNumber = rand()%52;
count = 0;
     if(count < randomNumber)
     {
     while(count < randomNumber)
         {
         *sourceP++;
          count++;
         }
           //check if destination is empty ie will accept
          //a card and will   not overwrite a card
         if((*destinationP).cvalue == 0)
         {
        // copy unshuffled "deck" to the shuffled "deck"


          strcopy((*destinationP).suit, (*sourceP).suit);
         strcopy((*destinationP).rank, (*sourceP).rank);
         (*destinationP).cvalue = (*sourceP).cvalue;
     *destinationP++;       
         }
      }
else
{
 if((*destinationP).cvalue == 0)
              {
                 // copy unshuffled "deck" to the shuffled "deck"


                   strcopy((*destinationP).suit, (*sourceP).suit);
                   strcopy((*destinationP).rank, (*sourceP).rank);
                   (*destinationP).cvalue = (*sourceP).cvalue;
               *destinationP++;     
               }  
  }}}
matt
  • 79
  • 2
  • 13
  • 1
    1) Encapsulation. Bury implementation when you can; for example, instead of laboriously copying the fields of one card to another in several places, write an assignment operator so that nothing outside `card` has to worry about these details. 2) Develop new functionality in isolation. Don't try to shuffle *cards*, try to shuffle *ints*. When the shuffling works perfectly, *then* shuffle cards. 3) Learn to prepare a [minimal complete example](http://stackoverflow.com/help/mcve). It will help you find the bug, and help us find the bug if you can't. – Beta Sep 18 '14 at 01:05

2 Answers2

6

Just use an std::vector and std::shuffle. Also don't use rand(), use the <random> header instead. And std::string instead of char*.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • 1
    +1, If you're using C++ you should use the STL too. There are usually containers and methods that will do what you want faster and safer than can be manually coded. – Serdalis Sep 18 '14 at 01:01
  • 1
    @Serdalis [It's not _STL_, but the c++ standard library.](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571) – πάντα ῥεῖ Sep 18 '14 at 01:18
  • @πάνταῥεῖ Well well, I did not know that. Learn something new every day, Thanks! – Serdalis Sep 18 '14 at 01:24
  • i cant use those functions, hence why the code is much more complicated than necessary – matt Sep 18 '14 at 01:52
  • @mattmowris In this case, get a debugger and debug your code. No one here will do that for you and you have to learn it anyways. – Baum mit Augen Sep 18 '14 at 09:18
0

I prefer the Fisher-Yeats shuffle which Donald Knuth put into his Art of Computer Programming as Algorithm P. Pseudo code can be found at: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle. Incidentally, for better randomization properties, I would recommend using a Mersenne Twister (http://en.wikipedia.org/wiki/Mersenne_Twister) for which you can find GPLd source in several languages