DemoStrategy的源代码
宣讲会展示的那个弱爆了的AI源代码。该AI只演示如何实现接口,直接参战必败无疑:)---------------------我是分割线---------------------
using GrapCity.Competition.CastleRush.Ai;
using GrapCity.Competition.CastleRush.Ai.View;
using System;
namespace AiDemo.FreeGuy
{
/// <summary>
/// 演示用AI程序:随机前进,逢矿必占
/// </summary>
public class DemoStrategy : ICastleStrategy
{
#region ICastleStrategy Members
/// <summary>
/// 策略的唯一标识,建议使用GUID
/// </summary>
/// <value>The id.</value>
/// <remarks></remarks>
/// <history>
/// 2012/01/13 16:08 Created
/// </history>
/// <remarks></remarks>
/// <history>
/// 2012/02/03 9:55 Created
/// </history>
public Guid Id
{
get { return new Guid("A3ABA1A3-F2E9-4414-ADDB-1D8217867B03"); }
}
/// <summary>
/// 选手名
/// </summary>
/// <value>The name of the friendly.</value>
/// <remarks></remarks>
/// <history>
/// 2012/01/13 17:37 Created
/// </history>
/// <remarks></remarks>
/// <history>
/// 2012/02/03 9:55 Created
/// </history>
public string FriendlyName
{
get { return "酱油党之随便转转"; }
}
public void Prefetch(MapView map)
{
//什么都不做
}
private int rectuiIndex = 0;
public RecruitKnightResult RecruitKnight(MapView map, CastleView castle)
{
rectuiIndex++;
return new RecruitKnightResult() { AttackPower = 1, MarchPower = 5, Name = "等待机会_" + rectuiIndex.ToString() };
}
public KnightDecision KnightDecision(MapView map, CastleView castle, KnightView activeKnight, int stepIndex)
{
// 先看是否可以占矿
if (activeKnight.Position.X + 1 < map.Map.GetLength(0) && map.Map.GetItemType() == ItemType.Mine && !(map.Map as MineView).IsMyUnit && (map.Map as MineView).Reserves > 0)
{
return new KnightDecision() { Action = ActionType.占领, Direction = Direction.右 };
}
if (activeKnight.Position.X - 1 > -1 && map.Map.GetItemType() == ItemType.Mine && !(map.Map as MineView).IsMyUnit && (map.Map as MineView).Reserves > 0)
{
return new KnightDecision() { Action = ActionType.占领, Direction = Direction.左 };
}
if (activeKnight.Position.Y + 1 < map.Map.GetLength(1) && map.Map.GetItemType() == ItemType.Mine && !(map.Map as MineView).IsMyUnit && (map.Map as MineView).Reserves > 0)
{
return new KnightDecision() { Action = ActionType.占领, Direction = Direction.下 };
}
if (activeKnight.Position.Y - 1 > -1 && map.Map.GetItemType() == ItemType.Mine && !(map.Map as MineView).IsMyUnit && (map.Map as MineView).Reserves > 0)
{
return new KnightDecision() { Action = ActionType.占领, Direction = Direction.上 };
}
// 再看是否可以攻城
if (activeKnight.Position.X + 1 < map.Map.GetLength(0) && map.Map.GetItemType() == ItemType.Caslte && !(map.Map as CastleView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.攻城, Direction = Direction.右 };
}
if (activeKnight.Position.X - 1 > -1 && map.Map.GetItemType() == ItemType.Caslte && !(map.Map as CastleView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.攻城, Direction = Direction.左 };
}
if (activeKnight.Position.Y + 1 < map.Map.GetLength(1) && map.Map.GetItemType() == ItemType.Caslte && !(map.Map as CastleView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.攻城, Direction = Direction.下 };
}
if (activeKnight.Position.Y - 1 > -1 && map.Map.GetItemType() == ItemType.Caslte && !(map.Map as CastleView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.攻城, Direction = Direction.上 };
}
// 再看是否可以攻击
if (activeKnight.Position.X + 1 < map.Map.GetLength(0) && map.Map.GetItemType() == ItemType.Knight && !(map.Map as KnightView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.作战, Direction = Direction.右 };
}
if (activeKnight.Position.X - 1 > -1 && map.Map.GetItemType() == ItemType.Knight && !(map.Map as KnightView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.作战, Direction = Direction.左 };
}
if (activeKnight.Position.Y + 1 < map.Map.GetLength(1) && map.Map.GetItemType() == ItemType.Knight && !(map.Map as KnightView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.作战, Direction = Direction.下 };
}
if (activeKnight.Position.Y - 1 > -1 && map.Map.GetItemType() == ItemType.Knight && !(map.Map as KnightView).IsMyUnit)
{
return new KnightDecision() { Action = ActionType.作战, Direction = Direction.上 };
}
// 什么都不能做则随机行军
Direction dir = Direction.原地不动;
bool canGo = false;
Random ran = new Random();
while (!canGo)
{
int answer = ran.Next(4);
switch (answer)
{
case 0:
{
if (activeKnight.Position.X + 1 < map.Map.GetLength(0) && map.Map.GetItemType() == ItemType.Road)
{
canGo = true;
dir = Direction.右;
}
break;
}
case 1:
{
if (activeKnight.Position.X - 1 > -1 && map.Map.GetItemType() == ItemType.Road)
{
canGo = true;
dir = Direction.左;
}
break;
}
case 2:
{
if (activeKnight.Position.Y + 1 < map.Map.GetLength(1) && map.Map.GetItemType() == ItemType.Road)
{
canGo = true;
dir = Direction.下;
}
break;
}
case 3:
{
if (activeKnight.Position.Y - 1 > -1 && map.Map.GetItemType() == ItemType.Road)
{
canGo = true;
dir = Direction.上;
}
break;
}
case 4:
{
canGo = true;
dir = Direction.原地不动;
break;
}
}
}
return new KnightDecision() { Action = ActionType.行军, Direction = dir };
}
#endregion
}
} 回复 1楼Valentine的帖子
我想问问是不是一定不会出现两个城堡间不可到达的现象? 回复 2楼994572168_liu的帖子
可能会啊。
比如对方的骑士把要道封死的话,不杀掉这些堵路的骑士那就真的到达不了对方城堡了。 回复 3楼Valentine的帖子
嗯 就是原始地图上 两个城堡肯定是直接相通的? 回复 4楼994572168_liu的帖子
是的 你好,
目前我有几个疑问:
1.RecruitKnight和KnightDecision是否在每个会合都会被调用?
2.比赛开始时双方都是没有金矿的么?
3.如果2成立,就是说在占领金矿后,每个回合每个金矿会给自己带来一金币的收益?
……
嗯,目前想到的就是上面三个问题,希望能帮忙解答,谢谢!
演示代码的问题
回复 1楼Valentine的帖子运行时出现了“未将对象引用设置到对象实例”的错误。。。为什么这个代码会这样呢???? 回复 7楼wyg_0802的帖子
目标平台需要设置为3.5或更低(VS2010默认是4.0)。 回复 1楼Valentine的帖子
GetLength(0)不是返回第一维长度么,应该是纵坐标啊,楼主是不是写错了??? 回复 9楼Orzmand的帖子
这个,似乎是写错了。
但是不影响比赛啦:)
页:
[1]
2