English 中文(简体)
需要在教学中理解这一守则
原标题:Need help understanding this code in a tutorial
  • 时间:2012-01-11 20:30:06
  •  标签:
  • c#
  • asp.net

该代码随机抽取52张卡,在4个文本箱中各排13张,每个点击。 我谈一下它如何发挥作用的一般想法,但我需要帮助消除一些混乱或了解它是如何运作的。 我对我不敢肯定的话提出了大胆的评论。

namespace Cards
{
    enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace}
}

namespace Cards
{
    enum Suit { Clubs, Diamonds, Hearts, Spades }
}

namespace Cards
{
    class PlayingCard 
    {
        private readonly Suit suit;
        private readonly Value value;

        public PlayingCard(Suit s, Value v)
        {
            this.suit = s;
            this.value = v;
        }

        public override string ToString()
        {
            string result = string.Format("{0} of {1}", this.value, this.suit);
            return result;
        }

        public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**
        {
            return this.suit;
        }

        public Value CardValue() // **Same as above**
        {
            return this.value;
        }
    }
}

namespace Cards
{
using System;
using System.Collections;

class Pack // **Data Access Layer?**
{
    public const int NumSuits = 4; 
    public const int CardsPerSuit = 13;
    private PlayingCard[,] cardPack;
    private Random randomCardSelector = new Random();

    public Pack() //**Storing all elements**
    {
        this.cardPack = new PlayingCard[NumSuits, CardsPerSuit]; // 

        for (Suit suit = Suit.Clubs; suit <= Suit.Spades; suit++) // 
        {
            for (Value value = Value.Two; value <= Value.Ace; value++)
            {
                this.cardPack[(int)suit, (int)value] = new PlayingCard(suit, value);
            }
        }
    }

    public PlayingCard DealCardFromPack() // **Purpose: To dealing unique cards**
    {
        Suit suit = (Suit)randomCardSelector.Next(NumSuits); // **picks random 0-3 from Suit**
        while (this.IsSuitEmpty(suit)) // **Purpose: Checks if empty but don t know how it works**
        {
            suit = (Suit)randomCardSelector.Next(NumSuits);
        }
        Value value = (Value)randomCardSelector.Next(CardsPerSuit);
        while (this.IsCardAlreadyDealt(suit, value)) // ?
        {
            value = (Value)randomCardSelector.Next(CardsPerSuit);
        }
        PlayingCard card = this.cardPack[(int)suit, (int)value];
        this.cardPack[(int)suit, (int)value] = null; // **sets the current element to null so it isn t reused.**
        return card;
    }

    private bool IsSuitEmpty(Suit suit) // **checks if empty**
    {
        bool result = true;

        for (Value value = Value.Two; value <= Value.Ace; value++) //**Checks Null or not**
        {
            if(!IsCardAlreadyDealt(suit, value))
            {
                result = false;
                break;
            }
        }
        return result;
    }

    private bool IsCardAlreadyDealt(Suit suit, Value value) //**returns current element null?**
    {
        return (this.cardPack[(int)suit, (int)value] == null);
    }
}
}

namespace Cards
{
using System;
using System.Collections;

class Hand // **Business layer?**
{
    public const int HandSize = 13;
    private PlayingCard[] cards = new PlayingCard[HandSize]; // **single array?  Thought it had to be 2 because of 2 parameters?**
    private int playingCardCount = 0;

    public void AddCardToHand(PlayingCard cardDealt)//**Confusion when class used as variable**
    {
        if (this.playingCardCount >= HandSize)
        {
            throw new ArgumentException("Too many cards");
        }
        this.cards[this.playingCardCount] = cardDealt; //**Confused...cardDealt value going in card[]?  How does that work..**
        this.playingCardCount++;//**incrementing, how is this helpful**
    }

    public override string ToString()  //to show all 13 cards in hand
    {
        string result = "";
        foreach (PlayingCard card in this.cards)
        {
            result += card.ToString() + "
";
        }

        return result;
    }
}
}

namespace Cards
{
    public partial class Game : Window
    {
        public const int NumHands = 4;

        private Pack pack = null;
        private Hand[] hands = {new Hand(), new Hand(), new Hand(), new Hand()}; //**Creates 4 different list all single array?**

        public Game()
        {
            InitializeComponent();
        }

