YU-GI-OH! POWER OF XLINQ [C#, XLINQ, XML]
WRITTEN BY ALLEN LEE
0. TABLE OF CONTENT
-
1. YU-GI-OH! POWER OF XLINQ.
-
2. GAME CARDS.
-
3. <CARDS>...</CARDS>
-
4. CLASS CARD { ... }
-
5. GIVE ME WHAT I WANT!
-
6. SHOW ME THE RESULTS.
- 7. REFERENCES.
1. YU-GI-OH! POWER OF XLINQ.
如果你玩过游戏王的决斗怪兽游戏,你会发现一个好的卡片信息管理器能使你装备卡片组的工作事半功倍。例如,Yu-Gi-Oh! Power of Chaos 系列游戏就内置了一个简单的卡片信息管理器。

现在,我需要一个更强大的卡片信息管理器,代号为 Positron,它从各个方面对卡片信息进行筛选和排序,并协助我寻找合适的卡片装备到我的卡片组中。
2. GAME CARDS.
要管理好这些卡片,我们得首先了解这些卡片是如何分类的,以及它们包含了哪些信息。根据官方的说明,卡片的概要分类如下:
-
Monster Cards(怪兽卡)
-
A. Normal Monster Cards(普通怪兽卡)
-
B. Fusion Monster Cards(融合怪兽卡)
-
C. Ritual Monster Cards(仪式怪兽卡)
- D. Effect Monster Cards(效果怪兽卡)
-
Spell Cards(魔法卡)
-
A. Normal Spell Cards(普通魔法卡)
-
B. Continuous Spell Cards(永续魔法卡)
-
C. Equip Spell Cards(装备魔法卡)
-
D. Field Spell Cards(场地魔法卡)
-
E. Quick-Play Spell Cards(速攻魔法卡)
- F. Ritual Spell Cards(仪式魔法卡)
-
Trap Cards(陷阱卡)
-
A. Normal Trap Cards(普通陷阱卡)
-
B. Counter Trap Cards(反击陷阱卡)
- C. Continuous Trap Cards(永续陷阱卡)
现在我们来细看每一种卡片上所包含的信息。所有的怪兽卡都包含了以下信息:
-
Monster Name(怪兽名字)
-
Attribute(属性)
-
Level(等级)
-
Type(类别)
-
Attack Points (ATK)(攻击力)
-
Defense Points (DEF)(防守力)
- Card Description(卡片描述)
并由 Attribute 和 Type 来划分类别:
Attribute 枚举:
Earth(地属性)、Water(水属性)、Fire(炎属性)、Wind(风属性)、Light(光属性)、Dark(闇属性)
Type 枚举:
Type:Dragon(龙族)、Spellcaster(魔法使族)、Zombie(不死族)、Warrior(战士族)、Beast-Warrior(兽战士族)、Beast(兽族)、Winged Beast(鸟兽族)、Fiend(恶魔族)、Fairy(天使族)、Insect(昆虫族)、Dinosaur(恐龙族)、Reptile(爬虫族)、Fish(鱼族)、Sea Serpent(海龙族)、Machine(机械族)、Thunder(雷族)、Aqua(水族)、Pyro(炎族)、Rock(岩石族)、Plant(植物族)

上图所展示的是一张效果怪兽卡,它包含如下信息:
-
Monster Name(怪兽名字):Deepsea Warrior
-
Attribute(属性):Water
-
Level(等级):5
-
Type(类别):Warrior
-
Attack Points (ATK)(攻击力):1600
-
Defense Points (DEF)(防守力):1800
- Card Description(卡片描述):When "Umi" is face-up on the field, this card is unaffected by any Spell Cards.
其中 Fusion Monster Cards 的 Card Description 上列出了融合所需的怪兽(Fusion-Material Monsters);Effect Monster Cards 的 Card Description 上描述了该效果怪兽的效果。
而 Spell Cards 和 Trap Cards 就比较简单,仅包含了:
-
Name(魔法卡或者陷阱卡的名字)
-
Category(魔法卡或者陷阱卡的类型)
- Card Description(卡片描述)

3. <CARDS>...</CARDS>
接下来,我要为这些卡片信息建立 XML 表示模型。首先是怪兽卡的 XML 表示模型:
<monstercardimg="
"category="
"name="
"attribute="
"level="
"type="
"atk="
"def="
">
</monstercard>
其中,
- img:指示该怪兽卡的图片位置。
- category:指示该怪兽卡的类别。允许的值为 Normal、Effect、Fusion 或者 Ritual,分别表示普通怪兽、效果怪兽、融合怪兽或者仪式怪兽。虽然融合怪兽或者仪式怪兽也可以带有效果,但在正式的卡片归类里,这些怪兽将归入融合怪兽或者仪式怪兽类别。例如 Thousand-Eye Restrict 就既是融合怪兽也是效果怪兽,却被归类到融合怪兽里。
- name:指示该怪兽卡的名字。
- attribute:指示该怪兽的属性,允许的值如上面的 Attribute 枚举所示。
- type:指示该怪兽的类型,允许的值如上面的 type 枚举所示。
- level:指示该怪兽的等级,对应着卡片右上方的星星数目,其值为整数类型。
- atk:指示怪兽的攻击力,其值为整数类型。
- def:指示怪兽的防守力,其值为整数类型。
- 最后,mostercard 元素的内容则为卡片描述内容。
类似地,魔法卡的表示为:
<spellcardimg="
"category="
"name="
">
</spellcard>
其中,
- img:指示该魔法卡的图片位置。
- category:指示该魔法卡的类别。允许的值为 Normal、Continuous、Equip、Field、QuickPlay 或者 Ritual,分别表示普通魔法卡、永续魔法卡、装备魔法卡、场地魔法卡、速攻魔法卡或者仪式魔法卡。
- name:指示该魔法卡的名字。
陷阱卡的表示为:
<trapcardimg="
"category="
"name="
">
</trapcard>
其中,
- img:指示该陷阱卡的图片位置。
- category:指示该陷阱卡的类别。允许的值为 Normal、Counter 或者 Continuous,分别表示普通陷阱卡、反击陷阱卡或者永续陷阱卡。
- name:指示该陷阱卡的名字。
以下是一份示范代码:

sample.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
<?xmlversion="1.0"encoding="utf-8"?>
<cards>
<monstercards>
<monstercardimg="MonsterCards\BarrelDragon.jpg"category="Effect"name="BarrelDragon"attribute="Dark"level="7"type="Machine"atk="2600"def="2200">Tossacoin3times.If2outof3resultsareHeads,destroy1monsteronyouropponent'ssideofthefield.Thiscard'seffectcanonlybeusedduringyourownturn,onceperturn.</monstercard>
<monstercardimg="MonsterCards\Blue-EyesWhiteDragon.jpg"category="Normal"name="Blue-EyesWhiteDragon"attribute="Light"level="8"type="Dragon"atk="3000"def="2500">Thislegendarydragonisapowerfulengineofdestruction.Virtuallyinvincible,veryfewhavefacedthisawesomecreatureandlivedtotellthetale.</monstercard>
<monstercardimg="MonsterCards\Thousand-EyeRestrict.jpg"category="Fusion"name="Thousand-EyeRestrict"attribute="Dark"level="1"type="Spellcaster"atk="0"def="0">"Relinquished"+"Thousand-EyesIdol".Aslongasthiscardremainsface-uponthefield,othermonsterscannotchangetheirpositionsorattack.ThismonstercantakeontheATKandDEFof1opponent'smonsteronthefield(aface-downmonsterresultsinanATKandDEFof0).TreattheselectedmonsterasanEquipSpellCardanduseittoequip"Thousand-EyesRestrict".Youmayusethiseffectonlyonceperturnandcanequip"Thousand-EyesRestrict"withonly1monsteratatime.</monstercard>
<monstercardimg="MonsterCards\MagicianofBlackChaos.jpg"category="Ritual"name="MagicianofBlackChaos"attribute="Dark"level="8"type="Spellcaster"atk="2800"def="2600">ThismonstercanonlybeRitualSommonedwiththeRitualSpellCard,"DarkMagicRitual".YoumustalsooffermonsterswhosetotalLevelStarsequal8ormoreasaTributefromthefieldoryourhand.</monstercard>
</monstercards>
<spellcards>
<spellcardimg="SpellCards\DarkHole.jpg"category="Normal"name="DarkHole">Destroysallmonstersonthefield.</spellcard>
</spellcards>
<trapcards>
<trapcardimg="TrapCards\WhiteHole.jpg"category="Normal"name="WhiteHole">Whenyouropponentplays"DarkHole",themonstersonyoursideofthefieldarenotdestroyed.</trapcard>
</trapcards>
</cards>
4. CLASS CARD { ... }
首先,我定义一个 Card 的抽象了,里面有一个 ToXElement 的抽象方法:
//Code#01

abstractclassCard


{
publicabstractXElementToXElement();

//Othercodeomitted
}
接下来,我们来看看 MonsterCard 的 ToXElement 实现和其中一个构造函数:
//Code#02

classMonsterCard:Card


{
publicMonsterCard(XElementinfo)


{
m_ImageLocation=(string)info.Attribute("img");
m_Name=(string)info.Attribute("name");
m_Category=(MonsterCardCategory)Enum.Parse(typeof(MonsterCardCategory),(string)info.Attribute("category"),false);
m_Attribute=(MonsterAttribute)Enum.Parse(typeof(MonsterAttribute),(string)info.Attribute("attribute"),false);
m_Type=(MonsterType)Enum.Parse(typeof(MonsterAttribute),(string)info.Attribute("type"),false);
m_Level=Int32.Parse((string)info.Attribute("level"));
m_Atk=Int32.Parse((string)info.Attribute("atk"));
m_Def=Int32.Parse((string)info.Attribute("def"));
m_Description=(string)info.Value;
}

publicoverrideXElementToXElement()


{
returnnewXElement("monstercard",
newXAttribute("img",m_ImageLocation),
newXAttribute("category",m_Category.ToString()),
newXAttribute("name",m_Name),
newXAttribute("attribute",m_Attribute.ToString()),
newXAttribute("level",m_Level.ToString()),
newXAttribute("type",m_Type.ToString()),
newXAttribute("atk",m_Atk.ToString()),
newXAttribute("def",m_Def.ToString()),
m_Description);
}

//Othercodeomitted
}
在 ToXElement 中,我们使用了 Functional Construction 方式来创建 XML,它调用了 XElement 的
//Code#03

publicXElement(XNamename,paramsobject[]contents)
XElement 懂得处理所有类型的输入参数,以下摘自 XLinq Overview:
-
A string, which is added as text content.
-
An XElement, which is added as a child element.
-
An XAttribute, which is added as an attribute.
-
An XProcessingInstruction, XComment, or XCData, which is added as child content.
-
An IEnumerable, which is enumerated, and these rules are applied recursively.
-
Anything else, ToString() is called and the result is added as text content.
- null, which is ignored.
从这里,我们可以看出,XLing 使得无论读取或者创建 XML 都变得比以前更为便捷了。
5. GIVE ME WHAT I WANT!
现在我们来看这张卡:

这张卡的效果是禁止场上所有等级4或以上的怪兽攻击,于是,我希望能够找到等级3或者以下,并具有能直接攻击对手的效果的怪兽:
//Code#04

XElementcards=XElement.Load(@"E:\MyDocuments\VisualStudio\Projects\LINQLab\Sample.xml");
varwanted=fromcincards.Element("monstercards").Elements("monstercard")
where(Int32.Parse((string)c.Attribute("level"))<4)&&((string)c.Attribute("category")=="Effect")
orderbyInt32.Parse((string)c.Attribute("atk"))
selectc;
foreach(varcinwanted)


{
Console.WriteLine((string)c.Attribute("name"));
Console.WriteLine("[{0}]",(string)c.Attribute("type"));
Console.WriteLine(c.Value);
Console.WriteLine("ATK/{0}DEF/{1}",(string)c.Attribute("atk"),(string)c.Attribute("def"));
}

//Output:
//
//Jinzo#7
//[Machine]
//Thismonstermayattackyouropponent'sLifePointsdirectly.
//ATK/500DEF/400
//
//Man-EaterBug
//[Insect]
//FLIP:Destroys1monsteronthefield(regardlessofposition).
//ATK/450DEF/600
由于 Positron 没有足够的能力分析卡片上的描述,所以我只好把第二个条件改为“具有效果的怪兽”,让 Positron 做第一轮筛选。如果筛选的结果显示出多个选择,那么毫无疑问我会选择攻击力高的,所以我让 Positron 根据怪兽的攻击力进行排序。从筛选结果中,我们可以看到 Jinzo #7 就是我要找的怪兽了。

当然,如果你已经把 XML 文件的数据映射到一组 MonsterCard 类(例如,List<MonsterCard> cards),你也可以这样:
//Code#05

varwanted=fromcincards
where(c.Level<4)&&(c.Category==MonsterCardCategory.Effect)
orderbyc.Atk
selectc;

foreach(varcinwanted)


{
Console.WriteLine(c.Name);
Console.WriteLine("[{0}]",c.Type.ToString());
Console.WriteLine(c.Description);
Console.WriteLine("ATK/{0}DEF/{1}",c.Atk.ToString(),c.Def.ToString());
}
6. SHOW ME THE RESULTS.
现在假设我已经筛选了一批后备卡片,并储存在一个 XElement 中,我希望把结果以 HTML 方式展示出来,那我有该如何处理呢?答案是用 Functional Construction 方式,把 HTML 当作 XML 来处理。
假设我需要把上面那份 sample.xml 转换为如下的 HTML 文件:

sample.html
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
<html>
<head>
<title>CardList</title>
</head>
<body>
<h2>
<fontcolor="#0000FF">MonsterCards</font>
</h2>
<p>
<imgsrc="MonsterCards\BarrelDragon.png"/>
</p>
<p>BarrelDragonMachine</p>
<p>Tossacoin3times.If2outof3resultsareHeads,destroy1monsteronyouropponent'ssideofthefield.Thiscard'seffectcanonlybeusedduringyourownturn,onceperturn.</p>
<p>ATK/2600DEF/2200</p>
<p>
<imgsrc="MonsterCards\Blue-EyesWhiteDragon.png"/>
</p>
<p>Blue-EyesWhiteDragonDragon</p>
<p>Thislegendarydragonisapowerfulengineofdestruction.Virtuallyinvincible,veryfewhavefacedthisawesomecreatureandlivedtotellthetale.</p>
<p>ATK/3000DEF/2500</p>
<p>
<imgsrc="MonsterCards\Thousand-EyeRestrict.png"/>
</p>
<p>Thousand-EyeRestrictSpellcaster</p>
<p>"Relinquished"+"Thousand-EyesIdol".Aslongasthiscardremainsface-uponthefield,othermonsterscannotchangetheirpositionsorattack.ThismonstercantakeontheATKandDEFof1opponent'smonsteronthefield(aface-downmonsterresultsinanATKandDEFof0).TreattheselectedmonsterasanEquipSpellCardanduseittoequip"Thousand-EyesRestrict".Youmayusethiseffectonlyonceperturnandcanequip"Thousand-EyesRestrict"withonly1monsteratatime.</p>
<p>ATK/0DEF/0</p>
<p>
<imgsrc="MonsterCards\MagicianofBlackChaos.png"/>
</p>
<p>MagicianofBlackChaosSpellcaster</p>
<p>ThismonstercanonlybeRitualSommonedwiththeRitualSpellCard,"DarkMagicRitual".YoumustalsooffermonsterswhosetotalLevelStarsequal8ormoreasaTributefromthefieldoryourhand.</p>
<p>ATK/2800DEF/2600</p>
<h2>
<fontcolor="#0000FF">SpellCards</font>
</h2>
<p>
<imgsrc="SpellCards\DarkHole.png"/>
</