gpt4 book ai didi

c++ - 使用指针随机化数组

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:32 25 4
gpt4 key购买 nike

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

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

最佳答案

只需使用 std::vector std::shuffle .也不要使用 rand() , 使用 <random> header 代替。和 std::string而不是 char* .

关于c++ - 使用指针随机化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25902574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com