Beispiel #1
0
        public void TwoMonkeyMovingAtRightOneAtLeft()
        {
            ArrayList monkeyList = new ArrayList();

            monkeyList.Add(new Monkey()
            {
                ID = new Guid(), Side = Direction.Right
            });
            monkeyList.Add(new Monkey()
            {
                ID = new Guid(), Side = Direction.Right
            });
            monkeyList.Add(new Monkey()
            {
                ID = new Guid(), Side = Direction.Left
            });

            var       direcctions      = "";
            Direction currentDirection = Direction.Undefined;

            while (true)
            {
                if (monkeyList.Count > 0)
                {
                    currentDirection = rope.GetDirection((Monkey)monkeyList[0]);
                }
                else
                {
                    break;
                }

                direcctions += $"{rope.currentDirection.ToString()},";

                var newList = Utils.GetListOfMonkeysAllowed(monkeyList, currentDirection, rope.GetMaxAllowedAtSameTime());

                foreach (Monkey item in newList)
                {
                    monkeyList.Remove(item);
                    rope.MonkeysInProgress++;
                    var pool = new Thread(new ParameterizedThreadStart(Run))
                    {
                        Name = "Monkey"
                    };
                    pool.Start(item);
                }
            }
            Assert.AreEqual(direcctions, $"{Direction.Right},{Direction.Left},");
        }
Beispiel #2
0
        /// <summary>
        /// Rope logic to handle monkeys threads
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tStart_Tick(object sender, EventArgs e)
        {
            Direction currentDirection = Direction.Undefined;

            var newList = new List <Monkey>();

            if (rope.MonkeysInProgress == 0)
            {
                if (monkeyList.Count > 0)
                {
                    currentDirection = rope.GetDirection((Monkey)monkeyList[0]);
                }

                newList = Utils.GetListOfMonkeysAllowed(monkeyList, currentDirection, rope.GetMaxAllowedAtSameTime());

                if (newList.Count != 0)
                {
                    var to = currentDirection.Equals(Direction.Right) ? Direction.Left : Direction.Right;
                    UpdateStatus($"Start to cross the rope from {currentDirection} to {to}" + Environment.NewLine);
                }
            }

            foreach (Monkey item in newList)
            {
                if (rope.MonkeysInProgress < 3 && currentDirection.Equals(item.Side))
                {
                    monkeyList.Remove(item);
                    rope.MonkeysInProgress++;
                    var pool = new Thread(new ParameterizedThreadStart(Run))
                    {
                        Name = "Monkey"
                    };
                    pool.IsBackground = true;
                    pool.Start(item);
                }
            }
        }