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++;     
               }  
  }}}
1
  • 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. It will help you find the bug, and help us find the bug if you can't. Commented Sep 18, 2014 at 1:05

2 Answers 2

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*.

Sign up to request clarification or add additional context in comments.

5 Comments

+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.
@πάνταῥεῖ Well well, I did not know that. Learn something new every day, Thanks!
i cant use those functions, hence why the code is much more complicated than necessary
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.