Esempio n. 1
0
        // This is executed in a new thread each time, so it is safe to use blocking calls
        protected override void OnIncomingCall(Call call)
        {
            AnswerResult resultAnswer = call.Answer();

            if (!resultAnswer.Successful)
            {
                Completed.Set();
                return;
            }

            PlayAction actionPlay = call.PlayTTSAsync("I'm a little teapot, short and stout.  Here is my handle, here is my spout.");
            bool       paused     = false;

            while (!actionPlay.Completed)
            {
                paused = !paused;
                if (paused)
                {
                    actionPlay.Pause();
                }
                else
                {
                    actionPlay.Resume();
                }
                Thread.Sleep(3000);
            }

            HangupResult resultHangup = call.Hangup();

            Successful = actionPlay.Result.Successful && resultHangup.Successful;
            Completed.Set();
        }
Esempio n. 2
0
        public static BaseAction ActionToClass(string action)
        {
            BaseAction actionClass;

            switch (action)
            {
            case "play":
                actionClass = new PlayAction();
                break;

            case "hug":
                actionClass = new HugAction();
                break;

            case "feed":
                actionClass = new FeedAction();
                break;

            case "sleep":
                actionClass = new SleepAction();
                break;

            default:
                return(null);
            }

            return(actionClass);
        }
Esempio n. 3
0
        /// <summary>
        /// Plays a single hand.  This may be called multiple times if a hand is split.
        /// </summary>
        /// <param name="table">The Table object to use to request cards</param>
        /// <param name="CurrentHand">Hand to play</param>
        public void PlayHand(Hand CurrentHand)
        {
            while (CurrentHand.Value <= 21 && !CurrentHand.DoubledDown)
            {
                PlayAction Action = this.DecideAction(_Table.UpCard, CurrentHand);
                switch (Action)
                {
                case PlayAction.Split:
                    _Table.Split(CurrentHand, this, Chips.RemoveChips(CurrentHand.Bet.Value));
                    Track.Splits++;
                    break;

                case PlayAction.DoubleDown:
                    _Table.DoubleDown(CurrentHand, Chips.RemoveChips(CurrentHand.Bet.Value));
                    Track.Doubles++;
                    break;

                case PlayAction.Hit:
                    _Table.Hit(CurrentHand);
                    Track.Hits++;
                    break;

                case PlayAction.Stand:
                    Track.Stands++;
                    return;
                }
            }
            if (CurrentHand.Value > 21)
            {
                Track.Busted++;
            }                                                   // Wah wah waaaah.
        }
Esempio n. 4
0
        public void PlayAction_AmmoChange_Both()
        {
            var diceGame = new DiceGame(DiceGameSettings.CreateDefaultGameSettings());
            var player   = diceGame.CurrentBoard.GetPlayer(0);
            var enemy    = diceGame.CurrentBoard.GetPlayer(1);

            enemy.PlayerStats.ChangeAmmo(1, this);
            diceGame.CurrentBoard.SetDices(new List <Dice>
            {
                new Dice(0, 1, diceGame.DiceGameSettings.DiceSettings),
                new Dice(1, 1, diceGame.DiceGameSettings.DiceSettings),
                new Dice(2, 2, diceGame.DiceGameSettings.DiceSettings),
                new Dice(3, 4, diceGame.DiceGameSettings.DiceSettings),
                new Dice(4, 4, diceGame.DiceGameSettings.DiceSettings),
                new Dice(5, 6, diceGame.DiceGameSettings.DiceSettings)
            });

            var playAction    = new PlayAction(0);
            var selectAction1 = new SelectAction(new Dice(3));
            var selectAction2 = new SelectAction(new Dice(4));

            diceGame.Play(selectAction1);
            diceGame.Play(selectAction2);

            diceGame.Play(playAction);

            Assert.IsTrue(player.PlayerStats.Ammo == 1);
            Assert.IsTrue(enemy.PlayerStats.Ammo == 0);
        }
Esempio n. 5
0
        // This is executed in a new thread each time, so it is safe to use blocking calls
        protected override void OnIncomingCall(Call call)
        {
            AnswerResult resultAnswer = call.Answer();

            if (!resultAnswer.Successful)
            {
                Completed.Set();
                return;
            }

            PlayAction actionPlayAudio = call.PlayAudioAsync("https://cdn.signalwire.com/default-music/welcome.mp3");

            PlayAction actionPlayTTS = call.PlayTTSAsync("I'm a little teapot, short and stout.  Here is my handle, here is my spout.");

            while (!actionPlayTTS.Completed)
            {
                Thread.Sleep(500);
            }

            actionPlayAudio.Stop();

            call.Hangup();

            Successful = actionPlayAudio.Result.Successful && actionPlayTTS.Result.Successful;
            Completed.Set();
        }
Esempio n. 6
0
 public void AddMove(PlayAction action)
 {
     if (actions.Count == 0 || actions.Peek() != action)
     {
         actions.Enqueue(action);
     }
 }
Esempio n. 7
0
        public void PerformAction(PlayAction action)
        {
            Position newPosition = new Position(this.Position);

            switch (action)
            {
            case PlayAction.Up:
                newPosition.Y -= 1;
                break;

            case PlayAction.Down:
                newPosition.Y += 1;
                break;

            case PlayAction.Left:
                newPosition.X -= 1;
                break;

            case PlayAction.Right:
                newPosition.X += 1;
                break;

            case PlayAction.NOP:
                World.Instance.Score -= 1;
                break;
            }
            if (World.CanGoTo(newPosition))
            {
                World.GoTo(newPosition);
            }
            else
            {
                World.Instance.Score -= 1;
            }
        }
Esempio n. 8
0
        public void PlayAction_SelfTarget()
        {
            var diceGame = new DiceGame(DiceGameSettings.CreateDefaultGameSettings());
            var player   = diceGame.CurrentBoard.CurrentPlayer;
            var enemy    = diceGame.CurrentBoard.EnemyPlayers[0];

            enemy.PlayerStats.ChangeAmmo(1, this);
            diceGame.CurrentBoard.SetDices(new List <Dice>
            {
                new Dice(0, 1),
                new Dice(1, 1),
                new Dice(2, 2),
                new Dice(3, 3),
                new Dice(4, 4),
                new Dice(5, 5),
            });

            var playAction   = new PlayAction(player.PlayerStats.ID);
            var selectAction = new SelectAction(new Dice(3));

            Assert.IsFalse(diceGame.CanBePlayed(playAction));

            diceGame.Play(selectAction);
            diceGame.Play(playAction);

            Assert.IsTrue(player.PlayerStats.Health == player.PlayerStats.MaxHealth - 1);
        }
Esempio n. 9
0
        public PlayAction Decide(World world)
        {
            // Convert world to model data
            var worldValue = WorldToValue();

            PlayAction action = PlayAction.NOP;
            int        odd    = rnd.Next(100);

            if (odd >= world.Epsilon)
            {
                // Trace.WriteLine("Calculated Action");

                inputDataMap[inputVariable] = Value.CreateBatch <float>(model.Arguments[0].Shape, worldValue, device);
                outputDataMap[model.Output] = null;

                model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);

                var output = outputDataMap[model].GetDenseData <float>(model)[0];

                // Convert output to PlayAction
                int maxIndex = CNTKHelper.CNTKHelper.ArgMax(output);
                if (maxIndex == -1)
                {
                    return(PlayAction.NOP);
                }
                action = (PlayAction)maxIndex;
            }
            else
            {
                //Trace.WriteLine("Random Action");
                action = RandomAgent.GetRandomAction();
            }
            return(action);
        }
Esempio n. 10
0
        bool PlayHand(Player player, Card dealerTopCard, Shoe shoe)
        {
            PlayAction play = player.Play(player.Hand, dealerTopCard);

            if (play == PlayAction.Stay)
            {
                return(true);
            }
            if (play == PlayAction.Hit)
            {
                Card card = shoe.GetNextCard();
                bool bust = player.TakeCard(card);
                if (bust)
                {
                    return(false);
                }
                return(PlayHand(player, dealerTopCard, shoe));
            }
            if (play == PlayAction.Double)
            {
                if (player.Hand.Count() != 2)
                {
                    throw new Exception("only double on first");
                }
                var bust = player.TakeCard(shoe.GetNextCard());
                if (bust)
                {
                    return(false);
                }
                return(true);
            }
            throw new InvalidOperationException("shouldn't be here");
        }
