Britbot
Fallback/FallbackBot.cs
Go to the documentation of this file.
00001 #region Usings
00002 
00003 using System;
00004 using System.Collections.Generic;
00005 using System.Diagnostics;
00006 using System.Linq;
00007 using System.Threading;
00008 using Pirates;
00009 
00010 #endregion
00011 
00012 namespace Britbot.Fallback
00013 {
00018     public class FallbackBot
00019     {
00023         private static List<Group> FallbackGroups;
00024 
00028         static FallbackBot()
00029         {
00030             FallbackBot.FallbackGroups = new List<Group>();
00031         }
00032 
00037         public static void UpdateFallbacks()
00038         {
00039             //TODO clone the groups inside Commander.Groups
00040         }
00041 
00047         public static Dictionary<Pirate, Direction> GetFallbackTurns(CancellationToken cToken)
00048         {
00049             try
00050             {
00051                 FallbackBot.FallbackGroups.RemoveAll(g => g.Pirates.Count == 0);
00052 
00053                 // Update all groups
00054                 FallbackBot.FallbackGroups.ForEach(g => g.Update());
00055 
00056                 //A list with all the moves from all groups
00057                 List<KeyValuePair<Pirate, Direction>> allMoves =
00058                     new List<KeyValuePair<Pirate, Direction>>(Bot.Game.AllMyPirates().Count);
00059 
00060                 //Get the moves from each group we have
00061                 foreach (Group group in FallbackBot.FallbackGroups)
00062                 {
00063                     List<KeyValuePair<Pirate, Direction>> mvs = group.GetGroupMoves(cToken).ToList();
00064                     allMoves.AddRange(mvs);
00065                 }
00066 
00067                 Logger.Write("===============FALLBACK READY=================");
00068 
00069                 //Convert the moves list to dictionary
00070                 return allMoves.ToDictionary(pair => pair.Key, pair => pair.Value);
00071             }
00072             catch (Exception ex)
00073             {
00074                 Logger.Write("======FALLBACK EXCEPTION=======",true);
00075                 Logger.Write("Fallback almost crashed because of exception: " + ex.Message, true);
00076                 
00077                 StackTrace exTrace = new StackTrace(ex, true);
00078                 StackFrame frame = exTrace.GetFrame(0);
00079                 Logger.Write(
00080                     string.Format("The exception was thrown from method {0} at file {1} at line #{2}", frame.GetMethod(),
00081                         frame.GetFileName(), frame.GetFileLineNumber()), true);
00082                 Logger.Write("======FALLBACK EXCEPTION=======", true);
00083 
00084                 return FallbackBot.SuperFuckedFallback();
00085             }
00086         }
00087 
00092         private static Dictionary<Pirate, Direction> SuperFuckedFallback()
00093         {
00094             Dictionary<Pirate, Direction> fallback = new Dictionary<Pirate, Direction>();
00095             foreach (Pirate pirate in Bot.Game.AllMyPirates())
00096             {
00097                 if (Bot.Game.GetTurn() % 2 == 0)
00098                     fallback.Add(pirate, Direction.NORTH);
00099                 else
00100                     fallback.Add(pirate, Direction.SOUTH);
00101             }
00102 
00103             return fallback;
00104         }
00105     }
00106 }