Example #1
0
        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]);
            }
        }