Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. //умные повороты
  2. public void TurnUp()
  3. {
  4. if (direction != 3 && !Check(HeadX(), (HeadY() - 1 + NY) % NY))
  5. ChangeDirection(1);
  6. else if (direction != 2 && !Check((HeadX() - 1 + NX) % NX, HeadY()))
  7. ChangeDirection(4);
  8. else if (direction != 4 && !Check((HeadX() + 1) % NX, HeadY()))
  9. ChangeDirection(2);
  10. else
  11. ChangeDirection(3);
  12.  
  13. }
  14. public void TurnRight()
  15. {
  16. if (direction != 4 && !Check((HeadX() + 1) % NX, HeadY()))
  17. ChangeDirection(2);
  18. else if (direction != 3 && !Check(HeadX(), (HeadY() - 1 + NY) % NY))
  19. ChangeDirection(1);
  20. else if (direction != 1 && !Check(HeadX(), (HeadY() + 1) % NY))
  21. ChangeDirection(3);
  22. else
  23. ChangeDirection(4);
  24. }
  25. public void TurnDown()
  26. {
  27. if (direction != 1 && !Check(HeadX(), (HeadY() + 1) % NY))
  28. ChangeDirection(3);
  29. else if (direction != 2 && !Check((HeadX() - 1 + NX) % NX, HeadY()))
  30. ChangeDirection(4);
  31. else if (direction != 4 && !Check((HeadX() + 1) % NX, HeadY()))
  32. ChangeDirection(2);
  33. else
  34. ChangeDirection(1);
  35. }
  36. public void TurnLeft()
  37. {
  38. if (direction != 2 && !Check((HeadX() - 1 + NX) % NX, HeadY()))
  39. ChangeDirection(4);
  40. else if (direction != 3 && !Check(HeadX(), (HeadY() - 1 + NY) % NY))
  41. ChangeDirection(1);
  42. else if (direction != 1 && !Check(HeadX(), (HeadY() + 1) % NY))
  43. ChangeDirection(3);
  44. else
  45. ChangeDirection(2);
  46. }
  47.  
  48. public void AI(int X, int Y)
  49. {
  50. Random rnd = new Random();
  51. if (rnd.Next(1, 11)>=3)
  52. {
  53. if (rnd.Next(1, 3) == 2)
  54. {
  55. if (X > HeadX())
  56. {
  57. if (NX - X + HeadX() > X - HeadX())
  58. TurnRight();
  59.  
  60. else
  61. TurnLeft();
  62. }
  63. else if (X < HeadX())
  64. {
  65. if (NX - HeadX() + X > HeadX() - X)
  66. TurnLeft();
  67.  
  68. else if (NX - X + HeadX() < X - HeadX())
  69. TurnRight();
  70. }
  71. }
  72. else
  73. {
  74. if (Y > HeadY())
  75. {
  76. if (NY - Y + HeadY() > Y - HeadY())
  77. TurnDown();
  78.  
  79. else
  80. TurnUp();
  81. }
  82. else if (Y < HeadY())
  83. {
  84. if (NY - HeadY() + Y > HeadY() - Y)
  85. TurnUp();
  86.  
  87. else
  88. TurnDown();
  89. }
  90.  
  91. }
  92. }
  93. else
  94. {
  95. ChangeDirection(rnd.Next(1, 5));
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement