Exemplo n.º 1
0
        public int getResult(NeuralNetwork nn, SnakeGame temp)
        {
            int result = 0;
            int runs   = 0;

            while (temp.gameOver == false)
            {
                if (runs > 5)
                {
                    return(result);
                }
                runs++;
                int distanceBefore = temp.distanceToFood();
                temp.moveHead(nn.calculateDirection(temp.getInputs()));

                if (temp.gameOver == true)
                {
                    return(result);
                }
                else
                {
                    result++;
                    if (temp.distanceToFood() < distanceBefore)
                    {
                        result = result + 5;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public int playGameGetScore(NeuralNetwork nn)
        {
            SnakeGame temp = new SnakeGame((SnakeGame)snake.Clone());

            while (temp.gameOver == false)
            {
                temp.moveHead(nn.calculateDirection(temp.getInputs()));
            }
            return(temp.score);
        }
Exemplo n.º 3
0
        public NeuralNetwork bestNN()
        {
            int           bestScore = -1;
            NeuralNetwork bestNN    = null;

            for (int i = 0; i < population.Count; i++)
            {
                SnakeGame     temp = new SnakeGame((SnakeGame)snake.Clone());
                NeuralNetwork nn   = population[i];
                while (temp.gameOver == false)
                {
                    temp.moveHead(nn.calculateDirection(temp.getInputs()));
                }
                if (temp.score > bestScore)
                {
                    bestScore = temp.score;
                    bestNN    = nn;
                }
            }
            return(bestNN);
        }