布尔比较在应该为真时返回假

本文关键字:返回 比较 布尔 | 更新日期: 2023-09-27 18:18:11

这是作业,注意了。我要写一个卡类,deck类,必须有Equals(deck deck),每8次洗牌应该完全匹配原始的deck,打印20次迭代。我有我的洗牌工作-第8次和第16次洗牌是相同的原始甲板,但等号类一直返回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();
        }
    }
}

布尔比较在应该为真时返回假

cards[i] != other[i]中使用的!=操作符在与引用类型一起使用时检查引用是否相等(除非您显式重载它)。你已经实现了一个Card.Equals方法,用它来代替:

for (int i = 0; i < CARDS; i++)
{
    if (!cards[i].Equals(other[i]))
    {
        return false;
    }
}

您应该覆盖object.Equals方法,而不是定义一个新的Equals方法。您可以阅读这篇文章和这篇文章,以获得有关c#中相等的更多信息。

Change…

public bool Equals(DeckOfCards other)
{
    for (int i = 0; i < CARDS; i++)
    {
        if (cards[i] != other[i])
        {
            return false;
        }
    }
    return true;
}

To this instead…

public bool Equals(DeckOfCards other)
{
    for (int i = 0; i < CARDS; i++)
    {
        if (!cards[i].Equals(other[i]))
        {
            return false;
        }
    }
    return true;
}

学习重写对象的正确方法。

首先,你的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;

            }
        }

关于你的问题,你需要比较两个对象的数据。

for (int i = 0; i < CARDS; i++)
            {
                if (!cards[i].Equals(other[i]))
                {
                    return false;
                }
            }

第三,DeckOfCards不应该继承Card,它们没有关系

第四,方法public void Deck应该是DeckOfCards
的构造函数