Пример #1
0
        /// <summary>
        /// Factor method to create a FieldLocation from a byte list
        /// </summary>
        /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
        /// <returns>A new object of this class</returns>
        new public static PlayingFieldLayout Create(ByteList bytes)
        {
            PlayingFieldLayout result = new PlayingFieldLayout();

            result.Decode(bytes);
            return(result);
        }
        public void PlayingFieldLayout_CheckConstructors()
        {
            PlayingFieldLayout pfl = new PlayingFieldLayout();
            Assert.AreEqual(0, pfl.Width);
            Assert.AreEqual(0, pfl.Height);
            Assert.IsNotNull(pfl.SidewalkSquares);

            pfl = new PlayingFieldLayout(20,25);
            Assert.AreEqual(20, pfl.Width);
            Assert.AreEqual(25, pfl.Height);
            Assert.IsNotNull(pfl.SidewalkSquares);
        }
        public void FieldLayoutReply_Everything()
        {
            PlayingFieldLayout pfl = new PlayingFieldLayout(100, 120);
            Assert.AreEqual(100, pfl.Width);
            Assert.AreEqual(120, pfl.Height);
            Assert.IsNotNull(pfl.SidewalkSquares);

            PlayingFieldReply r1 = new PlayingFieldReply(Reply.PossibleStatus.Success, pfl, "Test");

            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.IsNotNull(r1.Layout);
            Assert.AreSame(pfl, r1.Layout);

            ByteList bytes = new ByteList();
            r1.Encode(bytes);
            Message msg = Message.Create(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is PlayingFieldReply);
            PlayingFieldReply r2 = msg as PlayingFieldReply;
            Assert.AreEqual(Reply.PossibleStatus.Success, r2.Status);

            Assert.AreEqual(pfl.Height, r2.Layout.Height);
            Assert.AreEqual(pfl.Width, r2.Layout.Width);
        }
