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 SnakeGame(SnakeGame sg)
 {
     score        = sg.score;
     gridWidth    = sg.gridWidth;
     gridHeight   = sg.gridHeight;
     snakeBody    = sg.snakeBody;
     board        = (Box[, ])sg.board.Clone();
     foodLocation = (int[])sg.foodLocation.Clone();
     updateBoard();
 }
Exemple #5
0
 public void Train()
 {
     for (int i =0; i < iterations;i++)
     {
         while (snake.GameOver==false)
         {
             SnakeGame temp = (SnakeGame)snake.Clone();  
             
         }
     }
 }
Exemple #6
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 #7
0
        /*
         * public SnakeGame ShallowClone()
         * {
         *   return (SnakeGame)this.MemberwiseClone();
         * }
         */
        public SnakeGame DeepCopy()
        {
            SnakeGame other = (SnakeGame)this.MemberwiseClone();

            other.score      = score;
            other.gridWidth  = gridWidth;
            other.gridHeight = gridHeight;

            List <int[]> newList = new List <int[]>(snakeBody.Count);

            foreach (int[] item in snakeBody)
            {
                newList.Add((int[])item.Clone());
            }
            other.snakeBody = newList;

            other.board        = (Box[, ])board.Clone();
            other.foodLocation = (int[])foodLocation.Clone();

            return(other);
        }
Exemple #8
0
        public void Train()
        {
            int       t    = 0;
            SnakeGame temp = new SnakeGame((SnakeGame)snake.Clone());

            while (t < iterations)
            {
                Console.WriteLine("Iteration:" + t);
                List <NeuralNetwork> newPop = new List <NeuralNetwork>();
                while (newPop.Count < population.Count)
                {
                    if (temp.gameOver == true)
                    {
                        temp = new SnakeGame((SnakeGame)snake.Clone());
                    }
                    NeuralNetwork bestNN1     = new NeuralNetwork();
                    NeuralNetwork bestNN2     = new NeuralNetwork();
                    int           bestResult1 = -1;
                    int           bestResult2 = -1;
                    for (int i = 0; i < 10; i++)
                    {
                        int           nextnum = Rgen.Next(population.Count);
                        NeuralNetwork curNN   = population[i];

                        //int curResult = playGameGetScore(curNN);
                        int curResult = getResult(curNN, new SnakeGame((SnakeGame)temp.Clone()));
                        if (curResult > bestResult1)
                        {
                            bestResult1 = curResult;
                            bestNN1     = curNN;
                        }
                    }
                    for (int i = 0; i < 10; i++)
                    {
                        int           nextnum = Rgen.Next(population.Count);
                        NeuralNetwork curNN   = population[i];

                        //int curResult = playGameGetScore(curNN);
                        int curResult = getResult(curNN, new SnakeGame((SnakeGame)temp.Clone()));
                        if (curResult > bestResult2)
                        {
                            bestResult2 = curResult;
                            bestNN2     = curNN;
                        }
                    }

                    NeuralNetwork crossedNN = crossGen(bestNN1, bestNN2);
                    crossedNN = Mutate(crossedNN);
                    newPop.Add(crossedNN);
                }
                // int nextnum2 = Rgen.Next(newPop.Count);
                //temp.MakeMove(newPop[nextnum2].calculateDirection(temp.outputBox()));
                List <NeuralNetwork> newList = new List <NeuralNetwork>(newPop.Count);
                foreach (NeuralNetwork item in newPop)
                {
                    newList.Add(item);
                }
                population = newList;
                temp.moveHead(population[0].calculateDirection(temp.getInputs()));
                t++;
            }
        }
Exemple #9
0
 public GAClass(SnakeGame s)
 {
     this.snake = s;
     genPop();
     Train();
 }
Exemple #10
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();
             */
        }