我的课程中有一个任务是编写一个程序,创建52个卡片对象,每个卡片都有一个值和一个套装.在我们创建一个对象数组后,我们必须随机切换两张卡的位置.问题是,当我去交换我的卡时,它们都变成了相同的.这是导致问题的代码:
void SwapCards(Card* cardOne, Card* cardTwo) {
//swap cards here
Card *temp = cardOne;
*cardOne = *cardTwo;
*cardTwo = *temp;
//once it gets here all three variables end up with the same object?
}
Run Code Online (Sandbox Code Playgroud)
现在这里是调用此函数的for循环:
for (int i = 0; i < 104; i++) {
//get two random values and send them to SwapCards to switch both objects
int c_one = RandomRange(0, 51);
int c_two = RandomRange(0, 51);
SwapCards(deck[c_one], deck[c_two]);
}
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将不胜感激.我花了很多时间试图解决这个问题,但这让我很困惑.