Esempio n. 11
0
        // This is executed in a new thread each time, so it is safe to use blocking calls
        protected override void OnIncomingCall(Call call)
        {
            AnswerResult resultAnswer = call.Answer();

            if (!resultAnswer.Successful)
            {
                Completed.Set();
                return;
            }

            PlayAction actionPlay = call.PlayTTSAsync("I'm a little teapot, short and stout.  Here is my handle, here is my spout.", volume: 16);

            Thread.Sleep(1000);

            bool             increase         = false;
            PlayVolumeResult resultPlayVolume = null;

            while (!actionPlay.Completed)
            {
                increase         = !increase;
                resultPlayVolume = actionPlay.Volume(increase ? 8 : 0);
                if (!resultPlayVolume.Successful)
                {
                    break;
                }
                Thread.Sleep(3000);
            }

            call.Hangup();

            Successful = actionPlay.Result.Successful && resultPlayVolume.Successful;
            Completed.Set();
        }
    public GameStateController()
    {
        gameFsm       = new FSM("GameFSM");
        preStartState = gameFsm.AddState((byte)GameState.PreStart);
        idleState     = gameFsm.AddState((byte)GameState.Idle);
        playState     = gameFsm.AddState((byte)GameState.Play);
        pauseState    = gameFsm.AddState((byte)GameState.Pause);
        gameOverState = gameFsm.AddState((byte)GameState.GameOver);

        preStartAction = new PreStartAction(preStartState);
        playingAction  = new PlayAction(playState);
        pauseAction    = new PauseAction(pauseState);
        gameOverAction = new GameOverAction(gameOverState);
        idleAction     = new IdleAction(idleState);

        preStartState.AddTransition((byte)FSMTransition.ToIdle, idleState);
        idleState.AddTransition((byte)FSMTransition.ToPlay, playState);
        playState.AddTransition((byte)FSMTransition.ToPause, pauseState);
        playState.AddTransition((byte)FSMTransition.ToGameOver, gameOverState);
        gameOverState.AddTransition((byte)FSMTransition.ToIdle, idleState);

        preStartAction.Init();
        idleAction.Init();
        playingAction.Init();
        pauseAction.Init();
        gameOverAction.Init();
    }
Esempio n. 13
0
        private void OnWorldMovePerformed(World world, PlayAction action)
        {
            // Calculate reward
            state.Reward  = world.Score - previousScore;
            previousScore = world.Score;
            //Trace.WriteLine($"OnWorldMovePerformed => {action} Reward = {state.Reward}");

            states.Insert(0, state);
            if (state.Reward != 0 && states.Count >= batchSize)
            {
                states = states.Take(batchSize).ToList();
                Trace.WriteLine($"Train batch");

                // Calculate reward and expected output
                float reward  = 0;
                var   values  = new float[states.First().Value.Length *states.Count];
                var   actions = new float[World.PLAY_ACTION_COUNT * states.Count];
                int   i       = 0;
                foreach (var state in states)
                {
                    state.Value.CopyTo(values, i * state.Value.Length);

                    reward = decay * reward + state.Reward;

                    Trace.WriteLine($"Train batch - Action: {state.Action} Reward: {reward}");

                    var expectedActions = CNTKHelper.CNTKHelper.SoftMax(CNTKHelper.CNTKHelper.OneHot((int)state.Action, World.PLAY_ACTION_COUNT, reward));
                    expectedActions.CopyTo(actions, i * World.PLAY_ACTION_COUNT);

                    i++;
                }

                // Create Minibatches
                var inputs         = Value.CreateBatch <float>(model.Arguments[0].Shape, values, device);
                var inputMinibatch = new MinibatchData(inputs, (uint)states.Count());

                var outputs         = Value.CreateBatch <float>(model.Output.Shape, actions, device);
                var outputMinibatch = new MinibatchData(outputs, (uint)states.Count());

                // Apply learning

                var arguments = new Dictionary <Variable, MinibatchData>
                {
                    { inputVariable, inputMinibatch },
                    { actionVariable, outputMinibatch }
                };
                int epoc = 5;
                while (epoc > 0)
                {
                    trainer.TrainMinibatch(arguments, device);
                    CNTKHelper.CNTKHelper.PrintTrainingProgress(trainer, epoc);

                    epoc--;
                }
                // Go for next
                states.Clear();
            }
        }