        private void dealClick(object sender, RoutedEventArgs e)
        {
            try
            {
                pack = new Pack(); 

                for (int handNum = 0; handNum < NumHands; handNum++) //
                {
                    hands[handNum] = new Hand();
                    for (int numCards = 0; numCards < Hand.HandSize; numCards++)
                    {
                        PlayingCard cardDealt = pack.DealCardFromPack(); //**Deals 13 random cards into each array.  Don t understand how it worked though.**
                        hands[handNum].AddCardToHand(cardDealt);
                    }
                }

                north.Text = hands[0].ToString();
                south.Text = hands[1].ToString();
                east.Text = hands[2].ToString();
                west.Text = hands[3].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
最佳回答

<>>PlayingCard:

class PlayingCard // **Data Access Layer?**

这与商业模式一样,与数据获取无关。 事实上,我看不出在守则中完全使用任何数据(如数据库),因此这一应用没有数据进入层。

private readonly Suit suit; // **readonly to protect from changing value?**

更正 这使得“可变”价值得以改变,因为一旦造就可变。 注:这一数值在(和仅为)级建筑商中确定。 因此,为了有不同的<条码> 申请,需要为不同类别(不同物体)另立一个实例。

public PlayingCard(Suit s, Value v) //**Returning value of every card**

实际情况并非如此。 这是该阶级的构造者。 创建新班级(例如,var卡 = 新的游乐(一些苏特,一些Value))时,这种方法就是为了建立这一榜样。

public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**

这是获得该卡的现值(http://c.un.org/_t_t_t_t_.pdf)的一种方法。 因此,使用卡片的人可以看看看它是否适合。 注意其返回的价值为:http:// roc.un.org 否则,可以从该类外读。 (我不赞成这一执行,而是希望财产而不是一种方法,但这个主题完全属于另一个主题。)

<<>Pack>/strong>:

class Pack // **Data Access Layer?**

Nope, 同前。 这些都是模式。 我只字不提。

public Pack() //**Storing all elements**

司机再次像以前一样。

public PlayingCard DealCardFromPack() // **Purpose: To dealing unique cards**

是的。 这是在<代码>Pack物体上的合乎逻辑的方法。 可以合理地预计,<代码>Pack可用于处理<代码>。 宗旨和目标: 即可从<代码>Pack中提取。 因此,这一方法是什么。

Suit suit = (Suit)randomCardSelector.Next(NumSuits); // **picks random 0-3 from Suit**

缩略语

while (this.IsSuitEmpty(suit)) // **Purpose: Checks if empty but don t know how it works**

单袋的卡片数量有限。 每次删除一次,从持有卡的类别中删除(<代码>Pack)。 因此,它从不再有卡片的一件大事中去处理。 因此,这种检查是为了看看是否有在诉讼过程中留下的卡片,然后再试图处理。

this.cardPack[(int)suit, (int)value] = null; // **sets the current element to null so it isn t reused.**

这一点与前一部分有关。 处理卡时,该卡被拆除。 因此,这一“删除”源自<代码>Pack。

private bool IsSuitEmpty(Suit suit) // **checks if empty**

这是班级内的一种帮助方法。 处理卡片的方法需要检查,如果包裹是空的,那么这种方法就是用文字进行。 (所有法典都采用一种大方法,但设计不好。)

private bool IsCardAlreadyDealt(Suit suit, Value value) //**returns current element null?**

这是另一种帮助方法。 当处理卡时,人们可以拿已经处理过的卡片。 因此,这种检查就是这样。 因此,使用这些方法的较大方法是: “尽管随机检查诉讼案,但我需要一份仍然有卡片的诉状。 在随机抽取这张照相机时,我需要一张仍在车上的卡片。 现在从舱面拆除该卡

<>Hand:

class Hand // **Business layer?**

Kind of. Sort of. It s another model. I ll get to that in a second.

private PlayingCard[] cards = new PlayingCard[HandSize]; // **single array?  Thought it had to be 2 because of 2 parameters?**

什么是两个参数? 它有一系列的卡片。 每张<代码>Hand基本上都是收集卡片。 这些卡片的参考资料。

public void AddCardToHand(PlayingCard cardDealt)//**Confusion when class used as variable**

我不肯定你在评论中提出什么。 这种方法的目的是增加一张卡。 因此,对卡片的提及是采用这种方法的,而这种方法又增加了目前手。

this.cards[this.playingCardCount] = cardDealt; //**Confused...cardDealt value going in card[]?  How does that work..**

In this case, cardDealt is the card that was given to the Hand to be added. It has nothing to do with the "dealt" concept in the previous class. It s just the name of the variable. What this line is doing is adding that card to the array of cards in the Hand.

this.playingCardCount++;//**incrementing, how is this helpful**

The hand is now larger by one card. So if you added, say, the third card to the hand, it would be added as this.cards[2]. Adding another card to the hand after that needs to go in this.cards[3], otherwise it would replace an existing card, which isn t the desired effect.

public override string ToString()  //to show all 13 cards in hand

与此相对照的是,这种方法只是打印了目前载于<代码>的内容。 Hand 。


Conclusion:

我们现在开始这项工作(并感到可以自由要求澄清),现在,请允许我谈谈什么是“模范”。 它是属于“企业”的一类企业。 也就是说,它是一个包含商业概念的物体。

在这项申请中,有三种具体概念,一方面是交易卡的“企业”。 它们是:

  • A card
  • A deck, which contains a pre-determined finite list of cards
  • A hand, which contains a list of cards given to it

每个模式都有责任内部维护其业务逻辑。 这是根据其内部变量、特性、方法等进行的。 诸如<代码>DealCardFromDeck等内容是实际世界概念,说明如何将业务工作编码为模型。 另一种模式并不忽视这一模式如何实施这一商业逻辑,它们只是知道,我们可以从舱面购买一张卡。 (这是与其他模型的外部联系模式的一部分。)

商业逻辑(如果你会的话,如果你会的话,尽管这部法典实际上有“传承者”本身,那么这些模式如何相互交流。 我的上述三个模式的清单最简单地描述了它们是如何互动的。 守则只是这一逻辑的实施。

问题回答

部分答复:

贺卡、包装和手工艺是而不是 部分数据存取。 它们是申请的模式(目标/实体/主要模式)。

如果有的话,你可以向Save/Load发出任何附则,或者说是Db,或者说是DAL。

 private readonly Suit suit; // **readonly to protect from changing value?**
 private readonly Value value;

是的,只是因为一旦有一张卡就永远不会改变。 这将破坏游戏的逻辑(和诚实)。 把这一概念纳入模式是一个好的想法。

    public PlayingCard(Suit s, Value v) //**Returning value of every card**

这不是归还任何东西,而是“跳舞会”班的 建筑或>。

    public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**

页: 1 允许其他法典找到哪张卡的那类法律非常重要。 请注意,仅读的外地代码<> 诉请/代码>为非公开。

Question: // Data Access Layer?

Answer: No. This is a class that models a single card


Question: // Business layer?

答复:无 这是一个模范收集卡的班子。

守则是自我解释的。 我猜测你需要更深入地了解如何规划,例如:

  • Read only variables are used that way because their values can only be set on the constructor of the class.
  • the method you say it returns the value of every card is actually the constructor and it is used to assign the suit and value of the card.
  • the CardSuit() and CardValue() methods return the current suit and value values for a card.

大部分障碍是基本的C#,即一个体面的方案者应当承认正确。 处理随机卡的算法有缺陷。 概述:

选择任意诉讼。

如果已经处理该诉讼的所有卡片,将进入第1步。

选择随机排名。

如果已经处理选定诉讼的卡片和排名,将进入第3步。

Remove the selected card from the deck.

Deal it.

这方面最能说明存在缺陷的原因。 下面的卡片留在板块(2C=2个俱乐部):

2C 2S 3S 4S

在现实生活中,接触2个俱乐部的机会将是1/4。 采用这一算法是1/2,因为它拥有选择俱乐部的同等机会。 把它看作是将舱面分解成4个堆,任意选择一个堆子来处置。 每个弹片都是一件事。 这不是这样做的方法。

I give you the permission to look down on the writer of this code. Oh, and find a better book.

    public Suit CardSuit() // **Not sure why this is here.  Maybe for future use?**
    {
        return this.suit;
    }

    public Value CardValue() // **Same as above**
    {
        return this.value;
    }

These methods are commonly referred to as "getters". When called, these methods return a piece of the internal state (variables) of the class. Programmers commonly add them to a call, even there is no immediate use for them.

很抱歉,我认为,关于C#的介绍书比斯纳克里多。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签