public Manager() { this.BackColor = Color.FromArgb(0, 0, 100); this.Size = new Size(blockSize * width + blockSize, blockSize * height + blockSize * 2); this.Text = "Snake"; Block[,] blocks = game.GetBoard(); areCheatsEnabled = false; for (int i = 0; i < height; i++) { board[i] = new Label[width]; for (int j = 0; j < width; j++) { Label tmp = new Label(); tmp.Location = new Point(j * blockSize, i * blockSize); tmp.BackColor = GetBlockColor(blocks[i, j]); //tmp.Text = i + ", " + j; tmp.Size = new Size(blockSize, blockSize); tmp.Tag = new SPoint(j, i); tmp.Click += LabelClick; board[i][j] = tmp; Controls.Add(tmp); } } buttons = new Button[3]; for (int i = 0; i < 3; i++) { buttons[i] = new Button(); buttons[i].Location = new Point(i * blockSize * 2, width * blockSize); buttons[i].Size = new Size(blockSize * 2, blockSize); buttons[i].BackColor = Color.FromName("White"); buttons[i].Tag = i; buttons[i].Click += buttonClick; Controls.Add(buttons[i]); } buttons[0].Text = "Up"; buttons[1].Text = "Left"; buttons[2].Text = "Right"; toggleCheats = new Button(); toggleCheats.Location = new Point(6 * blockSize, width * blockSize); toggleCheats.Size = new Size(blockSize * 2, blockSize); toggleCheats.BackColor = Color.FromName("White"); toggleCheats.Click += toggleCheat; toggleCheats.Text = "Toggle Cheats"; Controls.Add(toggleCheats); FormClosed += (object sender, FormClosedEventArgs e) => { Environment.Exit(Environment.ExitCode); }; }
private void Timer_Tick(object sender, EventArgs e) { if (!game.isEnd) { float[] input = game.SetOutput(); float[] output = network.FeedForward(input); game.Tick(output); SPoint[] diffs = game.GetDiff(); foreach (SPoint diff in diffs) { if (diff.y >= height || diff.x >= width || diff.x < 0 || diff.y < 0) { continue; } board[diff.y][diff.x].BackColor = GetBlockColor(game.GetBoard()[diff.y, diff.x]); } } else { Text = "Score - " + game.GetScore(); game = new SnakeGame(height, width); Block[,] blocks = game.GetBoard(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { board[i][j].BackColor = GetBlockColor(blocks[i, j]); } } network = population.GetBestNeuralNetwork(); generationLabel.Text = population.generation.ToString(); } }
public void buttonClick(object sender, EventArgs e) { Button snder = (Button)sender; int active = (int)snder.Tag; float[] input = new float[3]; for (int i = 0; i < 3; i++) { input[i] = 0; } input[active] = 1; game.Tick(input); if (game.isEnd && !areCheatsEnabled) { MessageBox.Show("You lost!", "Game Over!", MessageBoxButtons.OK, MessageBoxIcon.Information); game = new SnakeGame(height, width); Block[,] blocks = game.GetBoard(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { board[i][j].BackColor = GetBlockColor(blocks[i, j]); } } return; } SPoint[] diffs = game.GetDiff(); foreach (SPoint diff in diffs) { if (diff.y >= height || diff.x >= width || diff.x < 0 || diff.y < 0) { continue; } board[diff.y][diff.x].BackColor = GetBlockColor(game.GetBoard()[diff.y, diff.x]); } }