snake.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package snakesimulator
  26. func (p Point) Copy() (pCopy *Point) {
  27. pCopy = &Point{
  28. X: p.X,
  29. Y: p.Y,
  30. }
  31. return
  32. }
  33. func NewSnake(direction Direction, field Field) (s *Snake) {
  34. fieldCenterX := field.Width / 2
  35. fieldCenterY := field.Height / 2
  36. switch direction {
  37. case Direction_Left:
  38. s = &Snake{
  39. Points: []*Point{
  40. &Point{X: fieldCenterX - 1, Y: fieldCenterY},
  41. &Point{X: fieldCenterX, Y: fieldCenterY},
  42. &Point{X: fieldCenterX + 1, Y: fieldCenterY},
  43. },
  44. }
  45. case Direction_Right:
  46. s = &Snake{
  47. Points: []*Point{
  48. &Point{X: fieldCenterX + 1, Y: fieldCenterY},
  49. &Point{X: fieldCenterX, Y: fieldCenterY},
  50. &Point{X: fieldCenterX - 1, Y: fieldCenterY},
  51. },
  52. }
  53. case Direction_Down:
  54. s = &Snake{
  55. Points: []*Point{
  56. &Point{X: fieldCenterX, Y: fieldCenterY - 1},
  57. &Point{X: fieldCenterX, Y: fieldCenterY},
  58. &Point{X: fieldCenterX, Y: fieldCenterY + 1},
  59. },
  60. }
  61. default:
  62. s = &Snake{
  63. Points: []*Point{
  64. &Point{X: fieldCenterX, Y: fieldCenterY + 1},
  65. &Point{X: fieldCenterX, Y: fieldCenterY},
  66. &Point{X: fieldCenterX, Y: fieldCenterY - 1},
  67. },
  68. }
  69. }
  70. return
  71. }
  72. func (s *Snake) NewHead(direction Direction) (newHead *Point) {
  73. newHead = s.Points[0].Copy()
  74. switch direction {
  75. case Direction_Up:
  76. newHead.Y -= 1
  77. case Direction_Down:
  78. newHead.Y += 1
  79. case Direction_Right:
  80. newHead.X += 1
  81. case Direction_Left:
  82. newHead.X -= 1
  83. }
  84. return
  85. }
  86. func (s *Snake) Move(newHead *Point) {
  87. s.Points = s.Points[:len(s.Points)-1]
  88. s.Points = append([]*Point{newHead}, s.Points...)
  89. }
  90. func (s *Snake) Feed(food *Point) {
  91. s.Points = append([]*Point{food}, s.Points...)
  92. }
  93. func (s *Snake) selfCollision(head *Point, direction Direction) bool {
  94. selfCollisionIndex := -1
  95. for index, point := range s.Points[:len(s.Points)-1] {
  96. if point.X == head.X && point.Y == head.Y {
  97. selfCollisionIndex = index
  98. break
  99. }
  100. }
  101. if selfCollisionIndex == 1 {
  102. switch direction {
  103. case Direction_Up:
  104. head.Y += 2
  105. case Direction_Down:
  106. head.Y -= 2
  107. case Direction_Left:
  108. head.X += 2
  109. default:
  110. head.X -= 2
  111. }
  112. return false
  113. }
  114. return selfCollisionIndex >= 0
  115. }
  116. func wallCollision(head *Point, field Field) bool {
  117. return head.X >= field.Width || head.Y >= field.Height || head.X < 0 || head.Y < 0
  118. }
  119. func foodCollision(head *Point, food *Point) bool {
  120. return head.X == food.X && head.Y == food.Y
  121. }