Пример #1
0
        private void Button3_Click(object sender, EventArgs e)
        {
            VsAI[0] = false;
            if (VsAI[2])
            {
                VsAI[2] = false; Buttons[2].Enabled = false; return;
            }
            VsAI[2] = true;

            SelectedPiece = null; PossibleMoves = null;
            new Task(() => { PanelUpdate(); }).Start();

            var nn1 = new NN();
            var nn2 = new NN();

            if (ResetNN)
            {
                nn1.Init(); nn2.Init();
            }
            else
            {
                nn1 = IO.Read(0); nn2 = IO.Read(0);
            }
            nn1 = nn1.SetColor(true); nn2 = nn2.SetColor(false);

            var thread =
                new Thread(() =>
            {
                Board Compitition = ActiveBoard;
                int movecount     = 0;
                //Compete until a victor is decided or movecount is exceeded
                do
                {
                    if (nn1.player.IsW == Compitition.WTurn)
                    {
                        Compitition = nn1.Move(Compitition);
                    }
                    else
                    {
                        Compitition = nn2.Move(Compitition);
                    }

                    Invoke((Action) delegate { ActiveBoard = Compitition; Buttons[2].Enabled = true; });
                    movecount++;
                }while (VsAI[2] && !Compitition.WWin && !Compitition.BWin && movecount < MaxMoves);
            });

            thread.IsBackground = true;
            thread.Start();
        }
Пример #2
0
        public static void Write(NN nn, int num)
        {
            var fs = new FileStream(BasePath + "\\WBs\\" + num.ToString() + ".txt", FileMode.Create, FileAccess.Write, FileShare.None);
            var sw = new StreamWriter(fs);

            sw.Write(nn.NumLayers + " ");
            foreach (Layer l in nn.Layers)
            {
                sw.Write(l.Length + " " + l.InputLength + " ");
                for (int i = 0; i < l.Length; i++)
                {
                    for (int ii = 0; ii < l.InputLength; ii++)
                    {
                        sw.Write(l.Weights[i, ii] + " ");
                    }
                    sw.Write(l.Biases[i] + " ");
                }
            }
            sw.Close(); fs.Close();
        }
Пример #3
0
        public static NN Read(int num)
        {
            NN nn = new NN();

            if (Running)
            {
                throw new Exception("Already accessing file");
            }
            Running = true;
            var    fs   = new FileStream(BasePath + "\\WBs\\" + num.ToString() + ".txt", FileMode.Open, FileAccess.Read, FileShare.None);
            var    sr   = new StreamReader(fs);
            string text = sr.ReadToEnd();

            sr.Close(); fs.Close();
            string[] split = text.Split(' ');

            int numlayers = int.Parse(split[0]);

            nn.Layers = new List <Layer>();

            int iterator = 1;

            for (int j = 0; j < numlayers; j++)
            {
                int length      = int.Parse(split[iterator]); iterator++;
                int inputlength = int.Parse(split[iterator]); iterator++;
                nn.Layers.Add(new Layer(length, inputlength));
                for (int i = 0; i < length; i++)
                {
                    for (int ii = 0; ii < inputlength; ii++)
                    {
                        nn.Layers[j].Weights[i, ii] = double.Parse(split[iterator]);
                        iterator++;
                    }
                    nn.Layers[j].Biases[i] = double.Parse(split[iterator]);
                    iterator++;
                }
            }
            Running = false;
            return(nn);
        }
Пример #4
0
        public Form1()
        {
            ActiveBoard = new Board(new Player(true), new Player(false), new Piece[8, 8], true).initBoard();
            ActiveNN    = new NN();

            if (ResetNN)
            {
                ActiveNN.Init(); new Task(() => { IO.Write(ActiveNN, 0); }).Start();
            }
            else
            {
                new Task(() => { ActiveNN = IO.Read(0); }).Start();
            }

            MaximizeBox = false;
            InitializeComponent();
            Text = "NotJustChess";
            Icon = new Icon(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JCPawn.Ico"));
            for (int i = 0; i < 8; i++)
            {
                for (int ii = 0; ii < 8; ii++)
                {
                    var newPanel = new Panel
                    {
                        Size     = new Size(tilesize, tilesize),
                        Location = new Point(tilesize * i, tilesize * ii)
                    };

                    newPanel.Click += newPanel_Click;
                    newPanel.BackgroundImageLayout = ImageLayout.Center;
                    if (!(ActiveBoard.Pieces[ii, i] is Empty))
                    {
                        newPanel.BackgroundImage = ScaleImage(ActiveBoard.Pieces[ii, i].PieceImage, 40);
                        newPanel.Anchor          = (AnchorStyles.Top | AnchorStyles.Left);
                    }

                    //Add panel to controls
                    Controls.Add(newPanel);
                    //Add panel to board
                    BoardPanel[i, ii] = newPanel;

                    //Color the board
                    if (i % 2 == 0)
                    {
                        newPanel.BackColor = ii % 2 != 0 ? Color.Black : Color.White;
                    }
                    else
                    {
                        newPanel.BackColor = ii % 2 != 0 ? Color.White : Color.Black;
                    }
                }
            }
            Button button = new Button
            {
                Size     = new Size(tilesize * 2, tilesize / 2),
                Location = new Point(tilesize * 2, tilesize * 8),
                Text     = "Reset"
            };

            Buttons[0]    = button;
            button.Click += Button_Click;
            Controls.Add(button);
            Button button2 = new Button
            {
                Size     = new Size(tilesize * 2, tilesize / 2),
                Location = new Point(tilesize * 4, tilesize * 8),
                Text     = "Train"
            };

            Buttons[1]     = button2;
            button2.Click += Button2_Click;
            Controls.Add(button2);
            Button button3 = new Button
            {
                Size     = new Size(tilesize * 2, tilesize / 2),
                Location = new Point(tilesize * 6, tilesize * 8),
                Text     = "AIvsAI"
            };

            Buttons[2]     = button3;
            button3.Click += Button3_Click;
            Controls.Add(button3);
            Button button4 = new Button
            {
                Size     = new Size(tilesize * 2, tilesize / 2),
                Location = new Point(0, tilesize * 8),
                Text     = "Vs AI"
            };

            Buttons[3]     = button4;
            button4.Click += Button4_Click;
            Controls.Add(button4);


            //Set dynamic scaling
            this.ResizeEnd += Form1_Resize;
            formsize        = new int[] { 0, 0 };
            Size            = new Size(500, 500);
            Form1_Resize(this, new EventArgs());
        }