Esempio n. 14
0
 public void ProcessPlayAction(PlayAction playAction)
 {
     PlayerHandDictionary[CurrentPlayer.PlayerId].Cards.Remove(playAction.Card);
     PlayActionList.Add(playAction);
     StickResult = GetStickResult();
     if (StickResult == null)
     {
         CurrentPlayer = PlayerGroupInfo.GetNextPlayer(CurrentPlayer.PlayerId);
     }
 }
Esempio n. 15
0
        static int _s_set_uiOffsetX(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

                PlayAction gen_to_be_invoked = (PlayAction)translator.FastGetCSObj(L, 1);
                gen_to_be_invoked.uiOffsetX = (float)LuaAPI.lua_tonumber(L, 2);
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
            return(0);
        }
Esempio n. 16
0
        public PlayAction Decide(World world)
        {
            // Convert world to model data
            var worldValue = WorldToValue();

            PlayAction action = PlayAction.NOP;
            int        odd    = rnd.Next(100);

            if (odd >= world.Epsilon)
            {
                // Trace.WriteLine("Calculated Action");

                inputDataMap[inputVariable] = Value.CreateBatch <float>(model.Arguments[0].Shape, worldValue, device);
                outputDataMap[model.Output] = null;

                model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);

                var output = outputDataMap[model].GetDenseData <float>(model)[0];

                // Convert output to PlayAction
                int maxIndex = CNTKHelper.CNTKHelper.ArgMax(output);
                if (maxIndex == -1)
                {
                    return(PlayAction.NOP);
                }
                action = (PlayAction)maxIndex;
            }
            else
            {
                //Trace.WriteLine("Random Action");
                // action = RandomAgent.GetRandomAction();

                var coin = world.Coins.Select(c => new { c, Dist = (world.Pacman.Position.X - c.Position.X) * (world.Pacman.Position.X - c.Position.X) + (world.Pacman.Position.Y - c.Position.Y) * (world.Pacman.Position.Y - c.Position.Y) }).OrderBy(c => c.Dist).First().c;

                var dx = coin.Position.X - world.Pacman.Position.X;
                var dy = coin.Position.Y - world.Pacman.Position.Y;
                if (Math.Abs(dx) > Math.Abs(dy))
                {
                    action = dx > 0 ? PlayAction.Right : PlayAction.Left;
                }
                else
                {
                    action = dy > 0 ? PlayAction.Down : PlayAction.Up;
                }
            }
            state = new State
            {
                Value  = worldValue,
                Action = action,
                Reward = 0
            };
            return(action);
        }
        public PlayAction PlayCard(IList <Card> allowedCards, IList <Card> currentTrickCards)
        {
            var sb = new StringBuilder();
            var allowedCardsList = new CardsCollection(allowedCards);

            allowedCardsList.Sort(this.Contract.Type);
            for (int i = 0; i < allowedCardsList.Count; i++)
            {
                sb.AppendFormat("{0}({1}); ", i + 1, allowedCardsList[i]);
            }

            // this.Draw();
            while (true)
            {
                var action = new PlayAction();
                ConsoleHelper.WriteOnPosition(new string(' ', 78), 0, Settings.ConsoleHeight - 3);
                ConsoleHelper.WriteOnPosition(new string(' ', 78), 0, Settings.ConsoleHeight - 2);
                ConsoleHelper.WriteOnPosition(sb.ToString().Trim(), 0, Settings.ConsoleHeight - 2);
                ConsoleHelper.WriteOnPosition("It's your turn! Please select card to play: ", 0, Settings.ConsoleHeight - 3);
                var cardIndexAsString = Console.ReadLine();
                int cardIndex;

                if (int.TryParse(cardIndexAsString, out cardIndex))
                {
                    if (cardIndex > 0 && cardIndex <= allowedCardsList.Count)
                    {
                        var cardToPlay = allowedCardsList[cardIndex - 1];
                        action.Card = cardToPlay;

                        if (this.hand.IsBeloteAllowed(Contract, currentTrickCards, cardToPlay))
                        {
                            ConsoleHelper.WriteOnPosition(new string(' ', 78), 0, Settings.ConsoleHeight - 3);
                            ConsoleHelper.WriteOnPosition(new string(' ', 78), 0, Settings.ConsoleHeight - 2);
                            ConsoleHelper.WriteOnPosition(new string(' ', 78), 0, Settings.ConsoleHeight - 1);
                            ConsoleHelper.WriteOnPosition("Y(es) / N(o)", 0, Settings.ConsoleHeight - 2);
                            ConsoleHelper.WriteOnPosition("You have belote! Do you want to announce it? Y/N ", 0, Settings.ConsoleHeight - 3);
                            var answer = Console.ReadLine();

                            if (!string.IsNullOrWhiteSpace(answer) && answer.Trim()[0] == 'N')
                            {
                                action.AnnounceBeloteIfAvailable = false;
                            }
                        }

                        this.hand.Remove(cardToPlay);
                        return(action);
                    }
                }
            }
        }
