Beispiel #1
0
 public void UpdateEnvironment(MinesweeperState ms)
 {
     UpdateNumBoom(ms.numOfBoom);
     UpdateNumTime(ms.time);
     UpdateFace(ms.face);
     UpdateSquares(ms.mapState);
 }
Beispiel #2
0
        public void CreateNewEnvironment(MinesweeperState ms)
        {
            UpdateNumBoom(ms.numOfBoom);
            UpdateNumTime(ms.time);
            UpdateFace(ms.face);

            if (ms.height > maxHeight)
            {
                ms.height = maxHeight;
            }
            if (ms.width > maxWidth)
            {
                ms.width = maxWidth;
            }

            if (height != ms.height || width != ms.width)
            {
                RemoveOldFromCanvas();
                squares = new ImageControlObject[ms.height][];
                height  = ms.height;
                width   = ms.width;
                for (int i = 0; i < ms.height; i++)
                {
                    squares[i] = new ImageControlObject[ms.width];
                    for (int j = 0; j < ms.width; j++)
                    {
                        squares[i][j] = ImageControlObject.CreateImage(squareBmpList, canvas);
                    }
                }

                borderLeft  = new ImageControlObject[height];
                borderRight = new ImageControlObject[height];
                borderTop   = new ImageControlObject[width];
                borderBot   = new ImageControlObject[width];

                for (int i = 0; i < height; i++)
                {
                    borderLeft[i]  = ImageControlObject.CreateImage(borderBmpList[5], canvas);
                    borderRight[i] = ImageControlObject.CreateImage(borderBmpList[5], canvas);
                }
                for (int i = 0; i < width; i++)
                {
                    borderTop[i] = ImageControlObject.CreateImage(borderBmpList[4], canvas);
                    borderBot[i] = ImageControlObject.CreateImage(borderBmpList[4], canvas);
                }
            }
            UpdateSquares(ms.mapState);

            SetPostion(x, y);
        }
        private void CreateDrawingMinesweeperEnv(int[] information)
        {
            time = 0;
            timer.Stop();
            MinesweeperState ms = new MinesweeperState();

            ms.time      = 0;
            ms.face      = 0;
            ms.height    = information[0];
            ms.width     = information[1];
            ms.numOfBoom = information[2];
            ms.mapState  = new int[ms.height][];
            for (int i = 0; i < ms.height; i++)
            {
                ms.mapState[i] = new int[ms.width];
            }

            dme.CreateNewEnvironment(ms);

            UpdateSizeWindow(ms.height, ms.width);
        }
Beispiel #4
0
        public MainWindow()
        {
            ActiveGame = new MinesweeperState()
            {
                //OneGameEnd = gameResult =>
                //{
                //    Dispatcher.Invoke(() =>
                //    {
                //        DrawMap();
                //        DrawMines();
                //    });
                //    if (gameResult == GameResult.Win)
                //    {
                //        //Debug.WriteLine("Win!");
                //        MessageBox.Show("Win!");
                //    }
                //    else if (gameResult == GameResult.Loose)
                //    {
                //        //Debug.WriteLine("Loose!");
                //        MessageBox.Show("Loose!");
                //    }

                //    ActiveGame.Initialize();
                //}
            };

            InitializeComponent();
            XUnit = (int)CanvasElement.Width / ActiveGame.SizeX;
            YUnit = (int)CanvasElement.Height / ActiveGame.SizeY;

            ActiveGame.Initialize();
            DrawMap();

            var updateFileIterator        = 10;
            var neuralNetworkDataFilePath = "../../../NeuralNetworkData.txt";
            var network = new MinesweeperNetwork(ActiveGame);

            try
            {
                var neuralNetworkDataText = File.ReadAllText(neuralNetworkDataFilePath);

                var neuralNetworkData = JsonConvert.DeserializeObject <NeuralNetworkData>(neuralNetworkDataText);

                // Keep in mind these arrays are copied by the reference.
                network.NeuralNetwork.Weights = neuralNetworkData.Weights;
                network.NeuralNetwork.Bias    = neuralNetworkData.Bias;
            }
            catch (FileNotFoundException)
            {
                File.CreateText(neuralNetworkDataFilePath);
            }

            Task.Factory.StartNew(() =>
            {
                var alreadyOpenSqaureClick = 0;

                while (ActiveGame.IsRunning)
                {
                    var(y, x) = network.Predict();

                    if (ActiveGame.VisibleMap[y][x] == 1)
                    {
                        alreadyOpenSqaureClick++;

                        if (alreadyOpenSqaureClick > 5)
                        {
                            alreadyOpenSqaureClick = 0;
                            ActiveGame.Initialize();
                            continue;
                        }
                    }

                    ActiveGame.MakeGuess(y, x);
                    var(training, outputs) = network.Learn();

                    this.Dispatcher.Invoke(() =>
                    {
                        DrawMap();
                    });

                    //> draw training data
                    for (int i = 0; i < training.Length; i++)
                    {
                        var yT = i / ActiveGame.SizeY;
                        var xT = i % ActiveGame.SizeX;
                        this.Dispatcher.Invoke(() =>
                        {
                            var number = new TextBlock()
                            {
                                Text       = Math.Round(training[i] * 1000.0d).ToString(),
                                Foreground = new SolidColorBrush(Colors.Green),
                                FontSize   = 12
                            };

                            Canvas.SetTop(number, yT * YUnit + 4);
                            Canvas.SetLeft(number, xT * XUnit + 5);
                            CanvasElement.Children.Add(number);

                            var number2 = new TextBlock()
                            {
                                Text       = Math.Round(outputs[i] * 1000.0d).ToString(),
                                Foreground = new SolidColorBrush(Colors.Purple),
                                FontSize   = 12
                            };

                            Canvas.SetTop(number2, yT * YUnit + 15);
                            Canvas.SetLeft(number2, xT * XUnit + 5);
                            CanvasElement.Children.Add(number2);
                        });
                    }
                    //<
                    Thread.Sleep(500);

                    if (!ActiveGame.IsRunning)
                    {
                        updateFileIterator--;
                        this.Dispatcher.Invoke(() =>
                        {
                            //DrawMap();
                            //DrawMines();
                        });

                        if (updateFileIterator <= 0)
                        {
                            File.WriteAllText(neuralNetworkDataFilePath, JsonConvert.SerializeObject(
                                                  new NeuralNetworkData()
                            {
                                Weights = network.NeuralNetwork.Weights,
                                Bias    = network.NeuralNetwork.Bias,
                            }
                                                  ));
                            updateFileIterator = 10;
                        }

                        ActiveGame.Initialize();
                    }
                }
            });
        }
Beispiel #5
0
        public void UpdateEnvironment(string json)
        {
            MinesweeperState ms = JsonConvert.DeserializeObject <MinesweeperState>(json);

            UpdateEnvironment(ms);
        }