Exemple #1
0
        private void ResetConfiguration()
        {
            if (rlvPanel != null)
            {
                rlvPanel.Close();
            }

            rlvPanel = null;

            config.Name = string.Empty;
            ((IList)config.Blocks).Clear();
            config.BlockTemplates.Clear();
            config.FreeMoves        = 1000;
            config.Height           = 20;
            config.Width            = 20;
            config.MoveFactor       = 100;
            config.StartingLocation = new Location {
                X = 0, Y = 0
            };

            config.Blocks = (ChallengerLib.Models.Block[, ])Utilities.ResizeArray(config.Blocks, new int[] { config.Height, config.Width });
            LoadSimulationBlocks();
            LoadConfigurationBlocks();

            txtName.Text      = config.Name;
            txtFreeMoves.Text = config.FreeMoves.ToString();
            txtHeight.Text    = config.Height.ToString();
            txtWidth.Text     = config.Width.ToString();
            sliderMoveFactor.SetValue(Slider.ValueProperty, config.MoveFactor);
        }
        private void btnVisualizer_Click(object sender, RoutedEventArgs e)
        {
            core = new RLVCore(dbIdentifier);

            rlvPanel = new VisualizerWindow(core, challenger.HighestMoveCount, challenger.RecentMoves);
            rlvPanel.Show();
        }
Exemple #3
0
        private void btnVisualizer_Click(object sender, RoutedEventArgs e)
        {
            //rlmDbData = new RlmDbDataPostgreSqlServer(dbIdentifier);
            rlmDbData = new RlmDbDataSQLServer(dbIdentifier);
            core      = new RLVCore(rlmDbData);

            rlvPanel = new VisualizerWindow(core, challenger.HighestMoveCount, challenger.RecentMoves);
            rlvPanel.Show();
        }
Exemple #4
0
        private void LoadConfiguration(string configName)
        {
            if (rlvPanel != null)
            {
                rlvPanel.Close();
            }

            rlvPanel = null;

            gridBlocks.Children.Clear();

            try
            {
                string path        = System.IO.Path.Combine("SimulationConfig", $"{configName}");
                var    valuesToStr = File.ReadAllText(path, Encoding.UTF8);
                config = JsonConvert.DeserializeObject <SimulationConfig>(valuesToStr, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                });

                LoadSimulationBlocks();

                txtName.Text             = config.Name;
                txtWidth.Text            = config.Width.ToString();
                txtHeight.Text           = config.Height.ToString();
                txtFreeMoves.Text        = config.FreeMoves.ToString();
                sliderMoveFactor.Value   = config.MoveFactor;
                checkAllowCopy.IsChecked = allowCopy;
                previousHeight           = config.Height;
                previousWidth            = config.Width;

                LoadConfigurationBlocks();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("No configuration file found.");
            }
        }
Exemple #5
0
        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            if (!HasStartSimObject())
            {
                notifier.ShowError("Please add \"START\" object.");
                return;
            }

            if (!HasEndSimObject())
            {
                notifier.ShowError("Please add at least 1 \"END or STOP\" object.");
                return;
            }

            if (isPlaying)
            {
                isPlaying = false;
                tokenSrc.Cancel();
                EnableConfigControls(true);
                btnPlay.Content = "Play";
                txtStatus.Text  = "Stopped";
            }
            else
            {
                SimulationPanel panel = new SimulationPanel();
                panel.SetSimSettings(simSettings);
                if (panel.ShowDialog() == true)
                {
                    if (rlvPanel != null)
                    {
                        rlvPanel.Close();
                    }

                    rlvPanel = null;

                    // update player brush
                    // TODO change this to no longer use the ID to determine the type
                    var blockTemplate = config.BlockTemplates.FirstOrDefault(a => a.ID == 1);
                    playerBrush = new ImageBrush()
                    {
                        ImageSource = new BitmapImage(new Uri(blockTemplate.Icon, UriKind.Relative))
                    };

                    isPlaying = true;
                    tokenSrc  = new CancellationTokenSource();
                    EnableConfigControls(false);
                    btnPlay.Content = "Stop";

                    simSettings.SimType          = panel.SimType;
                    simSettings.Score            = panel.Score;
                    simSettings.Hours            = panel.Hours;
                    simSettings.Sessions         = panel.Sessions;
                    simSettings.EnableSimDisplay = panel.EnableSimDisplay;

                    dbIdentifier = $"RLM_Challenger_{Guid.NewGuid().ToString("N")}";

                    simSettings.DBIdentifier = dbIdentifier;


                    //rlvPanel.Show();

                    //var startRect = rectangleContainer[config.StartingLocation.X, config.StartingLocation.Y];
                    //playerBrush = (ImageBrush)startRect.Fill;

                    challenger = new RLMChallenger(config, simSettings);

                    challenger.OptimizationStatus += (status) =>
                    {
                        if (tokenSrc.IsCancellationRequested)
                        {
                            return;
                        }
                        Dispatcher.Invoke(() =>
                        {
                            txtStatus.Text = status;
                            if (status == "Done")
                            {
                                EnableConfigControls(true);
                                btnPlay.Content = "Play";
                                isPlaying       = false;
                            }
                        });
                    };

                    double topScore = 0.0;
                    challenger.SessionComplete += (sessCnt, score, moves) =>
                    {
                        System.Diagnostics.Debug.WriteLine($"Sessions: {sessCnt} Score: {score} Moves: {moves}");
                        if (tokenSrc.IsCancellationRequested)
                        {
                            return;
                        }
                        Dispatcher.Invoke(() =>
                        {
                            txtSessionScore.Text = score.ToString("N");
                            txtSessionMoves.Text = moves.ToString();
                            if (score > topScore)
                            {
                                topScore = score;
                            }
                            txtTopScore.Text = topScore.ToString("N");
                        });
                    };

                    challenger.MazeCycleComplete += (x, y, bump) =>
                    {
                        //System.Diagnostics.Debug.Write($"x: {x} y: {y} bump: {bump} \r");
                        if (tokenSrc.IsCancellationRequested)
                        {
                            return;
                        }
                        Dispatcher.Invoke(() =>
                        {
                            //var index = (x == 0 && y == 0) ? 0 : x * gridMain.ColumnDefinitions.Count + y - 1;
                            var rect = rectangleContainer[x, y];

                            //if (rect.Tag != null && ((ChallengerLib.Models.Block)rect.Tag).IsEndSimulation)
                            //{
                            //    return;
                            //}
                            //else
                            if (lastRectangle != null && lastRectangle.Equals(rect))
                            {
                                return;
                            }

                            if (lastRectangle != null)
                            {
                                lastRectangle.Fill = lastBrush;
                            }

                            lastRectangle = rect;
                            lastBrush     = lastRectangle.Fill;
                            rect.Fill     = playerBrush;
                        });
                    };

                    //return;

                    Task.Run(() =>
                    {
                        challenger.Travel(100, tokenSrc.Token);
                    }, tokenSrc.Token);
                }
            }
        }