如何定义和访问类中的列表 - 如何在纸牌游戏中访问玩家的手牌

时间:2014-05-03 19:44:27

标签: c# list playing-cards

我意识到之前可能已经提到了这一点,但我一直在寻找,虽然我在理论上理解了我需要做什么,但实际上让它发挥作用的方式正在暗示着我。

我正在尝试通过尝试项目来学习C#和面向对象的编程概念。这个特别的项目是复制顶级特朗普纸牌游戏。我的主题将是权力的游戏。

我的问题是,当我初始化它时,我对填充播放器1和计算机之手感到难过。我可以看到卡片从甲板上消失了但是我无法在玩家的手中看到它们(虽然当我做断点/引脚来源时。我可以看到cardinHand填充。

问题在于游戏最终会有60张牌(总共)而不是15张。(在创建Deck时加载的XML中有30行)。 你能检查我的代码,指出我出错的地方。我知道我需要传递卡片对象,我担心我只是遗漏了一些简单的东西。我觉得这是重要的部分,因为我想根据玩家的手牌在表单中显示牌。

这是GameStart方法

public static void startGame()
    {
    Player player1 = new Player();
    Player computer = new Player();

    var newdeck = new Deck();
    newdeck.Shuffle();

    while (newdeck.CountCards() != 0)
    {
        newdeck.dealCards();
    }

    MessageBox.Show("New Game Started");

这是我的甲板课程

public class Deck
{

private List<Card> deckofCards = new List<Card>();


//indexer on the deck
// Define the indexer, which will allow client code 
// to use [] notation on the class instance itself. 

public Card this[int i]
{
    get
    {
        // This indexer is very simple, and just returns or sets 
        // the corresponding element from the internal array. 
        return deckofCards[i];
    }
}


public Deck()
{
    resetDeck();
}

public void resetDeck()
{

    XmlDocument xmldata = new XmlDocument();
    xmldata.Load(@"C:\GameofThronesXml.xml");
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldata.NameTable);

    XmlNodeList nodeList;
    XmlElement root = xmldata.DocumentElement;
    nodeList = xmldata.SelectNodes("//CharacterData/record", nsmgr);

    foreach (XmlElement data in nodeList)
    {
        Card singlecard = new Card();
        singlecard.CardID = int.Parse(data.SelectSingleNode("./CardID").InnerText);
        singlecard.Name = data.SelectSingleNode("./Name").InnerText;
        singlecard.Intelligence = int.Parse(data.SelectSingleNode("./Intelligence").InnerText);
        singlecard.Ruthlessness = int.Parse(data.SelectSingleNode("./Ruthlessness").InnerText);
        singlecard.Status = data.SelectSingleNode("./Status").InnerText;
        singlecard.Prestige = int.Parse(data.SelectSingleNode("./Prestige").InnerText);
        singlecard.FightingSkill = int.Parse(data.SelectSingleNode("./FightingSkill").InnerText);
        singlecard.AppearedIn = int.Parse(data.SelectSingleNode("./AppearedIn").InnerText);
        singlecard.Description = data.SelectSingleNode("./Description").InnerText;
        deckofCards.Add(singlecard);
    }

    //string path = @"C:\GameofThronesXml.xml";
    //XElement doc = XElement.Load(path);
    //deckofCards = (from items in doc.Descendants("CharacterData")
    //                select new Card(
    //                        int.Parse(items.Element("CardID").Value),
    //                        items.Element("Picture").Value,
    //                        items.Element("Name").Value,
    //                        int.Parse(items.Element("Intelligence").Value),
    //                        int.Parse(items.Element("Ruthlessness").Value),
    //                        items.Element("Status").Value,
    //                        int.Parse(items.Element("Prestige").Value),
    //                        int.Parse(items.Element("FightingSkill").Value),
    //                        int.Parse(items.Element("AppearedIn").Value),
    //                        items.Element("Description").Value)).ToList();
}

public void Shuffle()
{

    deckofCards = deckofCards.OrderBy(c => Guid.NewGuid())
                 .ToList();

}

public int CountCards()
{

    int number = deckofCards.Count();
    return number;

}

public Card dealCards()
{

    Hand hand = Player.hand;
    //Hand computerHand = Player.hand;

    if (this.deckofCards.Count == 0)
        throw new InvalidOperationException("There are no cards to deal.");

    Card card = deckofCards[0];

    hand.cardsinHand.Add(card);
    deckofCards.RemoveAt(0);
    return card;

 }
}

手类

public class Hand
  {

    /// <summary>
    /// The deck in the hand
    /// </summary>
    public List<Card> cardsinHand = new List<Card>();

    public int GetNumberofCards()
    {
        int count = cardsinHand.Count;
        return count;
    }

    public void AddCard(Card card)
    {
        cardsinHand.Add(card);
    }



}

玩家类

public class Player
{

    public static Hand hand = new Hand();


}

tl; dr - 希望有两名玩家等级的玩家,在这种情况下我可以访问已发牌的牌数,每张15张牌 - 我知道它与hand.cardsinhand有关.Count - 我无法使用已定义的播放器类访问它。

解决方案 -

        Player Player1 = new Player();
        Player Computer = new Player();

        Deck newdeck = new Deck();
        Hand CompHand = new Hand();
        Hand PlyrHand = new Hand();

        newdeck.Shuffle();

        while (newdeck.CountCards() != 0)
        {
            //The Hand adds the return dealt cards to the respective lists.
            CompHand.AddCard(newdeck.dealCards());
            PlyrHand.AddCard(newdeck.dealCards());
        }

        MessageBox.Show("New Game Started");

        //Snoopy Dance - Each card has 15 cards in each hand.
        MessageBox.Show(CompHand.cardsinHand.Count().ToString());
        MessageBox.Show(PlyrHand.cardsinHand.Count().ToString());

1 个答案:

答案 0 :(得分:1)

startGame中的此代码会一直处理,直到没有卡片为止:

while (newdeck.CountCards() != 0)
{
    newdeck.dealCards();
}

但是dealCards正在访问静态(即由类的所有实例共享)Player.hand,所以所有的卡都被添加到那里。 (我猜你在为两位玩家添加每张牌时都会得到60分,并且当其中一名玩家被注释掉时,你只会得到30分,就像问题一样。)

您不希望所有玩家共享同一只手,因此将Player.Hand更改为不是静态的(即删除static关键字)。然后通过你的两个初始化的球员&#39;手(player1.handcomputer.hand)作为dealCards的参数:

public Card dealCards(Hand hand, Hand computerHand)
...

我认为有哪些课程可以完成哪些课程负责做什么(可能会在游戏正常工作后发布到codereview.stackexchange.com),但这个改变应该帮助你振作起来。