使用指针随机化数组

时间:2014-09-18 00:57:49

标签: c++

我试图用指针洗牌。我的循环只打印出默认值,而不是在卡片中循环。我该如何解决。洗牌功能在底部?我试图循环并增加指针,同时用我的源指针替换目标指针。或解除引用的价值。我做错了什么?

#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++;     
               }  
  }}}

2 个答案:

答案 0 :(得分:6)

只需使用std::vectorstd::shuffle即可。另请勿使用rand(),而是使用<random>标头。而std::string代替char*

答案 1 :(得分:0)

我更喜欢Donald Knuth在他的计算机编程艺术中作为算法P的Fisher-Yeats shuffle。伪代码可以在http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle找到。顺便提一下,为了获得更好的随机化属性,我建议使用Mersenne Twister(http://en.wikipedia.org/wiki/Mersenne_Twister),您可以使用多种语言找到GPLd源