ICFP 2011 ICRS / src / Bots / Icfp2011.Icrs.FinalBot / FinalBot.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Icfp2011.Icrs.Bots
{
    using Bot;

    public class FinalBot : LtgBot
    {
        private static readonly LtgAction defaultAction = new LtgAction()
            {
                ApplyOrder = LtgCardApplyOrder.Left,
                Card = LtgCards.ICombinator,
                SlotIndex = 0,
            };

        private LtgAction? lastOppAction;

        private LtgSlot[] propSlots;
        private LtgSlot[] oppSlots;

        private int[] propSlotsUsages;
        private int[] oppSlotsUsages;

        public FinalBot()
        {
            this.propSlotsUsages = new int[LtgMatch.NumSlots];
            this.oppSlotsUsages = new int[LtgMatch.NumSlots];
        }

        public override void Start()
        {
            this.propSlots = this.Match.GetSlots(this.Player);
            this.oppSlots = this.Match.GetSlots(this.OtherPlayer);
        }

        public override LtgAction DoAction()
        {
            return defaultAction;
        }

        public override void OnProponentAction(LtgAction action)
        {
            IncrementSlotUsage(this.propSlots[action.SlotIndex]);
        }

        public override void OnOpponentThinking()
        {
            //
        }

        public override void OnOpponentAction(LtgAction action)
        {
            this.lastOppAction = action;

            IncrementSlotUsage(this.oppSlots[action.SlotIndex]);
        }

        private void IncrementSlotUsage(LtgSlot slot)
        {
            //
        }

        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;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.