Exemple #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);
        }
Exemple #2
0
        public double resultOfMove(moves move)
        {
            int       distanceBefore = distanceToFood();
            int       scoreBefore    = score;
            moves     closestMove    = moves.Forward;
            SnakeGame temp           = (SnakeGame)this.Clone();

            temp.moveHead(move);
            int scoreAfter = temp.score;

            if (temp.gameOver == true)
            {
                return(-1.00);
            }
            if (scoreAfter > scoreBefore)
            {
                return(5.00);
            }
            if (temp.distanceToFood() < distanceBefore)
            {
                return(1.00);
            }

            return(0);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            /*
             * ConsoleKey input;
             * SnakeGame sTest = new SnakeGame(20, 20);
             *
             * SnakeGame snakeyBoi = new SnakeGame((SnakeGame)sTest.Clone());
             * //snakeyBoi = (SnakeGame)sTest.Clone();
             * snakeyBoi.DisplayBoard();
             * snakeyBoi.curDirection = Direction.Right;
             *
             * while (snakeyBoi.gameOver == false)
             * {
             *  moves move = moves.Forward;
             *  Stopwatch timer = new Stopwatch();
             *  timer.Start();
             *  while (timer.Elapsed.TotalMilliseconds < 100)
             *  {
             *      if (Console.KeyAvailable)
             *      {
             *          input = Console.ReadKey().Key;
             *          if (input == ConsoleKey.UpArrow)
             *          {
             *              move = moves.Forward;
             *          }
             *
             *          if (input == ConsoleKey.LeftArrow)
             *          {
             *              move = moves.Left;
             *          }
             *          if (input == ConsoleKey.RightArrow)
             *          {
             *              move = moves.Right;
             *          }
             *      }
             *  }
             *  timer.Stop();
             *  snakeyBoi.moveHead(move);
             *  snakeyBoi.DisplayBoard();
             * }
             * Console.WriteLine("Game OVER!!");
             *
             * Console.ReadLine();
             * Console.Clear();
             * sTest.DisplayBoard();
             * Console.ReadLine();
             */
            SnakeGame snakeyBoi = new SnakeGame(10, 10);

            snakeyBoi.curDirection = Direction.Right;
            while (snakeyBoi.gameOver == false)
            {
                double resultForward = snakeyBoi.resultOfMove(moves.Forward);
                double resultLeft    = snakeyBoi.resultOfMove(moves.Left);
                double resultRight   = snakeyBoi.resultOfMove(moves.Right);
                moves  move          = moves.Right;
                if (resultForward > resultLeft)
                {
                    if (resultForward > resultRight)
                    {
                        move = moves.Forward;
                    }
                }
                else
                {
                    if (resultLeft > resultRight)
                    {
                        move = moves.Left;
                    }
                }
                snakeyBoi.moveHead(move);
                snakeyBoi.DisplayBoard();
                System.Threading.Thread.Sleep(1000);
            }
            snakeyBoi.DisplayBoard();
            Console.WriteLine("Game OVER!!");
            Console.ReadLine();

            /*
             * GAClass ga = new GAClass(snakeyBoi);
             * NeuralNetwork bestnn = ga.population[5];
             * //NeuralNetwork bestnn = ga.bestNN();
             * Stopwatch timer = new Stopwatch();
             * Console.Clear();
             * while (snakeyBoi.gameOver == false)
             * {
             *
             *  moves move = bestnn.calculateDirection(snakeyBoi.getInputs());
             *  snakeyBoi.moveHead(move);
             *  snakeyBoi.DisplayBoard();
             *  System.Threading.Thread.Sleep(1000);
             *
             * }
             *
             * snakeyBoi.DisplayBoard();
             * Console.WriteLine("Game OVER!!");
             * Console.ReadLine();
             */
        }