Commits

Alexander Regueiro  committed fd6d3d8

Random bot working.

  • Participants
  • Parent commits 33ce03f

Comments (0)

Files changed (10)

 #!/bin/sh
 
-mono --runtime=v2.0.50727 ./bin/Release/Icfp2011.Icrs.Player.exe ./bin/Release/Icfp2011.Icrs.Bots.RandomBot.dll $@
+mono --runtime=v2.0.50727 ./bin/Release/Icfp2011.Icrs.Player.exe ./bin/Release/Icfp2011.Icrs.Bots.FinalBot.dll $@

File src/Bots/Icfp2011.Icrs.FinalBot/FinalBot.cs

 
         private LtgAction? lastOppAction;
 
+        private LtgSlot[] propSlots;
+        private LtgSlot[] oppSlots;
+
+        private int[] propSlotsUsages;
+        private int[] oppSlotsUsages;
+
         public FinalBot()
         {
         }
 
         public override void Start()
         {
-            //
+            this.propSlots = this.Match.GetSlots(this.Player);
+            this.oppSlots = this.Match.GetSlots(this.OtherPlayer);
         }
 
         public override LtgAction DoAction()
         {
             this.lastOppAction = action;
         }
+
+        private int GetAliveSlotCount(LtgPlayer player)
+        {
+            var count = 0;
+            var slots = this.Match.GetSlots(player);
+            for (int i = 0; i < slots.Length; i++)
+            {
+                if (slots[i].IsAlive)
+                    count++;
+            }
+            return count;
+        }
+
+        private int GetSlotSize(LtgSlot slot)
+        {
+            return GetSlotSize(this.Match.Clone(), slot.Clone().Value);
+        }
+
+        private int GetSlotSize(LtgMatch match, LtgValue val)
+        {
+            var slotSize = 0;
+
+            var testMatch = match;
+            var evaluator = new LtgFunction((depth, propSlots, oppSlots, arg, reverseMode) =>
+            {
+                var func = arg as LtgFunction;
+                slotSize++;
+                if (func != null && func.Arguments != null)
+                {
+                    foreach (var a in func.Arguments)
+                    {
+                        slotSize += GetSlotSize(match, a);
+                    }
+                }
+                return LtgCards.ICombinator;
+            });
+
+            var evalDepth = 0;
+            var success = evaluator.Evaluate(ref evalDepth,
+                testMatch.GetProponentSlots(), testMatch.GetOpponentSlots(), val, false);
+            return slotSize;
+        }
     }
 }

File src/Bots/Icfp2011.Icrs.RandomBot/RandomBot.cs

 
         public RandomBot()
         {
-            this.rnd = new Random(unchecked(Environment.TickCount * 33 + Environment.TickCount));
+            this.rnd = new Random();
         }
 
         public override void Start()

File src/Icfp2011.Icrs.Arbiter/Program.cs

 {
     internal static class Program
     {
+        private const string playerProgramPath = "Icfp2011.Icrs.Player.exe";
+
         private static readonly string initialFuncText = LtgCards.ICombinator.ToName();
 
         private static bool hasVsHost;
                 hasVsHost = Process.GetCurrentProcess().MainModule.FileName.Contains(".vshost");
 
 #if DEBUG
-                Console.BufferHeight = 0xFFF;
-                Console.WindowHeight = 50;
+                try
+                {
+                    Console.BufferHeight = 0xFFF;
+                    Console.WindowHeight = 50;
+                }
+                catch (IOException)
+                { }
 #endif
                 Console.Out.NewLine = "\n";
 
                 var optionArgs = args.Where(a => a.StartsWith("-")).ToArray();
                 var freeArgs = args.Except(optionArgs).ToArray();
 
-                lightInterface = optionArgs.Contains("-light", StringComparer.InvariantCultureIgnoreCase);
-
                 if (freeArgs.Length == 0)
                 {
-                    RunMatch();
+                    lightInterface = optionArgs.Contains("-light", StringComparer.InvariantCultureIgnoreCase);
+                    var alternatePlayers = optionArgs.Contains("-alt", StringComparer.InvariantCultureIgnoreCase);
+                    RunMatch(alternatePlayers);
                 }
                 else if (freeArgs.Length == 1)
                 {
-                    RunMatch(args[0]);
+                    RunMatch(freeArgs[0]);
                 }
                 else if (freeArgs.Length == 2)
                 {
-                    RunMatch(args[0], args[1]);
+                    RunMatch(freeArgs[0], freeArgs[1]);
                 }
                 else
                 {
             }
         }
 
-        public static void RunMatch()
+        public static void RunMatch(bool alternatePlayers)
         {
             try
             {
                         try
                         {
                             DoAction(match, Console.In, false);
+                            if (alternatePlayers)
+                            {
+                                DoAction(match, Console.In, false);
+                                match.SwitchPlayerTurn();
+                                DoAction(match, Console.In, false);
+                                match.SwitchPlayerTurn();
+                            }
                         }
                         catch (FormatException)
                         {
             }
         }
 
-        public static void RunMatch(string playerProgramPath)
+        public static void RunMatch(string botAProgramPath)
         {
             try
             {
-                using (var playerAProcess = CreatePlayerProcess(playerProgramPath, 0))
+                using (var playerAProcess = CreatePlayerProcess(playerProgramPath, botAProgramPath, 0))
                 {
                     var match = new LtgMatch();
                     StartMatch(match);
             }
         }
 
-        public static void RunMatch(string player1ProgramPath, string player2ProgramPath)
+        public static void RunMatch(string botAProgramPath, string botBProgramPath)
         {
             try
             {
-                using (var playerAProcess = CreatePlayerProcess(player1ProgramPath, LtgPlayer.PlayerA))
-                using (var playerBProcess = CreatePlayerProcess(player2ProgramPath, LtgPlayer.PlayerB))
+                using (var playerAProcess = CreatePlayerProcess(playerProgramPath, botAProgramPath,
+                    LtgPlayer.PlayerA))
+                using (var playerBProcess = CreatePlayerProcess(playerProgramPath, botBProgramPath,
+                    LtgPlayer.PlayerB))
                 {
                     var match = new LtgMatch();
                     StartMatch(match);
             match.StartPlayerTurn();
 
             // Read card action from input stream and perform it.
-            var action = ReadCardAction(reader, echoInput);
-            var actionSlot = propSlots[action.SlotIndex];
             LtgException result;
-            actionSlot.DoAction(propSlots, oppSlots, action, out result);
-            if (action.ApplyOrder == LtgCardApplyOrder.Left)
+            var action = new LtgAction();
+            var actionDone = false;
+            try
             {
-                if (!lightInterface)
+                action = ReadCardAction(reader, echoInput);
+                actionDone = true;
+                var actionSlot = propSlots[action.SlotIndex];
+                actionSlot.DoAction(propSlots, oppSlots, action, out result);
+                if (action.ApplyOrder == LtgCardApplyOrder.Left)
                 {
-                    Console.Out.WriteLine("player {0} applied card {1} to slot {2}",
-                        (int)match.CurrentPlayer, action.Card.ToName(), action.SlotIndex);
+                    if (!lightInterface)
+                    {
+                        Console.Out.WriteLine("player {0} applied card {1} to slot {2}",
+                            (int)match.CurrentPlayer, action.Card.ToName(), action.SlotIndex);
+                    }
+                }
+                else if (action.ApplyOrder == LtgCardApplyOrder.Right)
+                {
+                    if (!lightInterface)
+                    {
+                        Console.Out.WriteLine("player {0} applied slot {2} to card {1}",
+                            (int)match.CurrentPlayer, action.Card.ToName(), action.SlotIndex);
+                    }
+                }
+                else
+                {
+                    throw new InvalidOperationException("Invalid card application order.");
                 }
             }
-            else if (action.ApplyOrder == LtgCardApplyOrder.Right)
+            catch (FormatException)
             {
-                if (!lightInterface)
-                {
-                    Console.Out.WriteLine("player {0} applied slot {2} to card {1}",
-                        (int)match.CurrentPlayer, action.Card.ToName(), action.SlotIndex);
-                }
-            }
-            else
-            {
-                throw new InvalidOperationException("Invalid card application order.");
+                result = new LtgException(LtgExceptionType.UnknownAction);
             }
 
             if (lightInterface)
                 else
                 {
                     Console.Out.WriteLine("Exception: {0}", GetLtgExceptionName(result.ErrorType));
-                    Console.Out.WriteLine("slot {0} reset to {1}", action.SlotIndex, initialFuncText);
+                    if (actionDone)
+                        Console.Out.WriteLine("slot {0} reset to {1}", action.SlotIndex, initialFuncText);
                 }
             }
         }
         {
             switch (type)
             {
+                case LtgExceptionType.UnknownAction:
+                    return "Failure";
                 case LtgExceptionType.Overflow:
                     return "Native.AppLimitExceeded";
                 default:
             var sb = new StringBuilder();
 
             var testMatch = match;
-            var evalDepth = 0;
             var evaluator = new LtgFunction((depth, propSlots, oppSlots, arg, reverseMode) =>
                 {
                     var func = arg as LtgFunction;
                     return LtgCards.ICombinator;
                 });
 
+            var evalDepth = 0;
             var success = evaluator.Evaluate(ref evalDepth,
                 testMatch.GetProponentSlots(), testMatch.GetOpponentSlots(), val, false);
             return sb.ToString();
                         Console.Out.WriteLine("slot no?");
                     action.SlotIndex = int.Parse(ReadValue(reader, echoInput));
                 };
-            Action readCardNumber = () =>
+            Action readCardName = () =>
                 {
                     if (!lightInterface)
                         Console.Out.WriteLine("card name?");
             action.ApplyOrder = (LtgCardApplyOrder)int.Parse(ReadValue(reader, echoInput));
             if (action.ApplyOrder == LtgCardApplyOrder.Left || lightInterface)
             {
-                readCardNumber();
+                readCardName();
                 readSlotNumber();
             }
             else if (action.ApplyOrder == LtgCardApplyOrder.Right)
             {
                 readSlotNumber();
-                readCardNumber();
+                readCardName();
             }
             else
             {
             return value;
         }
 
-        private static Process CreatePlayerProcess(string path, LtgPlayer player)
+        private static Process CreatePlayerProcess(string path, string botPath, LtgPlayer player)
         {
             var procStartInfo = new ProcessStartInfo(path);
-            procStartInfo.Arguments = ((int)player).ToString();
+            procStartInfo.Arguments = string.Format("{0} {1}", botPath, ((int)player));
             procStartInfo.UseShellExecute = false;
             procStartInfo.RedirectStandardInput = true;
             procStartInfo.RedirectStandardOutput = true;

File src/Icfp2011.Icrs.Bot/LtgBot.cs

             set;
         }
 
+        public LtgPlayer OtherPlayer
+        {
+            get { return this.Match.GetOtherPlayer(this.Player); }
+        }
+
         public abstract void Start();
 
         public abstract LtgAction DoAction();

File src/Icfp2011.Icrs.Player/Program.cs

 
                 Console.Out.NewLine = "\n";
 
-                if (args.Length < 2)
+                var optionArgs = args.Where(a => a.StartsWith("-")).ToArray();
+                var freeArgs = args.Except(optionArgs).ToArray();
+
+                if (freeArgs.Length < 2)
                 {
 #if DEBUG
                     Console.Error.WriteLine(Properties.Settings.Default.MessageNotEnoughArgs);
                     return Properties.Settings.Default.ExitCodeNotEnoughArgs;
                 }
 
-                var botProgramPath = args[0];
-                LoadBot(botProgramPath); ;
+                var botProgramPath = freeArgs[0];
+                LoadBot(botProgramPath);
 
-                var programPlayer = (LtgPlayer)int.Parse(args[1]);
-                RunPlayer(programPlayer);
+                var programPlayer = (LtgPlayer)int.Parse(freeArgs[1]);
+                var singlePlayerMode = optionArgs.Contains("-single", StringComparer.InvariantCultureIgnoreCase);
+                RunPlayer(programPlayer, singlePlayerMode);
 
                 return Properties.Settings.Default.ExitCodeSuccess;
             }
             playerBot = (LtgBot)Activator.CreateInstance(botType);
         }
 
-        private static void RunPlayer(LtgPlayer player)
+        private static void RunPlayer(LtgPlayer player, bool singlePlayerMode)
         {
             var match = new LtgMatch();
 
             {
                 match.StartPlayerTurn();
                 DoProponentAction(match);
-                match.SwitchPlayerTurn();
-
-                match.StartPlayerTurn();
-                DoOpponentAction(match);
-                match.SwitchPlayerTurn();
+                if (!singlePlayerMode)
+                {
+                    match.SwitchPlayerTurn();
+                    match.StartPlayerTurn();
+                    DoOpponentAction(match);
+                    match.SwitchPlayerTurn();
+                }
 
                 match.NextTurn();
             }
         private static void WriteCardAction(LtgAction action)
         {
             Action writeSlotNumber = () => Console.Out.WriteLine(action.SlotIndex);
-            Action writeCardNumber = () => Console.Out.WriteLine(action.Card.ToName());
+            Action writeCardName = () => Console.Out.WriteLine(action.Card.ToName());
 
             Console.Out.WriteLine((int)action.ApplyOrder);
             if (action.ApplyOrder == LtgCardApplyOrder.Left)
             {
-                writeCardNumber();
+                writeCardName();
                 writeSlotNumber();
             }
             else if (action.ApplyOrder == LtgCardApplyOrder.Right)
             {
                 writeSlotNumber();
-                writeCardNumber();
+                writeCardName();
             }
             else
             {
             var action = new LtgAction();
 
             Action readSlotNumber = () => action.SlotIndex = int.Parse(Console.In.ReadLine());
-            Action readCardNumber = () => action.Card = LtgCards.FromName(Console.In.ReadLine());
+            Action readCardName = () => action.Card = LtgCards.FromName(Console.In.ReadLine());
 
             action.ApplyOrder = (LtgCardApplyOrder)int.Parse(Console.In.ReadLine());
             if (action.ApplyOrder == LtgCardApplyOrder.Left)
             {
-                readCardNumber();
+                readCardName();
                 readSlotNumber();
             }
             else if (action.ApplyOrder == LtgCardApplyOrder.Right)
             {
                 readSlotNumber();
-                readCardNumber();
+                readCardName();
             }
             else
             {

File src/Icfp2011.Icrs/LtgCards.cs

 {
     public static class LtgCards
     {
-        public static readonly IList<LtgValue> AllCards = new[]
-            {
-                Zero,
-                ICombinator,
-                Succ,
-                Dbl,
-                Get,
-                Put,
-                SCombinator,
-                KCombinator,
-                Inc,
-                Dec,
-                Attack,
-                Help,
-                Copy,
-                Revive,
-                Zombie,
-            };
+        public static readonly IList<LtgValue> AllCards;
 
         public static readonly LtgValue Zero = new LtgNumber(0);
 
                     });
             });
 
+        static LtgCards()
+        {
+            AllCards = new[]
+                {
+                    Zero,
+                    ICombinator,
+                    Succ,
+                    Dbl,
+                    Get,
+                    Put,
+                    SCombinator,
+                    KCombinator,
+                    Inc,
+                    Dec,
+                    Attack,
+                    Help,
+                    Copy,
+                    Revive,
+                    Zombie,
+                };
+        }
+
         public static LtgValue FromName(string name)
         {
             if (name == "I")

File src/Icfp2011.Icrs/LtgException.cs

 {
     public class LtgException : Exception
     {
+        public LtgException(LtgExceptionType type, string data)
+            : this(type)
+        {
+            this.Data = data;
+        }
+
         public LtgException(LtgExceptionType type)
             : base()
         {
             get;
             private set;
         }
+
+        public string Data
+        {
+            get;
+            private set;
+        }
     }
 
     public enum LtgExceptionType
     {
+        UnknownAction,
         CannotApplyNumber,
         Overflow,
         NotAlive,

File src/Icfp2011.Icrs/LtgFunction.cs

         public override LtgValue Evaluate(ref int depth, LtgSlot[] propSlots, LtgSlot[] oppSlots, LtgValue arg,
             bool reverseMode)
         {
-            if (depth > LtgUtilities.MaxEvaluationDepth)
+            if (depth >= LtgUtilities.MaxEvaluationDepth)
                 throw new LtgException(LtgExceptionType.Overflow);
 
             depth++;

File src/Icfp2011.Icrs/LtgSlot.cs

 
         public void DoAction(LtgSlot[] propSlots, LtgSlot[] oppSlots, LtgAction action, out LtgException result)
         {
+            if (action.Card == null)
+                throw new LtgException(LtgExceptionType.UnknownAction);
+
             if (action.ApplyOrder == LtgCardApplyOrder.Left)
                 ApplyLeft(propSlots, oppSlots, action.Card, out result);
             else if (action.ApplyOrder == LtgCardApplyOrder.Right)