简体   繁体   English

布尔比较在应为true时返回false

[英]Boolean comparision returns false when it should be true

This is homework, heads up. 注意,这是作业。 I'm to write a card class, deck class, must have Equals(Deck aDeck), every 8 shuffle should perfectly match the original deck, print 20 iterations. 我要编写一个卡片类,卡片组,必须具有Equals(Deck aDeck),每8个随机播放都应完全匹配原始卡片组,打印20次迭代。 I have my shuffle working - 8th and 16th shuffle is the same as the original deck, but the Equals class keeps returning false. 我正在进行随机播放-第8和第16随机播放与原始卡片组相同,但Equals类始终返回false。 I'm missing something and have been poring over this - can anybody guide me to my error? 我缺少了一些东西,并且一直在仔细研究-有人可以引导我解决我的错误吗? Thanks in advance! 提前致谢!

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();
        }
    }
}

The != operator you use in cards[i] != other[i] checks reference equality when used with reference types (unless you explicitly overload it). cards[i] != other[i]使用的!=运算符cards[i] != other[i]与引用类型一起使用时,将检查引用相等性(除非您显式重载了它)。 You already implemented a Card.Equals method, use it instead: 您已经实现了Card.Equals方法,请改用它:

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

Instead of defining a new Equals method, you should override object.Equals method. 而不是定义新的Equals方法,您应该覆盖object.Equals方法。 You can read this and this article to get some more information about equality in C#. 您可以阅读本文以及本文,以获取有关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;
}

Study up on proper ways to override object.Equals, and dealing the operator overloading for !=, etc. 研究覆盖对象相等的正确方法,并处理运算符重载!=等。

ok, first, your Shuffle method is super in-effective so I must fix it : 好的,首先,您的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;


            }
        }

As to your question, you need to compare between the data of 2 objects. 关于您的问题,您需要在2个对象的数据之间进行比较。

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

And third, DeckOfCards should not inherit from Card , they have no relation 第三, DeckOfCards不应继承Card ,它们没有关系

Forth, the method public void Deck should be the constructor of DeckOfCards 第四,方法public void Deck应该是DeckOfCards的构造DeckOfCards

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM