Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1.         {
  2.             // Inputs: {Snake Head Location (x,y), Apple Location (x,y)}
  3.             // Output: Array of Directions {DownValue,UpValue,RightValue,LeftValue}
  4.  
  5.             NeuralNetwork snakeNN = new NeuralNetwork(9, 5, 4);
  6.             while (!m_GameOver)
  7.             {
  8.                 Thread.Sleep(m_GameSpeed);
  9.                 m_Snake.Move();
  10.                 ShowXY();
  11.  
  12.                 if (m_Snake.isEatingApple(m_Apple))
  13.                 {
  14.                     m_Snake.Grow();
  15.                     m_Apple.GenerateNewRandomApple((int)eBoardSize.Width, (int)eBoardSize.Height);
  16.                     UpdateScore();
  17.                 }
  18.  
  19.                 double[] InputArr = {
  20.                     m_Snake.Head.X,
  21.                     m_Snake.Head.Y,
  22.                     Snake.DistanceFromApple(m_Snake.Head,m_Apple),
  23.                     check((float)(m_Snake.Head.X  + 1)% (float)eBoardSize.Width,m_Snake.Head.Y),
  24.                     check(m_Snake.Head.X,(float)(m_Snake.Head.Y + 1% (float)eBoardSize.Height)),
  25.                     check((float)(m_Snake.Head.X - 1)  % (float)eBoardSize.Width,m_Snake.Head.Y),
  26.                     check(m_Snake.Head.X,(float)(m_Snake.Head.Y -1) % (float)eBoardSize.Height),
  27.                     high,
  28.                     m_GameScore
  29.                 };
  30.                 double[] NNoutput = snakeNN.FeedForward(InputArr);
  31.                 double[] PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
  32.  
  33.                 int NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
  34.  
  35.                
  36.                
  37.                
  38.  
  39.                 bool collides = false;
  40.  
  41.                 for (int i = 0;i < m_Snake.SnakeSize;i++) {
  42.                     if (m_Snake.Head.X == m_Snake.m_Snake[i].X && m_Snake.Head.Y == m_Snake.m_Snake[i].Y && i != m_Snake.SnakeSize - 1
  43.                     && i != m_Snake.SnakeSize - 2)
  44.                      {
  45.                         /*m_Snake.m_Snake.Clear();
  46.                         m_Snake.m_Snake.Insert(0, new Point(10, 10, (char)Snake.eSnakeShapes.SnakeHead));
  47.                         snakeNN.TrainNetwork(InputArr, PcGuess, 0.3);
  48.                         NNoutput = snakeNN.FeedForward(InputArr);
  49.                         PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
  50.                         NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
  51.                         Console.WriteLine("Collide");
  52.                         break;
  53.                         */
  54.                         collides =true;
  55.                         break;
  56.                        
  57.                     }
  58.                 }
  59.  
  60.                 if ((PcGuess[NNGuessDirection] != 1 && PcGuess[NNGuessDirection] != 0.5))
  61.                 {
  62.                     snakeNN.TrainNetwork(InputArr, PcGuess, 0.3);
  63.                     NNoutput = snakeNN.FeedForward(InputArr);
  64.                     PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
  65.                     NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
  66.                 }
  67.  
  68.                 if (collides) {
  69.                    m_Snake.m_Snake.Clear();
  70.                    
  71.                         m_Snake.m_Snake.Insert(0, new Point(10, 10, (char)Snake.eSnakeShapes.SnakeHead));
  72.                         snakeNN.TrainNetwork(InputArr, PcGuess, 0.4);
  73.                         Console.Clear();
  74.                         if (m_GameScore > high) high = m_GameScore;
  75.                         m_GameScore = 0;
  76.                         gen++;
  77.                        
  78.                         InitBoard();
  79.                         continue;                    
  80.  
  81.                 } else {
  82.                    
  83.                 }
  84.  
  85.                
  86.  
  87.                 if (NNGuessDirection == 0)
  88.                 {
  89.                     m_Snake.Dir = Snake.eSnakeDirections.Right;
  90.                 }
  91.                 else if (NNGuessDirection == 1)
  92.                 {
  93.                     m_Snake.Dir = Snake.eSnakeDirections.Left;
  94.                 }
  95.                 else if (NNGuessDirection == 2)
  96.                 {
  97.                     m_Snake.Dir = Snake.eSnakeDirections.Down;
  98.                 }
  99.                 else if (NNGuessDirection == 3)
  100.                 {
  101.                     m_Snake.Dir = Snake.eSnakeDirections.Up;
  102.                 }
  103.  
  104.                 Console.SetCursorPosition(0, 21);
  105.  
  106.                 Console.WriteLine(string.Format("Right  : {0:P}", NNoutput[0]));
  107.                 Console.WriteLine(string.Format("Left   : {0:P}", NNoutput[1]));
  108.                 Console.WriteLine(string.Format("Down   : {0:P}", NNoutput[2]));
  109.                 Console.WriteLine(string.Format("Up     : {0:P}", NNoutput[3]));
  110.  
  111.             }
  112.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement