示例#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;
        }
示例#2
0
        public LMD_Game(Control parent)
        {
            this.parent    = parent;
            this.BackColor = Color.FromArgb(100, 100, 100);
            this.topBoard  = ((Form1)parent).topBoard;
            this.Size      = new Size(parent.Width / 2 - 2, parent.Height - topBoard.Height - 2);
            this.Location  = new Point(1, topBoard.Bottom);
            this.Paint    += (o, e) =>
            {
                Graphics g = e.Graphics;
                g.Clear(this.BackColor);
                Pen pen = new Pen(Color.FromArgb(192, 192, 192), 10);

                g.DrawLine(pen, 0, this.Height / 3, this.Width, this.Height / 3);
                g.DrawLine(pen, 0, this.Height / 3 + this.Height / 3, this.Width, this.Height / 3 + this.Height / 3);

                g.DrawLine(pen, this.Width / 3, 0, this.Width / 3, this.Height);
                g.DrawLine(pen, this.Width / 3 + this.Width / 3, 0, this.Width / 3 + this.Width / 3, this.Height);
            };
            parent.Controls.Add(this);

            but = new Button[9];
            for (int i = 0; i < but.Length; i++)
            {
                but[i] = new Button();
                but[i].FlatAppearance.BorderSize = 0;
                but[i].FlatStyle = FlatStyle.Flat;
                //	but[i].BackColor = Color.FromArgb(192, 192, 192);
                but[i].Font      = new System.Drawing.Font("Arial", 40f, FontStyle.Bold);
                but[i].ForeColor = Color.FromArgb(220, 120, 50);
                but[i].Location  = new Point(0, 0);
                but[i].FlatAppearance.MouseDownBackColor = Color.FromArgb(130, 130, 130);
                but[i].Size   = new Size(this.Width / 3 - 2, this.Height / 3 - 2);
                but[i].Click += (o, e) =>
                {
                    if (enable)
                    {
                        List <float> x = getVectorX();

                        if (step != null)
                        {
                            for (int j = 0; j < but.Length; j++)
                            {
                                if (but[j].ContainsFocus)
                                {
                                    if (CheckRect(j + 1))
                                    {
                                        but[j].Text = "x";
                                        step(x, j + 1, "x");
                                    }
                                }
                            }
                        }
                    }
                };

                this.Controls.Add(but[i]);
            }
            but[1].Location = new Point(but[0].Right + 4, 0);
            but[2].Location = new Point(but[1].Right + 4, 0);
            but[3].Location = new Point(0, but[0].Bottom + 4);
            but[4].Location = new Point(but[3].Right + 4, but[0].Bottom + 4);
            but[5].Location = new Point(but[4].Right + 4, but[0].Bottom + 4);
            but[6].Location = new Point(0, but[5].Bottom + 4);
            but[7].Location = new Point(but[6].Right + 4, but[5].Bottom + 4);
            but[8].Location = new Point(but[7].Right + 4, but[5].Bottom + 4);
        }
