Source

ICFP 2011 ICRS / src / Icfp2011.Icrs / LtgMatch.cs

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

namespace Icfp2011.Icrs
{
    public class LtgMatch
    {
        public const int NumSlots = 256;
        public const int InitialVitality = 10000;
        public const int MaxNumTurns = 100000;

        public static readonly LtgValue InitialSlotValue = LtgCards.ICombinator;

        private LtgSlot[] slotsPlayerA;
        private LtgSlot[] slotsPlayerB;

        private LtgPlayer curPlayer;

        private int turnNumber;

        public LtgMatch()
        {
            this.slotsPlayerA = new LtgSlot[NumSlots];
            this.slotsPlayerB = new LtgSlot[NumSlots];

            this.curPlayer = LtgPlayer.PlayerA;
            this.turnNumber = 1;

            Initialize();
        }

        private LtgMatch(LtgSlot[] slotsPlayerA, LtgSlot[] slotsPlayerB, LtgPlayer curPlayer)
        {
            this.slotsPlayerA = slotsPlayerA;
            this.slotsPlayerB = slotsPlayerB;
            this.curPlayer = curPlayer;
        }

        public LtgSlot[] SlotsPlayerA
        {
            get { return this.slotsPlayerA; }
        }

        public LtgSlot[] SlotsPlayerB
        {
            get { return this.slotsPlayerB; }
        }

        public LtgPlayer CurrentPlayer
        {
            get { return this.curPlayer; }
        }

        public int TurnNumber
        {
            get { return this.turnNumber; }
        }

        private void Initialize()
        {
            for (int i = 0; i < NumSlots; i++)
            {
                this.slotsPlayerA[i] = new LtgSlot(InitialVitality, InitialSlotValue);
                this.slotsPlayerB[i] = new LtgSlot(InitialVitality, InitialSlotValue);
            }
        }

        public LtgSlot[] GetProponentSlots()
        {
            if (this.curPlayer == LtgPlayer.PlayerA)
                return this.slotsPlayerA;
            else if (this.curPlayer == LtgPlayer.PlayerB)
                return this.slotsPlayerB;
            else
                throw new InvalidOperationException();
        }

        public LtgSlot[] GetOpponentSlots()
        {
            if (this.curPlayer == LtgPlayer.PlayerA)
                return this.slotsPlayerB;
            else if (this.curPlayer == LtgPlayer.PlayerB)
                return this.slotsPlayerA;
            else
                throw new InvalidOperationException();
        }

        // Returns whether match can continue.
        public bool NextTurn(bool switchTurns)
        {
            if (switchTurns)
            {
                if (this.curPlayer == LtgPlayer.PlayerA)
                    this.curPlayer = LtgPlayer.PlayerB;
                else if (this.curPlayer == LtgPlayer.PlayerB)
                    this.curPlayer = LtgPlayer.PlayerA;
                else
                    throw new InvalidOperationException();
            }

            this.turnNumber++;
            return this.turnNumber < MaxNumTurns;
        }

        public LtgMatch Clone()
        {
            return new LtgMatch(
                this.slotsPlayerA.CloneAll(),
                this.slotsPlayerB.CloneAll(),
                this.curPlayer
                );
        }
    }

    public enum LtgPlayer : byte
    {
        PlayerA = 0,
        PlayerB = 1,
    }
}