Exemple #1
0
        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;
        }
Exemple #2
0
        private void timer_Tick(object sender, EventArgs e)
        {
            form.Text = "Tic-Tac-Toe Round[" + game.count_round + "] X[" + game.count_win_x + "] O[" + game.count_win_o + "] N[" + net.neurons.Count + "]";
            count_step++;

            if (count_step >= 9)
            {
                count_step = 0;
                game.Clear();
                game.count_round++;
            }
            else
            {
                int index = 0;

                if (game.present_step == TYPE_STEP.X)
                {
                    index = game.setRandom(TYPE_STEP.X);
                }
                else if (game.present_step == TYPE_STEP.O)
                {
                    index = net.Recognize(game.getVectorX(), info.listBox);

                    if (game.CheckRect(index))
                    {
                        game.setO(index);
                        info.WriteLine("Нейрон активирован");
                    }
                    else
                    {
                        index = game.setRandom(TYPE_STEP.O);
                        info.WriteLine("Нейрон активирован");
                    }
                }

                mHistory.add(game.getTableConvertHistory(), index, game.getVectorX());

                if (game.CheckWinX())
                {
                    count_step = 0;
                    info.WriteLine("Победил X!");
                    win_x();

                    game.count_win_x++;
                    game.Clear();
                    mHistory.Clear();
                    game.present_step = TYPE_STEP.X;
                }
                else if (game.CheckWinO())
                {
                    count_step = 0;
                    info.WriteLine("Победил O!");
                    win_o();

                    game.count_win_o++;
                    game.Clear();
                    mHistory.Clear();
                    game.present_step = TYPE_STEP.X;
                }
                else if (game.CheckNoWin())
                {
                    count_step = 0;
                    info.WriteLine("Ничья!");
                    win_x();
                    game.Clear();
                    game.count_no_win++;
                    mHistory.Clear();
                    game.present_step = TYPE_STEP.X;
                }

                if (info.listBox.Items.Count >= 5000)
                {
                    info.listBox.Items.Clear();
                }
            }
        }