示例#3
0
        public LMD_Information(Control parent)
        {
            this.parent    = parent;
            this.BackColor = Color.FromArgb(100, 100, 100);
            this.topBoard  = ((Form1)parent).topBoard;
            this.Size      = new Size(parent.Width / 2 - 2, parent.Height - topBoard.Height - 2);
            this.Location  = new Point(parent.Width / 2 + 1, topBoard.Bottom);
            parent.Controls.Add(this);

            listBox             = new ListBox();
            listBox.BackColor   = Color.FromArgb(120, 120, 120);
            listBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            listBox.Size        = new Size(this.Width - 20, this.Height - 100);
            listBox.ForeColor   = Color.FromArgb(192, 192, 192);
            listBox.Location    = new Point(10, 4);
            this.Controls.Add(listBox);

            listBox.Items.Add("------------------------------------------------ Tic-Tac-Toe -----------------------------------------------------------------");

            rtb = new RichTextBox[4];
            for (int i = 0; i < rtb.Length; i++)
            {
                rtb[i]             = new RichTextBox();
                rtb[i].BorderStyle = System.Windows.Forms.BorderStyle.None;
                rtb[i].BackColor   = Color.FromArgb(140, 140, 140);
                rtb[i].Size        = new Size(30, 15);
                rtb[i].Location    = new Point(100, listBox.Bottom);
                rtb[i].ForeColor   = Color.FromArgb(70, 70, 70);
                this.Controls.Add(rtb[i]);
            }
            rtb[0].Width    = 40;
            rtb[0].Location = new Point(65, listBox.Bottom + 21);
            rtb[1].Location = new Point(80, listBox.Bottom + 60);
            rtb[2].Location = new Point(170, listBox.Bottom + 60);
            rtb[3].Location = new Point(262, listBox.Bottom + 60);

            but = new Button[4];
            for (int i = 0; i < but.Length; i++)
            {
                but[i] = new Button();
                but[i].FlatAppearance.BorderSize = 0;
                but[i].FlatStyle = FlatStyle.Flat;
                but[i].Size      = new Size(80, 100 / 4 - 2);
                but[i].BackColor = Color.FromArgb(50, 50, 50);
                but[i].ForeColor = Color.FromArgb(150, 150, 150);
                but[i].Font      = new Font("Arial", 9);

                but[i].Click += (o, e) =>
                {
                    try
                    {
                        Button b = (Button)o;

                        switch (b.Text)
                        {
                        case "Обучить": if (remember != null)
                            {
                                remember(int.Parse(rtb[0].Text));
                            }
                            break;

                        case "Сохранить": if (save != null)
                            {
                                save();
                            }
                            break;

                        case "Загрузить": if (load != null)
                            {
                                load();
                            }
                            break;

                        case "Correct": if (correct != null)
                            {
                                correct(float.Parse(rtb[1].Text), int.Parse(rtb[2].Text), int.Parse(rtb[3].Text));
                            }
                            break;
                        }
                    }
                    catch { MessageBox.Show("Не корректная запись в поле!"); }
                };

                this.Controls.Add(but[i]);
            }
            but[0].Location = new Point(115, listBox.Bottom + 16);
            but[1].Location = new Point(200, listBox.Bottom + 16);
            but[2].Location = new Point(280, listBox.Bottom + 16);
            but[3].Location = new Point(300, listBox.Bottom + 56);
            but[3].Width    = 60;
            but[3].Text     = "Correct";
            but[0].Text     = "Обучить";
            but[1].Text     = "Сохранить";
            but[2].Text     = "Загрузить";

            Label[] lab = new Label[4];
            for (int i = 0; i < lab.Length; i++)
            {
                lab[i]           = new Label();
                lab[i].ForeColor = Color.FromArgb(12, 192, 192);
                lab[i].Font      = new Font("Arial", 10f);
                this.Controls.Add(lab[i]);
            }
            lab[0].Location = new Point(7, listBox.Bottom + 20);
            lab[0].Text     = "Индекс: ";

            lab[1].Location = new Point(7, listBox.Bottom + 60);
            lab[1].Text     = "Скорость: ";

            lab[2].Width    = 60;
            lab[2].Location = new Point(115, listBox.Bottom + 60);
            lab[2].Text     = "Дельта: ";

            lab[3].Location = new Point(207, listBox.Bottom + 60);
            lab[3].Text     = "Индекс: ";

            this.Paint += (o, e) =>
            {
                Graphics g = e.Graphics;
                g.Clear(this.BackColor);
                Pen pen = new Pen(Color.FromArgb(30, 120, 30), 3);

                g.DrawRectangle(pen, 5, listBox.Bottom + 9, this.Width - 10, 35);
                g.DrawRectangle(pen, 5, listBox.Bottom + 50, this.Width - 10, 35);
            };
        }