Esempio n. 18
0
 public void ProcessPlayAction(PlayAction playAction)
 {
     CurrentStickRound.ProcessPlayAction(playAction);
     if (CurrentStickRound.StickResult != null)
     {
         Log.Information(CurrentStickRound.StickResult.ToString());
         ConfirmAction?.Invoke();
         PlayResult = GetPlayResult();
         if (PlayResult == null)
         {
             var currentPlayer = CurrentStickRound.StickResult.Winner;
             var stickRound    = new StickRound(PlayerGroupInfo, PlayerHandDictionary, currentPlayer, PlayType);
             StickRoundList.Add(stickRound);
         }
     }
     return;
 }
Esempio n. 19
0
        static int __CreateInstance(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                if (LuaAPI.lua_gettop(L) == 1)
                {
                    PlayAction gen_ret = new PlayAction();
                    translator.Push(L, gen_ret);

                    return(1);
                }
            }
            catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
            return(LuaAPI.luaL_error(L, "invalid arguments to PlayAction constructor!"));
        }
Esempio n. 20
0
        private async Task pickAndPlay(PlayAction action, object sender = null)
        {
            // Create and open the file picker
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            openPicker.FileTypeFilter.Add(".mp4");
            openPicker.FileTypeFilter.Add(".mkv");
            openPicker.FileTypeFilter.Add(".avi");

            mVideoFile = await openPicker.PickSingleFileAsync();

            if (null != mVideoFile && null != action)
            {
                action(sender, null);
            }
        }
Esempio n. 21
0
        public void PlayAction_Valid()
        {
            var diceGame = new DiceGame(DiceGameSettings.CreateDefaultGameSettings());

            diceGame.CurrentBoard.SetDices(new List <Dice>
            {
                new Dice(0, 1, diceGame.DiceGameSettings.DiceSettings),
                new Dice(1, 1, diceGame.DiceGameSettings.DiceSettings),
                new Dice(2, 2, diceGame.DiceGameSettings.DiceSettings),
                new Dice(3, 3, diceGame.DiceGameSettings.DiceSettings),
                new Dice(4, 4, diceGame.DiceGameSettings.DiceSettings),
                new Dice(5, 6, diceGame.DiceGameSettings.DiceSettings)
            });

            var playAction   = new PlayAction(0);
            var selectAction = new SelectAction(new Dice(1));

            diceGame.Play(selectAction);

            Assert.IsTrue(diceGame.CurrentBoard.Players.TrueForAll(p => p.PlayerStats.Health < p.PlayerStats.MaxHealth));
        }
Esempio n. 22
0
        public void PlayAction_Valid()
        {
            var diceGame = new DiceGame(DiceGameSettings.CreateDefaultGameSettings());

            diceGame.CurrentBoard.SetDices(new List <Dice>
            {
                new Dice(0, 1),
                new Dice(1, 1),
                new Dice(2, 2),
                new Dice(3, 3),
                new Dice(4, 4),
                new Dice(5, 5),
            });

            var playAction   = new PlayAction(0);
            var selectAction = new SelectAction(new Dice(1));

            Assert.IsFalse(diceGame.CanBePlayed(playAction));

            diceGame.Play(selectAction);

            Assert.IsTrue(diceGame.CanBePlayed(playAction));
        }
