Exemplo n.º 1
0
        public bool ValidateWholeStatus()
        {
            if (LeftShore == null || RightShore == null)
            {
                return(false);
            }

            return(LeftShore.ValidateStatus() && RightShore.ValidateStatus());
        }
Exemplo n.º 2
0
        public async Task TransportAsync(int delayMillisecond = 1000)
        {
            var step  = 0;
            var carry = default(TransportObject);

            this.OnStart?.Invoke();
            Step();

            while (true)
            {
                await Report("檢查左岸是否為空");

                if (LeftShore.Count == 0)
                {
                    await Report("左岸為空,不進行搬運");

                    break;
                }

                carry = LeftShore.Dequeue();
                await Report($"將 '{carry}' 放上船");

                Status(new StatusReportEventArgs(LeftShore, carry, RightShore));

                //檢查左岸是否會發生需要避免的狀況
                await Report("檢查左岸是否會發生需要避免的狀況");

                if (CheckAvoidSituation(LeftShore))
                {
                    await Report($"會有危險,把 '{carry}' 放回左岸");

                    //會有危險,把TO物件放回左岸並換下一個
                    LeftShore.Enqueue(carry);
                    Status(new StatusReportEventArgs(LeftShore, RightShore));
                    continue;
                }
                else
                {
                    await Report("左岸沒危險,渡河,到達右岸");

                    //左岸沒危險,渡河,到達右岸
                    RightShore.Enqueue(carry);
                    Status(new StatusReportEventArgs(LeftShore, RightShore));

                    //左岸全空
                    if (LeftShore.Count == 0)
                    {
                        //結束
                        await Report("左岸全空");

                        break;
                    }

                    //檢查右岸是否會發生需要避免的狀況
                    await Report("檢查右岸是否會發生需要避免的狀況");

                    if (CheckAvoidSituation(RightShore))
                    {
                        carry = RightShore.Dequeue();
                        await Report($"會有危險,把會造成危機的 '{carry}' 放上船");

                        Status(new StatusReportEventArgs(LeftShore, carry, RightShore));

                        //送往左岸
                        LeftShore.Enqueue(carry);
                        await Report("渡河,到達左岸");

                        Status(new StatusReportEventArgs(LeftShore, RightShore));
                    }

                    //右岸沒有危險,空手回左岸
                    else
                    {
                        await Report("右岸沒有危險,空手回左岸");

                        Status(new StatusReportEventArgs(LeftShore, this._void, RightShore));
                    }
                }

                Step();
                await Report("目前狀況");

                Status(new StatusReportEventArgs(LeftShore, RightShore));
            }

            this.OnCompleted?.Invoke();
            return;

            void Step()
            => this.OnStepUpdate?.Invoke(step++);

            void Status(StatusReportEventArgs e)
            => this.OnStatusUpdate?.Invoke(e);

            async Task Report(string message)
            {
                this.OnProgressUpdate?.Invoke(message);
                await Task.Delay(delayMillisecond);
            }
        }