YU-GI-OH! POWER OF XLINQ [C#, XLINQ, XML]

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 系列游戏就内置了一个简单的卡片信息管理器。

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(植物族)

Deepsea Warrior (Effect Monster Card)

上图所展示的是一张效果怪兽卡,它包含如下信息:

  • 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(卡片描述)

Dark Hole (Normal Spell Card)

3. <CARDS>...</CARDS>

接下来,我要为这些卡片信息建立 XML 表示模型。首先是怪兽卡的 XML 表示模型:

None.gif < monstercard img ="dot.gif" category ="dot.gif" name ="dot.gif" attribute ="dot.gif" level ="dot.gif" type ="dot.gif" atk ="dot.gif" def ="dot.gif" > dot.gif </ monstercard >

其中,

  • img:指示该怪兽卡的图片位置。
  • category:指示该怪兽卡的类别。允许的值为 Normal、Effect、Fusion 或者 Ritual,分别表示普通怪兽、效果怪兽、融合怪兽或者仪式怪兽。虽然融合怪兽或者仪式怪兽也可以带有效果,但在正式的卡片归类里,这些怪兽将归入融合怪兽或者仪式怪兽类别。例如 Thousand-Eye Restrict 就既是融合怪兽也是效果怪兽,却被归类到融合怪兽里。
  • name:指示该怪兽卡的名字。
  • attribute:指示该怪兽的属性,允许的值如上面的 Attribute 枚举所示。
  • type:指示该怪兽的类型,允许的值如上面的 type 枚举所示。
  • level:指示该怪兽的等级,对应着卡片右上方的星星数目,其值为整数类型。
  • atk:指示怪兽的攻击力,其值为整数类型。
  • def:指示怪兽的防守力,其值为整数类型。
  • 最后,mostercard 元素的内容则为卡片描述内容。

类似地,魔法卡的表示为:

None.gif < spellcard img ="dot.gif" category ="dot.gif" name ="dot.gif" > dot.gif </ spellcard >

其中,

  • img:指示该魔法卡的图片位置。
  • category:指示该魔法卡的类别。允许的值为 Normal、Continuous、Equip、Field、QuickPlay 或者 Ritual,分别表示普通魔法卡、永续魔法卡、装备魔法卡、场地魔法卡、速攻魔法卡或者仪式魔法卡。
  • name:指示该魔法卡的名字。

陷阱卡的表示为:

None.gif < trapcard img ="dot.gif" category ="dot.gif" name ="dot.gif" > dot.gif </ trapcard >

其中,

  • img:指示该陷阱卡的图片位置。
  • category:指示该陷阱卡的类别。允许的值为 Normal、Counter 或者 Continuous,分别表示普通陷阱卡、反击陷阱卡或者永续陷阱卡。
  • name:指示该陷阱卡的名字。

以下是一份示范代码:

ContractedBlock.gif ExpandedBlockStart.gif sample.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->None.gif<?xmlversion="1.0"encoding="utf-8"?>
None.gif
<cards>
None.gif
<monstercards>
None.gif
<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>
None.gif
<monstercardimg="MonsterCards\Blue-EyesWhiteDragon.jpg"category="Normal"name="Blue-EyesWhiteDragon"attribute="Light"level="8"type="Dragon"atk="3000"def="2500">Thislegendarydragonisapowerfulengineofdestruction.Virtuallyinvincible,veryfewhavefacedthisawesomecreatureandlivedtotellthetale.</monstercard>
None.gif
<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>
None.gif
<monstercardimg="MonsterCards\MagicianofBlackChaos.jpg"category="Ritual"name="MagicianofBlackChaos"attribute="Dark"level="8"type="Spellcaster"atk="2800"def="2600">ThismonstercanonlybeRitualSommonedwiththeRitualSpellCard,"DarkMagicRitual".YoumustalsooffermonsterswhosetotalLevelStarsequal8ormoreasaTributefromthefieldoryourhand.</monstercard>
None.gif
</monstercards>
None.gif
<spellcards>
None.gif
<spellcardimg="SpellCards\DarkHole.jpg"category="Normal"name="DarkHole">Destroysallmonstersonthefield.</spellcard>
None.gif
</spellcards>
None.gif
<trapcards>
None.gif
<trapcardimg="TrapCards\WhiteHole.jpg"category="Normal"name="WhiteHole">Whenyouropponentplays"DarkHole",themonstersonyoursideofthefieldarenotdestroyed.</trapcard>
None.gif
</trapcards>
None.gif
</cards>

4. CLASS CARD { ... }

首先,我定义一个 Card 的抽象了,里面有一个 ToXElement 的抽象方法:

None.gif // Code#01
None.gif

None.gif
abstract class Card
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
publicabstractXElementToXElement();
InBlock.gif
InBlock.gif
//Othercodeomitteddot.gif
ExpandedBlockEnd.gif
}

接下来,我们来看看 MonsterCard 的 ToXElement 实现和其中一个构造函数:

