Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- // Inputs: {Snake Head Location (x,y), Apple Location (x,y)}
- // Output: Array of Directions {DownValue,UpValue,RightValue,LeftValue}
- NeuralNetwork snakeNN = new NeuralNetwork(9, 5, 4);
- while (!m_GameOver)
- {
- Thread.Sleep(m_GameSpeed);
- m_Snake.Move();
- ShowXY();
- if (m_Snake.isEatingApple(m_Apple))
- {
- m_Snake.Grow();
- m_Apple.GenerateNewRandomApple((int)eBoardSize.Width, (int)eBoardSize.Height);
- UpdateScore();
- }
- double[] InputArr = {
- m_Snake.Head.X,
- m_Snake.Head.Y,
- Snake.DistanceFromApple(m_Snake.Head,m_Apple),
- check((float)(m_Snake.Head.X + 1)% (float)eBoardSize.Width,m_Snake.Head.Y),
- check(m_Snake.Head.X,(float)(m_Snake.Head.Y + 1% (float)eBoardSize.Height)),
- check((float)(m_Snake.Head.X - 1) % (float)eBoardSize.Width,m_Snake.Head.Y),
- check(m_Snake.Head.X,(float)(m_Snake.Head.Y -1) % (float)eBoardSize.Height),
- high,
- m_GameScore
- };
- double[] NNoutput = snakeNN.FeedForward(InputArr);
- double[] PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
- int NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
- bool collides = false;
- for (int i = 0;i < m_Snake.SnakeSize;i++) {
- 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
- && i != m_Snake.SnakeSize - 2)
- {
- /*m_Snake.m_Snake.Clear();
- m_Snake.m_Snake.Insert(0, new Point(10, 10, (char)Snake.eSnakeShapes.SnakeHead));
- snakeNN.TrainNetwork(InputArr, PcGuess, 0.3);
- NNoutput = snakeNN.FeedForward(InputArr);
- PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
- NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
- Console.WriteLine("Collide");
- break;
- */
- collides =true;
- break;
- }
- }
- if ((PcGuess[NNGuessDirection] != 1 && PcGuess[NNGuessDirection] != 0.5))
- {
- snakeNN.TrainNetwork(InputArr, PcGuess, 0.3);
- NNoutput = snakeNN.FeedForward(InputArr);
- PcGuess = GetPcPMove(m_Snake.Head.X, m_Snake.Head.Y, m_Apple.X, m_Apple.Y);
- NNGuessDirection = NNoutput.ToList().IndexOf(NNoutput.Max());
- }
- if (collides) {
- m_Snake.m_Snake.Clear();
- m_Snake.m_Snake.Insert(0, new Point(10, 10, (char)Snake.eSnakeShapes.SnakeHead));
- snakeNN.TrainNetwork(InputArr, PcGuess, 0.4);
- Console.Clear();
- if (m_GameScore > high) high = m_GameScore;
- m_GameScore = 0;
- gen++;
- InitBoard();
- continue;
- } else {
- }
- if (NNGuessDirection == 0)
- {
- m_Snake.Dir = Snake.eSnakeDirections.Right;
- }
- else if (NNGuessDirection == 1)
- {
- m_Snake.Dir = Snake.eSnakeDirections.Left;
- }
- else if (NNGuessDirection == 2)
- {
- m_Snake.Dir = Snake.eSnakeDirections.Down;
- }
- else if (NNGuessDirection == 3)
- {
- m_Snake.Dir = Snake.eSnakeDirections.Up;
- }
- Console.SetCursorPosition(0, 21);
- Console.WriteLine(string.Format("Right : {0:P}", NNoutput[0]));
- Console.WriteLine(string.Format("Left : {0:P}", NNoutput[1]));
- Console.WriteLine(string.Format("Down : {0:P}", NNoutput[2]));
- Console.WriteLine(string.Format("Up : {0:P}", NNoutput[3]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement