private async Task Move(Object state)
        {
            //IBox boxClient = ServiceProxy.Create<IBox>(new Uri("fabric:/TermiteModel/Box"), new ServicePartitionKey(0));
 
            TermiteState mState = await this.StateManager.GetStateAsync<TermiteState>("State");
            
            if (!mState.HasWoodchip)
            {
                var result = await boxClient.TryPickUpWoodChipAsync(mState.X, mState.Y);
                if (result)
                    mState.HasWoodchip = true;
            }
            else
            {
                var result = await boxClient.TryPutDonwWoodChipAsync(mState.X, mState.Y);
                if (result)
                    mState.HasWoodchip = false;
            }
            await this.StateManager.SetStateAsync<TermiteState>("State", mState);

            int action = rand.Next(1, 9);
            //1-left; 2-left-up; 3-up; 4-up-right; 5-right: 6-right-down; 7-down; 8-down-left
            if ((action == 1 || action == 2 || action == 8) && mState.X > 0)
                mState.X = mState.X - 1;
            if ((action == 4 || action == 5 || action == 6) && mState.X < size - 1)
                mState.X = mState.X + 1;
            if ((action == 2 || action == 3 || action == 4) && mState.Y > 0)
                mState.Y = mState.Y - 1;
            if ((action == 6 || action == 7 || action == 8) && mState.Y < size - 1)
                mState.Y = mState.Y + 1;
        }
예제 #2
0
        private async Task Move(object state)
        {
            TermiteState tState = state as TermiteState;

            IBox boxClient = ServiceProxy.Create <IBox>(new Uri("fabric:/TermiteModel/Box"),
                                                        new Microsoft.ServiceFabric.Services.Client.ServicePartitionKey(rand.Next()));

            if (!tState.HasWoodChip)
            {
                var result = await boxClient.TryPickUpWoodChipAsync(tState.X, tState.Y);

                if (result)
                {
                    tState.HasWoodChip = true;
                }
            }
            else
            {
                var result = await boxClient.TryPutDownWoodChipAsync(tState.X, tState.Y);

                if (result)
                {
                    tState.HasWoodChip = false;
                }
            }

            int action = rand.Next(1, 9);

            //1-left; 2-left-up; 3-up; 4-up-right; 5-right: 6-right-down; 7-down; 8-down-left
            if ((action == 1 || action == 2 || action == 8) && tState.X > 0)
            {
                tState.X = tState.X - 1;
            }
            if ((action == 4 || action == 5 || action == 6) && tState.X < size - 1)
            {
                tState.X = tState.X + 1;
            }
            if ((action == 2 || action == 3 || action == 4) && tState.Y > 0)
            {
                tState.Y = tState.Y - 1;
            }
            if ((action == 6 || action == 7 || action == 8) && tState.Y < size - 1)
            {
                tState.Y = tState.Y + 1;
            }

            await this.SetStateAsync(state);

            await this.SaveStateAsync();
        }