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(); }
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()); }