summaryrefslogtreecommitdiff
path: root/Entelect.BattleCity.Challenge/AiAgent.cs
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2013-10-14 19:33:05 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2013-10-14 19:33:05 +0200
commit26210d7b73f37e30d1b5ca88e2275fd01caf5d72 (patch)
treeadb848e49b7d55456ae3a86957f4c9f09eb68913 /Entelect.BattleCity.Challenge/AiAgent.cs
Initial commit with the Mono Rot Bot
Diffstat (limited to 'Entelect.BattleCity.Challenge/AiAgent.cs')
-rw-r--r--Entelect.BattleCity.Challenge/AiAgent.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/Entelect.BattleCity.Challenge/AiAgent.cs b/Entelect.BattleCity.Challenge/AiAgent.cs
new file mode 100644
index 0000000..e577bcb
--- /dev/null
+++ b/Entelect.BattleCity.Challenge/AiAgent.cs
@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+
+namespace Entelect.BattleCity.Challenge
+{
+ class AiAgent
+ {
+ private int _tankId;
+
+ public AiAgent()
+ {
+
+ }
+
+ public AiAgent(int tankId)
+ {
+ _tankId = tankId;
+ }
+
+ public Move GetBestMove(ChallengeService.state?[][] state, ChallengeService.game game, int tankIndex)
+ {
+ ChallengeService.player me = null;
+ ChallengeService.player enemy = null;
+ ChallengeService.unit tank = null;
+ bool bulletInAir = false;
+
+ string playerName = game.playerName;
+ foreach (ChallengeService.player player in game.players)
+ {
+ if (player.name.Equals(playerName))
+ {
+ me = player;
+ }
+ else
+ {
+ enemy = player;
+ }
+ }
+ if (me.units.Length <= tankIndex)
+ {
+ return null;
+ }
+ tank = me.units[tankIndex];
+
+ if (me.bullets != null)
+ {
+ foreach (var bullet in me.bullets)
+ {
+ if (Math.Abs(bullet.x - tank.x) < state.Length / 4)
+ {
+ bulletInAir = true;
+ }
+ }
+ }
+
+ var enemyBase = enemy.@base;
+
+ ChallengeService.direction chosenDirection =
+ tank.y != enemyBase.y ?
+ (
+ tank.y > enemyBase.y ?
+ ChallengeService.direction.UP :
+ ChallengeService.direction.DOWN
+ ) :
+ (
+ tank.x > enemyBase.x ?
+ ChallengeService.direction.LEFT :
+ ChallengeService.direction.RIGHT
+ );
+
+ if (chosenDirection != tank.direction || bulletInAir)
+ {
+ return MoveInDirection(tank.id, chosenDirection);
+ }
+ else
+ {
+ return new Move(tank.id, ChallengeService.action.FIRE);
+ }
+ }
+
+ public Move MoveInDirection(int tankId, ChallengeService.direction direction)
+ {
+ switch (direction)
+ {
+ case ChallengeService.direction.UP:
+ return new Move(tankId, ChallengeService.action.UP);
+ case ChallengeService.direction.DOWN:
+ return new Move(tankId, ChallengeService.action.DOWN);
+ case ChallengeService.direction.LEFT:
+ return new Move(tankId, ChallengeService.action.LEFT);
+ case ChallengeService.direction.RIGHT:
+ return new Move(tankId, ChallengeService.action.RIGHT);
+ default:
+ return new Move(tankId, ChallengeService.action.NONE);
+ }
+ }
+ }
+}