这是家庭作业,抬头。我要写一个卡片类,卡片类,必须有Equals(Deck aDeck),每8个shuffle应该与原版套牌完美匹配,打印20次迭代。我有我的shuffle工作 - 第8和第16次洗牌与原始牌组相同,但Equals类不断返回false。我错过了一些东西并且一直在研究这个问题 - 任何人都可以指导我的错误吗?提前谢谢!
namespace lab2part3
{
public class DeckOfCards : Card
{
const int CARDS = 52;
private Card[] cards;
public void Deck()
{
cards = new Card[CARDS];
for (int suitVal = 0; suitVal < 4; suitVal++)
{
for (int rankVal = 1; rankVal < 14; rankVal++)
{
cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
}
}
}
public Card GetCard(int cardNum)
{
if (cardNum >= 0 & cardNum <= 51)
return cards[cardNum];
else
throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 0 and 51."));
}
public void Shuffle()
{
Card[] newDeck = new Card[CARDS];
bool[] assigned = new bool[CARDS];
Random sourceGen = new Random();
for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;
while (foundCard == false)
{
destCard = sourceGen.Next(CARDS);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}
public void Faro()
{
Card[] firstDeck = new Card[26];
Card[] secondDeck = new Card[26];
Card[] finalDeck = new Card[CARDS];
Array.Copy(cards, 0, firstDeck, 0, 26);
Array.Copy(cards, 26, secondDeck, 0, 26);
for (int i = 0, j = 0; i < CARDS; i += 2, j++)
{
cards[i] = firstDeck[j];
cards[i + 1] = secondDeck[j];
}
}
public bool Equals(DeckOfCards other)
{
for (int i = 0; i < CARDS; i++)
{
if (cards[i] != other[i])
{
return false;
}
}
return true;
}
public Card this[int i]
{
get { return cards[i]; }
}
}
}
-
namespace lab2part3
{
public class Card
{
public enum Suit { H, C, D, S }
public enum Rank { _A = 1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _J, _Q, _K }
public Suit suit { get; set; }
public Rank rank { get; set; }
public Card(Suit newSuit, Rank newRank)
{
suit = newSuit;
rank = newRank;
}
public Card() { }
public override string ToString()
{
StringBuilder s = new StringBuilder(rank.ToString());
s.Remove(0, 1);
return (s + "" + suit);
}
public bool Equals(Card other)
{
return rank == other.rank && suit == other.suit;
}
}
}
-
namespace lab2part3
{
public class CardTester
{
public static void Main()
{
DeckOfCards MyDeck = new DeckOfCards();
DeckOfCards CopyDeck = new DeckOfCards();
Card tempCard = new DeckOfCards();
MyDeck.Deck();
CopyDeck.Deck();
// initial deck setup
for (int i = 0; i < 52; i++)
{
tempCard = MyDeck.GetCard(i);
Console.Write(tempCard.ToString());
if (i != 51)
Console.Write(", ");
else
Console.WriteLine();
if (i == 12 || i == 25 || i == 38)
Console.WriteLine();
}
// 20 looped shuffles
for (int j = 0; j < 20; j++)
{
MyDeck.Faro();
Console.WriteLine("\nShuffle #" + (j + 1) + ":\n");
for (int i = 0; i < 52; i++)
{
tempCard = MyDeck.GetCard(i);
Console.Write(tempCard.ToString());
if (i != 51)
Console.Write(", ");
else
Console.WriteLine();
if (i == 12 || i == 25 || i == 38)
Console.WriteLine();
}
// compare
Console.WriteLine("does this deck equal the original deck? {0}", CopyDeck.Equals(MyDeck));
// print original deck
for (int i = 0; i < 52; i++)
{
tempCard = CopyDeck.GetCard(i);
Console.Write(tempCard.ToString());
if (i != 51)
Console.Write(", ");
else
Console.WriteLine();
if (i == 12 || i == 25 || i == 38)
Console.WriteLine();
}
}
Console.ReadKey();
}
}
}
答案 0 :(得分:2)
!=
中使用的cards[i] != other[i]
运算符在与引用类型一起使用时检查引用相等性(除非您明确地重载它)。您已经实现了Card.Equals
方法,请改为使用它:
for (int i = 0; i < CARDS; i++)
{
if (!cards[i].Equals(other[i]))
{
return false;
}
}
您应该覆盖Equals
方法,而不是定义新的object.Equals
方法。
您可以阅读this和this article以获取有关C#中相等性的更多信息。
答案 1 :(得分:1)
更改...
public bool Equals(DeckOfCards other)
{
for (int i = 0; i < CARDS; i++)
{
if (cards[i] != other[i])
{
return false;
}
}
return true;
}
改为......
public bool Equals(DeckOfCards other)
{
for (int i = 0; i < CARDS; i++)
{
if (!cards[i].Equals(other[i]))
{
return false;
}
}
return true;
}
研究覆盖object.Equals的正确方法,并为!=等处理运算符重载。
答案 2 :(得分:0)
好的,首先,你的Shuffle方法超级有效,所以我必须解决它:
public void Shuffle()
{
Random sourceGen = new Random();
for (int i = 0; i < 52; i++)
{
Card temp = cards[i];
int pos = sourceGen.Next(CARDS);
cards[i] = cards[pos];
cards[pos] = temp;
}
}
关于你的问题,你需要比较2个对象的数据。
for (int i = 0; i < CARDS; i++)
{
if (!cards[i].Equals(other[i]))
{
return false;
}
}
第三,DeckOfCards
不应该从Card
继承,它们没有关系
Forth,方法public void Deck
应该是DeckOfCards