Esempio n. 23
0
        public void Load()
        {
            if (MapMgr.Instance.InMainCity == false)
            {
                if (BossDeadEffect.GameObj != null)
                {
                    BossDeadEffect.Load("Animation/effect/BossDeadEffect", (a, b) =>
                    {
                        BossDeadEffect.GameObj.SetActive(false);
                    });

                    //在GetComponentInChildren之前先判断是否active,如果不是的话,先打开
                    if (BossDeadEffect.GameObj.activeInHierarchy == false)
                    {
                        BossDeadEffect.GameObj.SetActive(true);
                    }

                    Camera cam = BossDeadEffect.GameObj.GetComponentInChildren <Camera>();
                    if (cam != null && null != CoreEntry.gCameraMgr.MainCamera)
                    {
                        cam.CopyFrom(CoreEntry.gCameraMgr.MainCamera);
                        BaseTool.ResetTransform(cam.transform);
                    }
                    if (null != CoreEntry.gCameraMgr.MainCamera)
                    {
                        BossDeadEffect.GameObj.transform.parent = CoreEntry.gCameraMgr.MainCamera.transform;
                    }
                    BaseTool.ResetTransform(BossDeadEffect.GameObj.transform);

                    //不让动画自动播放
                    BossDeadEffect.GameObj.SetActive(false);

                    BossDeadEffect_PlayAction = BossDeadEffect.GameObj.GetComponent <PlayAction>();
                }
            }
        }
Esempio n. 24
0
 private void btnPlayAction_Click(object sender, RoutedEventArgs e)
 {
     PlayAction?.Invoke(this, new EventArgs());
 }
Esempio n. 25
0
    // Use this for initialization
    void Start()
    {
        gridScript = gameObject.GetComponent<GridScript>();
        rpcScript = gameObject.GetComponent<RPCScript>();

        // Initialize game state variables
        curPlayAction = PlayAction.None;
        curGameState = GameState.Connecting;
        // Run game initialization
        gridInited = false;
        existSelection = false;

        string playerName = PlayerPrefs.GetString("playerName");
        myname = playerName;
        rpcScript.sendPlayerName(myname);
        setPlayerType();

        int LoadedInt = PlayerPrefs.GetInt("LoadedGame");

        if (LoadedInt == 1) {
            //This is a loaded game
            //isLoadedGame = true;
            //Send RPC to say its a loaded game.

            //Change current game state.
            isLoadedGame = true;
            loadFilePath = PlayerPrefs.GetString("LoadedGamePath");
            Debug.Log(loadFilePath);
        }

        player1SetupDone = false;
        player2SetupDone = false;
        player1SetupAcceptance = false;
        player2SetupAcceptance = false;

        setupAccepted = false;

        myRadarCount = 1;
        myKamikazeCount = 1;
        myCruiserCount = 2;
        myDestroyerCount = 3;
        myTorpedoCount = 2;
        myMineLayerCount = 2;
    }
Esempio n. 26
0
 private void btnPlay_Click(object sender, EventArgs e)
 {
     PlayAction?.Invoke();
 }
Esempio n. 27
0
 public AiAction(Stone ToGive, PlayAction Action)
 {
     this.ToGive = ToGive;
     this.Action = Action;
 }
Esempio n. 28
0
 public static LaunchAction ToLaunchAction(this PlayAction action)
 => action == PlayAction.Launch ? LaunchAction.Launch : LaunchAction.Default;