Пример #4
0
        public static DistributableObject Create(ByteList bytes)
        {
            DistributableObject result = null;

            if (bytes == null || bytes.RemainingToRead < 4)
            {
                throw new ApplicationException("Invalid byte array");
            }

            DISTRIBUTABLE_CLASS_IDS objType = (DISTRIBUTABLE_CLASS_IDS)bytes.PeekInt16();

            switch (objType)
            {
            case DISTRIBUTABLE_CLASS_IDS.MessageNumber:
                result = MessageNumber.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Bomb:
                result = Bomb.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.AgentInfo:
                result = AgentInfo.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.AgentList:
                result = AgentList.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.EndPoint:
                result = EndPoint.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Excuse:
                result = Excuse.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.FieldLocation:
                result = FieldLocation.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.GameConfiguration:
                result = GameConfiguration.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.PlayingFieldLayout:
                result = PlayingFieldLayout.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Tick:
                result = Tick.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.WhiningTwine:
                result = WhiningTwine.Create(bytes);
                break;

            default:
                throw new ApplicationException(string.Format("Invalid Class Id={0}", objType));
            }
            return(result);
        }
 /// <summary>
 /// Constructor used by all specializations, which in turn are used by
 /// senders of a message 
 /// </summary>
 /// <param name="conversationId">conversation id</param>
 /// <param name="status">Status of the ack/nak</status>
 /// <param name="note">error message or note</note>       
 public PlayingFieldReply(PossibleStatus status, PlayingFieldLayout layout, string note = null)
     : base(Reply.PossibleTypes.PlayingFieldReply, status, note)
 {
     Layout = layout;
 }
        public void PlayingFieldLayout_CheckEncodeAndDecode()
        {
            PlayingFieldLayout pfl1 = new PlayingFieldLayout(20, 30);
            Assert.AreEqual(20, pfl1.Width);
            Assert.AreEqual(30, pfl1.Height);
            Assert.IsNotNull(pfl1.SidewalkSquares);

            List<FieldLocation> flList = new List<FieldLocation> { new FieldLocation(1, 1), new FieldLocation(2, 1) };
            pfl1.SidewalkSquares = flList;
            Assert.AreSame(flList, pfl1.SidewalkSquares);

            ByteList bytes = new ByteList();
            pfl1.Encode(bytes);
            PlayingFieldLayout pfl2 = PlayingFieldLayout.Create(bytes);
            Assert.AreEqual(pfl1.Width, pfl2.Width);
            Assert.AreEqual(pfl1.Height, pfl2.Height);
            Assert.IsNotNull(pfl2.SidewalkSquares);
            Assert.AreEqual(pfl1.SidewalkSquares.Count, pfl1.SidewalkSquares.Count);

            bytes.Clear();
            pfl1.Encode(bytes);
            bytes.GetByte();            // Read one byte, which will throw the length off
            try
            {
                pfl2 = PlayingFieldLayout.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }

            bytes.Clear();
            pfl1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                pfl2 = PlayingFieldLayout.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }

            pfl1 = new PlayingFieldLayout(100, 100);
            SetUpSidewalks(pfl1);
            Assert.AreEqual(100, pfl1.Width);
            Assert.AreEqual(100, pfl1.Height);
            Assert.IsNotNull(pfl1.SidewalkSquares);

            bytes = new ByteList();
            pfl1.Encode(bytes);
            pfl2 = PlayingFieldLayout.Create(bytes);
            Assert.AreEqual(pfl1.Width, pfl2.Width);
            Assert.AreEqual(pfl1.Height, pfl2.Height);
            Assert.IsNotNull(pfl2.SidewalkSquares);
            Assert.AreEqual(pfl1.SidewalkSquares.Count, pfl1.SidewalkSquares.Count);

            pfl1 = new PlayingFieldLayout(200, 300);
            SetUpSidewalks(pfl1);
            Assert.AreEqual(200, pfl1.Width);
            Assert.AreEqual(300, pfl1.Height);
            Assert.IsNotNull(pfl1.SidewalkSquares);

            bytes = new ByteList();
            pfl1.Encode(bytes);
            pfl2 = PlayingFieldLayout.Create(bytes);
            Assert.AreEqual(pfl1.Width, pfl2.Width);
            Assert.AreEqual(pfl1.Height, pfl2.Height);
            Assert.IsNotNull(pfl2.SidewalkSquares);
            Assert.AreEqual(pfl1.SidewalkSquares.Count, pfl1.SidewalkSquares.Count);
        }
 private void SetUpSidewalks(PlayingFieldLayout playingFieldLayout)
 {
     SetupOutsideSidewalks(playingFieldLayout);
     SetupInsideSidewalks(playingFieldLayout);
 }
 private void SetupOutsideSidewalks(PlayingFieldLayout playingFieldLayout)
 {
     if (playingFieldLayout.Width > 8 && playingFieldLayout.Height > 8)
     {
         for (Int16 column = 2; column < playingFieldLayout.Width - 3; column++)
         {
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation(column, (Int16)2));
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation(column, Convert.ToInt16(playingFieldLayout.Height - 4)));
         }
         for (Int16 row = 3; row < playingFieldLayout.Height - 4; row++)
         {
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation((Int16)2, row));
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation(Convert.ToInt16(playingFieldLayout.Width - 4), row));
         }
     }
 }
 private void SetupInsideSidewalks(PlayingFieldLayout playingFieldLayout)
 {
     if (playingFieldLayout.Width > 16 && playingFieldLayout.Height > 16)
     {
         Int16 centerColumn = Convert.ToInt16((playingFieldLayout.Width / 2) - 1);
         Int16 centerRow = Convert.ToInt16((playingFieldLayout.Height / 2) - 1);
         for (Int16 column = 2; column < playingFieldLayout.Width - 3; column++)
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation(column, centerRow));
         for (Int16 row = 3; row < playingFieldLayout.Height - 4; row++)
             playingFieldLayout.SidewalkSquares.Add(new FieldLocation(centerColumn, row));
     }
 }
        public void PlayingFieldLayout_CheckProperties()
        {
            PlayingFieldLayout pfl = new PlayingFieldLayout(20, 30);
            Assert.AreEqual(20, pfl.Width);
            Assert.AreEqual(30, pfl.Height);
            Assert.IsNotNull(pfl.SidewalkSquares);

            pfl.Width = 0;
            Assert.AreEqual(0, pfl.Width);
            pfl.Width = 35;
            Assert.AreEqual(35, pfl.Width);

            pfl.Height = 0;
            Assert.AreEqual(0, pfl.Height);
            pfl.Height = 45;
            Assert.AreEqual(45, pfl.Height);

            List<FieldLocation> flList = new List<FieldLocation> { new FieldLocation(1, 1), new FieldLocation(2, 1) };
            pfl.SidewalkSquares = flList;
            Assert.AreSame(flList, pfl.SidewalkSquares);
            pfl.SidewalkSquares = null;
            Assert.IsNull(pfl.SidewalkSquares);
        }
 /// <summary>
 /// Factor method to create a FieldLocation from a byte list
 /// </summary>
 /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
 /// <returns>A new object of this class</returns>
 public static new PlayingFieldLayout Create(ByteList bytes)
 {
     PlayingFieldLayout result = new PlayingFieldLayout();
     result.Decode(bytes);
     return result;
 }