コード例 #1
0
        public AutoRemember(LMD_Game game, LMD_Information info, Neural_Network.Net net, ManagerHistory mHistory, Form1 form)
        {
            this.game     = game;
            this.info     = info;
            this.net      = net;
            this.mHistory = mHistory;
            this.form     = form;

            timer          = new System.Windows.Forms.Timer();
            timer.Interval = 10;
            timer.Tick    += timer_Tick;
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: ArtLait/LDM___tic-tac-toe
        private void Form1_Load(object sender, EventArgs e)
        {
            #region Initialize
            net      = new Neural_Network.Net();
            topBoard = new GUI.LMD_TopBoard(this, false);
            info     = new GUI.LMD_Information(this);
            game     = new GUI.LMD_Game(this);
            mHistory = new GUI.ManagerHistory();
            autoRem  = new GUI.AutoRemember(game, info, net, mHistory, this);
            #endregion
            #region changedState
            topBoard.changedState += (state) =>
            {
                this.state = state;

                switch (state)
                {
                case STATE_PROGRAM.auto:
                    info.WriteLine("Включен режим автоматического обучения");
                    autoRem.Start();
                    count_step = 0;
                    break;

                case STATE_PROGRAM.game:
                    info.WriteLine("Включен режим игрового обучения");
                    autoRem.Stop();
                    count_step = 0;
                    this.Text  = "LMD Tic-Tac-Toe";
                    break;

                case STATE_PROGRAM.handle:
                    info.WriteLine("Включен режим ручного обучения");
                    autoRem.Stop();
                    count_step = 0;
                    this.Text  = "LMD Tic-Tac-Toe";
                    break;
                }
                game.enable = true;
            };
            #endregion
            #region Clear
            topBoard.butClick += () =>
            {
                info.WriteLine("Поле было очищено");
                game.Clear();
                info.listBox.Items.Clear();
                info.listBox.Items.Add("------------------------------------------------ Tic-Tac-Toe -----------------------------------------------------------------");
                count_step  = 0;
                game.enable = true;
            };
            #endregion
            #region Remember
            info.remember += (int index) =>
            {
                net.AddNeuron(index, game.getVectorX(), info.listBox);

                step_x(game.getVectorX(), index);
            };
            #endregion
            #region Save
            info.save += () =>
            {
                Save();
            };
            #endregion
            #region Load
            info.load += () =>
            {
                LoadNS();
            };
            #endregion
            #region Correct
            info.correct += (float speed, int delta, int index) =>
            {
                net.Correct(game.getVectorX(), index, speed, delta, info.listBox);
            };
            #endregion
            #region step
            game.step += (List <float> x, int index, String str) =>
            {
                count_step++;

                #region Handle
                if (state == STATE_PROGRAM.handle)
                {
                    info.WriteLine("Ход x: " + index);

                    if (net.neurons.Count <= 0)
                    {
                        info.WriteLine("Заблокировано");
                        MessageBox.Show("Нужно сеть обучить!");
                    }
                    else
                    {
                        if (str == "x")
                        {
                            int     o_index = net.Recognize(game.getVectorX(), info.listBox);
                            Boolean b_rect  = game.CheckRect(o_index);

                            if (b_rect)
                            {
                                info.WriteLine("Ход о: " + o_index);
                                game.setO(o_index);
                            }
                            else if (!b_rect)
                            {
                                info.WriteLine("Жизнь нейронную сеть к такому не готовила");
                                MessageBox.Show("Сеть не знает что делать!");
                            }
                        }
                    }
                }
                #endregion
                #region Game
                if (state == STATE_PROGRAM.game)
                {
                    if (game.CheckWinX())
                    {
                        info.WriteLine("Крестики победили!");
                        mHistory.add(game.getTableConvertHistory(), index, game.getVectorX());

                        List <GUI.History> history = mHistory.getHistory();
                        int num = 0;

                        foreach (GUI.History h in history)
                        {
                            num++;
                            int[] table = h.table;
                            info.WriteLine(num + " - index[" + h.index + "]");
                            info.WriteLine(table[0] + "|" + table[1] + "|" + table[2]);
                            info.WriteLine(table[3] + "|" + table[4] + "|" + table[5]);
                            info.WriteLine(table[6] + "|" + table[7] + "|" + table[8]);
                        }

                        int          _index = history[history.Count - 1].index;
                        List <float> _x     = history[history.Count - 2].x;

                        info.WriteLine("Требуемый нейрон[" + _index + "]");

                        net.AddNeuron(_index, _x, info.listBox);

                        mHistory.Clear();
                        game.enable = false;
                    }
                    else if (game.CheckWinO())
                    {
                        info.WriteLine("Нолики победили!");
                        mHistory.add(game.getTableConvertHistory(), index, game.getVectorX());
                        autoRem.win_o();
                        mHistory.Clear();
                        game.enable = false;
                    }
                    else if (game.CheckNoWin())
                    {
                        info.WriteLine("Ничья!");
                        mHistory.Clear();
                        game.enable = false;
                    }
                    else
                    {
                        if (count_step <= 8)
                        {
                            if (str == "x")
                            {
                                step_x(game.getVectorX(), index);
                                mHistory.add(game.getTableConvertHistory(), index, game.getVectorX());
                            }
                            else if (str == "o")
                            {
                            }
                        }
                        else
                        {
                            count_step = 0;
                        }
                    }
                }
                #endregion
            };
            #endregion

            info.WriteLine("Включен режим игрового обучения");
            this.state = Form1.STATE_PROGRAM.game;
        }