Esempio n. 29
0
        public PlayAction Decide(World world)
        {
            PlayAction action = PlayAction.NOP;

            // Find closest coin
            if (world.Coins.Count == 0)
            {
                return(action);
            }

            if (path == null)
            {
                var coin = world.Coins.Select(c => new { c, Dist = (world.Pacman.Position.X - c.Position.X) * (world.Pacman.Position.X - c.Position.X) + (world.Pacman.Position.Y - c.Position.Y) * (world.Pacman.Position.Y - c.Position.Y) }).OrderBy(c => c.Dist).First().c;

                var dx = coin.Position.X - world.Pacman.Position.X;
                var dy = coin.Position.Y - world.Pacman.Position.Y;

                if (Math.Abs(dx) > Math.Abs(dy))
                {
                    action = dx > 0 ? PlayAction.Right : PlayAction.Left;
                }
                else
                {
                    action = dy > 0 ? PlayAction.Down : PlayAction.Up;
                }

                if (world.CanGo(action))
                {
                    return(action);
                }
                else
                {
                    // Find best route using graph
                    var nodes = Graph.FindShortestRoute(world.Pacman.Position, coin.Position, SearchAlgorithm.Dijkstra);
                    if (nodes == null)
                    {
                        return(RandomAgent.GetRandomAction());
                    }

                    nodes.RemoveAt(0);
                    path = new Queue <INode <Position> >(nodes);
                    var node = path.Dequeue();

                    if (path.Count == 0)
                    {
                        path = null;
                    }
                    else
                    {
                        GameObject.Renderer.DrawPath(path);
                    }

                    if (world.Pacman.Position.X - node.Value.X > 0)
                    {
                        return(PlayAction.Left);
                    }
                    if (world.Pacman.Position.X - node.Value.X < 0)
                    {
                        return(PlayAction.Right);
                    }
                    if (world.Pacman.Position.Y - node.Value.Y > 0)
                    {
                        return(PlayAction.Up);
                    }
                    if (world.Pacman.Position.Y - node.Value.Y < 0)
                    {
                        return(PlayAction.Down);
                    }
                }
            }
            else
            {
                var node = path.Dequeue();

                if (path.Count == 0)
                {
                    path = null;
                }
                else
                {
                    GameObject.Renderer.DrawPath(path);
                }

                if (world.Pacman.Position.X - node.Value.X > 0)
                {
                    return(PlayAction.Left);
                }
                if (world.Pacman.Position.X - node.Value.X < 0)
                {
                    return(PlayAction.Right);
                }
                if (world.Pacman.Position.Y - node.Value.Y > 0)
                {
                    return(PlayAction.Up);
                }
                if (world.Pacman.Position.Y - node.Value.Y < 0)
                {
                    return(PlayAction.Down);
                }
            }
            return(action);
        }
Esempio n. 30
0
 public void Play()
 {
     PlayAction.DoAction();
 }
