winforms 如何将另一个类中的方法添加到我的WindowsForms类中?

gajydyqb  于 2022-12-14  发布在  Windows
关注(0)|答案(1)|浏览(148)

我正在创建一个21点游戏,并使用了从互联网教程到这一点。然而,我已经完成了教程,我已经离开添加所有的方法从其他类到类,在那里玩21点游戏,不能弄清楚如何添加方法。我想知道我将如何去添加所有的方法到P1类。
第一个

dnph8jn4

dnph8jn41#

在你的帖子中,你问如何“将其他类的所有方法添加到要玩21点游戏的类中”。基本上,当您在MainForm中声明类似Deck类的 instance 时,它的所有成员都已被“添加”因为你可以使用你声明的成员属性来访问它们。所以这是我们在MainForm中要做的第一件事。我还会提到,有时候有staticconst成员,它们不需要示例来使用它们,而是使用类名来代替。你会在MainForm构造函数中看到这样的例子,比如Card.Spades

public partial class MainForm : Form
{
    // Make instance of the card deck using the `Deck` class
    Deck DeckInstance = new Deck();
    public MainForm()
    {
        InitializeComponent();
        Text = "Card Game";
        tableLayoutCards.Font = new Font("Sergoe UI Symbol", 9);
        // Static or const members of a class do not require an
        // instance. Use the class name to reference these members.
        labelHandA1.Text = $"10 {Card.Spades}";
        labelHandA2.Text = $"J {Card.Spades}";
        labelHandA3.Text = $"Q {Card.Spades}";
        labelHandA4.Text = $"K {Card.Spades}";
        labelHandA5.Text = $"A {Card.Spades}";
        labelHandB1.Text = $"10 {Card.Hearts}";
        labelHandB2.Text = $"J {Card.Hearts}";
        labelHandB3.Text = $"Q {Card.Hearts}";
        labelHandB4.Text = $"K {Card.Hearts}";
        labelHandB5.Text = $"A {Card.Hearts}";
        buttonDeal.Click += dealTheCards;
    }
    private async void dealTheCards(object sender, EventArgs e)
    {
        buttonDeal.Refresh(); UseWaitCursor = true;
        // When a non-UI task might take some time, run it on a Task.
        await DeckInstance.Shuffle();
        // Now we need the instance of the Desk to get the
        // cards one-by-one so use the property we declared.
        setCard(labelHandA1, DeckInstance.Dequeue());
        setCard(labelHandA2, DeckInstance.Dequeue());
        setCard(labelHandA3, DeckInstance.Dequeue());
        setCard(labelHandA4, DeckInstance.Dequeue());
        setCard(labelHandA5, DeckInstance.Dequeue());
        setCard(labelHandB1, DeckInstance.Dequeue());
        setCard(labelHandB2, DeckInstance.Dequeue());
        setCard(labelHandB3, DeckInstance.Dequeue());
        setCard(labelHandB4, DeckInstance.Dequeue());
        setCard(labelHandB5, DeckInstance.Dequeue());
        UseWaitCursor = false;
        // Dum hack to make sure the cursor redraws.
        Cursor.Position = Point.Add(Cursor.Position, new Size(1,1));
    }
    private void setCard(Label label, Card card)
    {
        label.Text = card.ToString();
        switch (card.CardSuit)
        {
            case CardSuit.Hearts:
            case CardSuit.Diamonds:
                label.ForeColor = Color.Red;
                break;
            case CardSuit.Spades:
            case CardSuit.Clubs:
                label.ForeColor = Color.Black;
                break;
        }
    }
}

总的来说,我从代码 * 示例 * 中学到了比教程更多的东西,在代码 * 示例 * 中,我可以运行和设置断点,所以我做了一个你可以clone(但它不是一个 * 21点 * 游戏,我会留给你,所以我不会剥夺你的乐趣!)

使用下列简化类别:

信用卡

public class Card
{
    // https://office-watch.com/2021/playing-card-suits-%E2%99%A0%E2%99%A5%E2%99%A6%E2%99%A3-in-word-excel-powerpoint-and-outlook/#:~:text=Insert%20%7C%20Symbols%20%7C%20Symbol%20and%20look,into%20the%20character%20code%20box.
    public const string Hearts = "\u2665";
    public const string Spades = "\u2660";
    public const string Clubs = "\u2663";
    public const string Diamonds = "\u2666";
    public CardValue CardValue { get; set; }
    public CardSuit CardSuit { get; set; }
    public override string ToString()
    {
        string value, suit = null;
        switch (CardValue)
        {
            case CardValue.Ace: value = "A"; break;
            case CardValue.Jack: value = "J"; break;
            case CardValue.Queen:  value = "Q"; break;
            case CardValue.King: value = "K"; break;
            default: value = $"{(int)CardValue}"; break;
        }
        switch (CardSuit)
        {
            case CardSuit.Hearts: suit = Hearts; break;
            case CardSuit.Spades: suit = Spades; break;
            case CardSuit.Clubs: suit = Clubs; break;
            case CardSuit.Diamonds: suit = Diamonds ; break;
        }
        return $"{value} {suit}";
    }
}

甲板

public class Deck : Queue<Card>
{
    // Instantiate Random ONE time (not EVERY time).
    private readonly Random _rando = new Random();
    private readonly Card[] _unshuffled;
    public Deck()
    {
        List<Card> tmp = new List<Card>();
        foreach(CardValue value in Enum.GetValues(typeof(CardValue)))
        {
            foreach (CardSuit suit in Enum.GetValues(typeof(CardSuit)))
            {
                tmp.Add(new Card { CardValue = value, CardSuit = suit });
            }
        }
        _unshuffled = tmp.ToArray();
    }
    public async Task Shuffle()
    {
        Clear();
        List<int> sequence = Enumerable.Range(0, 52).ToList();
        while (sequence.Count != 0)
        {
            // Choose a unique card index from the sequence at
            // random based on the number of ints that remain.
            int randomIndexInSequence = _rando.Next(0, sequence.Count());
            int cardNumberOutOfOf52 = sequence[randomIndexInSequence];
            Enqueue(_unshuffled[cardNumberOutOfOf52]);
            sequence.RemoveAt(randomIndexInSequence);
        }
        // Spin a wait cursor as a visual indicator that "something is happening".
        await Task.Delay(TimeSpan.FromMilliseconds(500)); 
    }
    public Card GetNext() => Dequeue();
}

枚举

public enum CardValue
{
    Ace = 1,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King,
}
public enum CardSuit
{
    Hearts,
    Spades,
    Clubs,
    Diamonds,
}

相关问题