None.gif // Code#02
None.gif

None.gif
class MonsterCard:Card
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
publicMonsterCard(XElementinfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_ImageLocation
=(string)info.Attribute("img");
InBlock.gifm_Name
=(string)info.Attribute("name");
InBlock.gifm_Category
=(MonsterCardCategory)Enum.Parse(typeof(MonsterCardCategory),(string)info.Attribute("category"),false);
InBlock.gifm_Attribute
=(MonsterAttribute)Enum.Parse(typeof(MonsterAttribute),(string)info.Attribute("attribute"),false);
InBlock.gifm_Type
=(MonsterType)Enum.Parse(typeof(MonsterAttribute),(string)info.Attribute("type"),false);
InBlock.gifm_Level
=Int32.Parse((string)info.Attribute("level"));
InBlock.gifm_Atk
=Int32.Parse((string)info.Attribute("atk"));
InBlock.gifm_Def
=Int32.Parse((string)info.Attribute("def"));
InBlock.gifm_Description
=(string)info.Value;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
publicoverrideXElementToXElement()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnnewXElement("monstercard",
InBlock.gif
newXAttribute("img",m_ImageLocation),
InBlock.gif
newXAttribute("category",m_Category.ToString()),
InBlock.gif
newXAttribute("name",m_Name),
InBlock.gif
newXAttribute("attribute",m_Attribute.ToString()),
InBlock.gif
newXAttribute("level",m_Level.ToString()),
InBlock.gif
newXAttribute("type",m_Type.ToString()),
InBlock.gif
newXAttribute("atk",m_Atk.ToString()),
InBlock.gif
newXAttribute("def",m_Def.ToString()),
InBlock.gifm_Description);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Othercodeomitteddot.gif
ExpandedBlockEnd.gif
}

在 ToXElement 中,我们使用了 Functional Construction 方式来创建 XML,它调用了 XElement 的

None.gif // Code#03
None.gif

None.gif
public XElement(XNamename, params object []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!

现在我们来看这张卡:

Gravity Bind (Continuous Trap Card)

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

None.gif // Code#04
None.gif

None.gifXElementcards
= XElement.Load( @" E:\MyDocuments\VisualStudio\Projects\LINQLab\Sample.xml " );
None.gifvarwanted
= fromc in cards.Element( " monstercards " ).Elements( " monstercard " )
None.gifwhere(Int32.Parse((
string )c.Attribute( " level " )) < 4 ) && (( string )c.Attribute( " category " ) == " Effect " )
None.giforderbyInt32.Parse((
string )c.Attribute( " atk " ))
None.gifselectc;
None.gif
foreach (varc in wanted)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifConsole.WriteLine((
string)c.Attribute("name"));
InBlock.gifConsole.WriteLine(
"[{0}]",(string)c.Attribute("type"));
InBlock.gifConsole.WriteLine(c.Value);
InBlock.gifConsole.WriteLine(
"ATK/{0}DEF/{1}",(string)c.Attribute("atk"),(string)c.Attribute("def"));
ExpandedBlockEnd.gif}

None.gif
None.gif
// Output:
None.gif
//
None.gif
// Jinzo#7
None.gif
// [Machine]
None.gif
// Thismonstermayattackyouropponent'sLifePointsdirectly.
None.gif
// ATK/500DEF/400
None.gif
//
None.gif
// Man-EaterBug
None.gif
// [Insect]
None.gif
// FLIP:Destroys1monsteronthefield(regardlessofposition).
None.gif
// ATK/450DEF/600

由于 Positron 没有足够的能力分析卡片上的描述,所以我只好把第二个条件改为“具有效果的怪兽”,让 Positron 做第一轮筛选。如果筛选的结果显示出多个选择,那么毫无疑问我会选择攻击力高的,所以我让 Positron 根据怪兽的攻击力进行排序。从筛选结果中,我们可以看到 Jinzo #7 就是我要找的怪兽了。

Jinzo #7 (Effect Monster)

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

None.gif // Code#05
None.gif

None.gifvarwanted
= fromc in cards
None.gifwhere(c.Level
< 4 ) && (c.Category == MonsterCardCategory.Effect)
None.giforderbyc.Atk
None.gifselectc;
None.gif
None.gif
foreach (varc in wanted)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifConsole.WriteLine(c.Name);
InBlock.gifConsole.WriteLine(
"[{0}]",c.Type.ToString());
InBlock.gifConsole.WriteLine(c.Description);
InBlock.gifConsole.WriteLine(
"ATK/{0}DEF/{1}",c.Atk.ToString(),c.Def.ToString());
ExpandedBlockEnd.gif}

6. SHOW ME THE RESULTS.

现在假设我已经筛选了一批后备卡片,并储存在一个 XElement 中,我希望把结果以 HTML 方式展示出来,那我有该如何处理呢?答案是用 Functional Construction 方式,把 HTML 当作 XML 来处理。

假设我需要把上面那份 sample.xml 转换为如下的 HTML 文件:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值