如何定义和访问类中的列表-如何在纸牌游戏中访问玩家的手牌
本文关键字:访问 纸牌游戏 玩家 何定义 定义 列表 | 更新日期: 2023-09-27 18:01:44
我意识到这可能已经被问过了,但我一直在寻找,虽然我在理论上理解我需要做什么,但实际上使它工作的方式是在暗示我。
我试图通过尝试一个项目来学习c#和面向对象编程的概念。这个特别的项目是复制王牌纸牌游戏。我的主题是《权力的游戏》
我的问题是,当我初始化玩家1和电脑手时,我在填充玩家1和电脑手方面遇到了困难。我可以看到卡片从甲板上消失,但我看不到他们在玩家的手中(尽管当我做断点/引脚源。我可以看到枢机主教的人数。
问题是,游戏结束时他们手中总共有60张牌,而不是15张。(在创建Deck时加载XML中的30行)。你能检查我的代码,给指针在哪里我错了。我知道我需要传递card对象,我担心我漏掉了一些简单的东西。我觉得这是很重要的一部分,因为我想要根据玩家的手牌来显示卡片。
这是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");
This is my Deck Class
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张牌每个 -我知道它有一些与手。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());
startGame
中的代码一直发牌,直到没有牌剩余:
while (newdeck.CountCards() != 0)
{
newdeck.dealCards();
}
但是dealCards
正在访问静态(即由类的所有实例共享)Player.hand
,所以所有的卡牌都被添加到其中。(我猜当你为两名玩家添加每张卡牌时,你会得到60,而当其中一名玩家被注释掉时,你只会得到30,就像问题中那样。)
你不希望所有玩家共用同一手牌,所以将Player.Hand
更改为非静态(即删除static
关键字)。然后将两个初始化的玩家手牌(player1.hand
和computer.hand
)作为参数传递给dealCards
:
public Card dealCards(Hand hand, Hand computerHand)
...
我认为有一些整理做关于哪些类有责任做什么(也许张贴到codereview.stackexchange.com一旦你的游戏工作),但这一变化应该让你启动和运行。