Esempio n. 31
0
        public void OnWorldMovePerformed(World world, PlayAction action)
        {
            // Calculate reward
            state.Reward  = world.Score - previousScore;
            previousScore = world.Score;
            //Trace.WriteLine($"OnWorldMovePerformed => {action} Reward = {state.Reward}");

            states.Insert(0, state);
            if (state.Reward != 0 && states.Count >= batchSize)
            {
                states = states.Take(batchSize).ToList();

                // Calculate reward and expected output
                float   reward          = 0;
                var     values          = new float[states.First().Value.Length *batchSize];
                var     actions         = new float[World.PLAY_ACTION_COUNT * batchSize];
                int     i               = 0;
                float[] expectedActions = null;
                foreach (var state in states)
                {
                    state.Value.CopyTo(values, i * state.Value.Length);

                    reward = decay * reward + state.Reward;

                    //if (reward > 100)
                    //{
                    //    TraceValues(state.Value, "State Values");
                    //}

                    //Trace.WriteLine($"Train batch - Action: {state.Action} Reward: {reward}");

                    expectedActions = CNTKHelper.CNTKHelper.SoftMax(CNTKHelper.CNTKHelper.OneHot((int)state.Action, World.PLAY_ACTION_COUNT, reward));

                    expectedActions.CopyTo(actions, i * World.PLAY_ACTION_COUNT);

                    i++;
                }

                // Create Minibatches
                var inputs         = Value.CreateBatch <float>(model.Arguments[0].Shape, values, device);
                var inputMinibatch = new MinibatchData(inputs, (uint)states.Count());

                var outputs         = Value.CreateBatch <float>(model.Output.Shape, actions, device);
                var outputMinibatch = new MinibatchData(outputs, (uint)states.Count());

                // Calculate output before learning (for debug)

#if DEBUG
                //inputDataMap[inputVariable] = inputs;
                //outputDataMap[model.Output] = null;

                //model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);

                //var beforeOutput = outputDataMap[model].GetDenseData<float>(model);
#endif

                // Apply learning
                var arguments = new Dictionary <Variable, MinibatchData>
                {
                    { inputVariable, inputMinibatch },
                    { actionVariable, outputMinibatch }
                };
                int epoc = 10;
                while (epoc > 0)
                {
                    trainer.TrainMinibatch(arguments, device);

                    epoc--;
                }
                CNTKHelper.CNTKHelper.PrintTrainingProgress(trainer, epoc);

                //inputDataMap[inputVariable] = inputs;
                //outputDataMap[model.Output] = null;

                //model.Evaluate(inputDataMap, outputDataMap, DeviceDescriptor.CPUDevice);

                //var afterOutput = outputDataMap[model].GetDenseData<float>(model);
                //Trace.WriteLine("Action  \t" + Enum.GetValues(typeof(PlayAction)).OfType<PlayAction>().Select(p => p.ToString()).Aggregate((a, b) => a + "  \t" + b));
                //for (i = 0; i < beforeOutput.Count; i++)
                //{
                //    var ea = actions.Skip(i * World.PLAY_ACTION_COUNT).Take(World.PLAY_ACTION_COUNT).ToArray<float>();
                //    var bo = beforeOutput[i];
                //    var ao = afterOutput[i];
                //    Trace.WriteLine("Expected\t" + ea.Select(p => p.ToString("0.000")).Aggregate((a, b) => a + "\t" + b) + "\t" + (PlayAction)CNTKHelper.CNTKHelper.ArgMax(ea));
                //    Trace.WriteLine("Before  \t" + bo.Select(p => p.ToString("0.000")).Aggregate((a, b) => a + "\t" + b) + "\t" + (PlayAction)CNTKHelper.CNTKHelper.ArgMax(bo));
                //    Trace.WriteLine("After   \t" + ao.Select(p => p.ToString("0.000")).Aggregate((a, b) => a + "\t" + b) + "\t" + (PlayAction)CNTKHelper.CNTKHelper.ArgMax(ao));
                //}

                // Go for next
                states.Clear();
            }
        }
Esempio n. 32
0
        public void PlayFile(List<Track> tracks, int trackIndex, TimeSpan time, PlayAction action)
        {
            _allTracks = tracks;
            Track track = tracks[trackIndex];
            this.CurrentTrack = track;
            this._currentTrackIndex = trackIndex;
            this.CurrentPlayPosition = time;

            bool isChangingFile = _loadedTrack != CurrentTrack;
            PlayStatus playStatusAtMethodCall = PlayStatus;
            if (track != _loadedTrack)
            {
                Stop();
            }

            if (action == PlayAction.Play || (action == PlayAction.MaintainStatus && playStatusAtMethodCall == Common.PlayStatus.Playing))
            {
                //'LoadFile' is a bad method name -- it actually PLAYS the file, too.
                if (track != _loadedTrack
                    || playStatusAtMethodCall == Common.PlayStatus.Stopped)
                {
                    _play.LoadFile(track.FilePath);
                    _loadedTrack = track;
                    _play.VideoExited -= VideoExited;
                    _play.VideoExited += new MplayerEventHandler(VideoExited);
                }

                //When we load a file, mplayer resets the volume. If we have the non-default volume, ensure it's set. This isn't perfect
                //since it'll start the file at default volume for a splitsecond.
                //TODO: switch volume control from mplayer's native controls to Windows's volume settings for the application.
                if (_currentVolume != Constants.DEFAULT_VOLUME)
                {
                    Volume(_currentVolume);
                }

                //NOTE: if we're on the SAME FILE, and we want to seek to the start... we should be able to. if we're changing file... it
                //will start off at the beginning, so seeking will just make it stutter.
                if ((isChangingFile && time != TimeSpan.Zero)
                    || (false == isChangingFile))
                {
                    _play.Seek(Convert.ToInt32(time.TotalSeconds), LibMPlayerCommon.Seek.Absolute);
                }

                _playPositionTimer.Start();
            }

            SetPlayStatus();
            //set the play position, to instantly update the trackbar... instead of waiting for the timer to tick.
            //TODO: the timer still makes it jump back for some reason - mplayer's returning the old time for a second or two ?
            this.CurrentPlayPosition = time;
        }