我在一个单链表中有一副牌。我试图随机交换没有重复的值。为此,我添加了一个布尔变量"used"到每个节点并将其设置为false。每当我使用卡片时,我都会将其更新为true,这样它们就不会再次使用。我试图添加一个while循环,以确保没有使用过的卡片被交换。然而,一旦开始while循环,每个节点的used变量都被设置为true,因此while循环永远不会中断。我在while循环之前打印了使用的变量值,它们都被设置为false。如有任何帮助,不胜感激。
void DeckOfCards::randomSwap()
{
Card* ptr = head;
Card* n = NULL;
Card* temp = NULL;
srand((unsigned)(time(0)));
for (int i = 1; i < 52; ++i)
{
int r = rand() % 52 + 1;
n = getNode(r);
while(n->used != false)
{
r = rand() % 52 + 1;
n = getNode(r);
}
temp = ptr;
ptr->setRank(n->getRank());
ptr->setSuit(n->getSuit());
n->setRank(temp->getRank());
n->setSuit(temp->getSuit());
n->used = true;
ptr->used = true;
ptr = ptr->next;
}
std::cout << "Cards randomly swapped." << std::endl;
}
#pragma once
#include <iostream>
class Card
{
private:
int cardRank;
std::string cardSuit;
public:
Card* next;
bool used = false;
Card();
~Card();
void setRank(int);
void setSuit(std::string);
int getRank();
std::string getSuit();
};
#include "Card.h"
Card::Card()
{
cardRank = 0;
cardSuit = "Default";
next = NULL;
used = false;
}
Card::~Card()
{
next = NULL;
delete next;
}
void Card::setRank(int r)
{
cardRank = r;
}
void Card::setSuit(std::string s)
{
cardSuit = s;
}
int Card::getRank()
{
return cardRank;
}
std::string Card::getSuit()
{
return cardSuit;
}
#include "DeckOfCards.h"
#include <ctime>
#include <iostream>
DeckOfCards::DeckOfCards()
{
head = NULL;
tail = NULL;
}
DeckOfCards::~DeckOfCards()
{
head = NULL;
delete head;
tail = NULL;
delete tail;
}
void DeckOfCards::addNode(int r, std::string s)
{
Card* n = new Card;
n->setRank(r);
n->setSuit(s);
n->used = false;
n->next = NULL;
if (head == NULL)
{
head = n;
tail = n;
}
else
{
tail->next = n;
tail = tail->next;
}
}
void DeckOfCards::createDeck()
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 13; ++j)
{
addNode(rank[j], suit[i]);
}
}
}
void DeckOfCards::print()
{
Card* temp = head;
while (temp != NULL)
{
std::cout << "(" << temp->getRank() << " of " << temp->getSuit() << ")n";
temp = temp->next;
}
std::cout << std::endl;
}
bool DeckOfCards::CardExists(int r, std::string s)
{
Card* temp = head;
for (int i = 0; i < 52; ++i)
{
if (r == temp->getRank() && s == temp->getSuit())
return true;
else
{
temp = temp->next;
}
}
return false;
}
Card* DeckOfCards::getNode(int pos)
{
Card* temp = head;
for (int i = 0; i < pos-1; ++i)
{
temp = temp->next;
}
return temp;
}
void DeckOfCards::randomSwap()
{
Card* ptr = head;
Card* n = NULL;
for (int i = 1; i < 52; ++i)
{
int r = rand() % 52 + 1;
n = getNode(r);
while(n->used != false)
{
std::cout << r << " " << n->used << std::endl;
r = rand() % 52 + 1;
n = getNode(r);
}
int tempRank = ptr->getRank();
std::string tempSuit = ptr->getSuit();
ptr->setRank(n->getRank());
ptr->setSuit(n->getSuit());
n->setRank(tempRank);
n->setSuit(tempSuit);
n->used = true;
ptr->used = true;
ptr = ptr->next;
}
std::cout << "Cards randomly swapped." << std::endl;
}
void DeckOfCards::shuffleCards()
{
Card* tail = NULL;
Card* temp = NULL;
Card* ptr = head;
while (ptr != NULL)
{
tail = ptr;
ptr = ptr->next;
}
ptr = head;
temp = tail;
for (int i = 0; i < 26; ++i)
{
int tempRank = temp->getRank();
std::string tempSuit = temp->getSuit();
temp->setSuit(ptr->getSuit());
temp->setRank(ptr->getRank());
ptr->setSuit(tempSuit);
ptr->setRank(tempRank);
temp = getNode(51-i);
ptr = ptr->next;
}
std::cout << "Cards shuffled.n" << std::endl;
}
#pragma once
#include "Card.h"
#include <iostream>
class DeckOfCards
{
private:
Card *head, *tail;
int rank[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
std::string suit[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
public:
DeckOfCards();
~DeckOfCards();
void addNode(int,std::string);
void createDeck();
void randomSwap();
void print();
void shuffleCards();
Card* getNode(int);
bool CardExists(int r, std::string s);
};
#include <iostream>
#include "DeckOfCards.h"
#include "CircularList.h"
#include <ctime>
int main()
{
srand((unsigned)(time(0)));
DeckOfCards d;
d.createDeck();
d.randomSwap();
d.shuffleCards();
return 0;
temp = ptr;
好的,所以temp
和ptr
都是指向同一个对象的指针。
ptr->setRank(n->getRank());
ptr->setSuit(n->getSuit());
现在,对象temp
和ptr
都指向,对象n
指向具有相同的秩和花色。
n->setRank(temp->getRank());
n->setSuit(temp->getSuit());
哎呀,现在你设置对象n
点回到相同的等级和适合它在第一个地方。那不是你想做的。
你一定认为ptr->setRank
和temp->SetRank
做不同的事情。但是它们做的事情是一样的,因为它们都在同一个对象上调用setRank
。所以temp
在这里不起作用。您需要一个临时对象或其他可以在交换操作期间保存rank和suit的临时对象。临时指针对你没有帮助。
也:
Card::~Card()
{
next = NULL;
delete next;
}
这不起任何作用。将next
设置为NULL
没有任何作用,因为该对象即将停止存在。在NULL
上调用delete
没有任何作用。那么析构函数的目的是什么呢?