snake.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package snakesimulator
  2. func (p Point) Copy() (pCopy *Point) {
  3. pCopy = &Point{
  4. X: p.X,
  5. Y: p.Y,
  6. }
  7. return
  8. }
  9. func NewSnake(direction Direction, field Field) (s *Snake) {
  10. fieldCenterX := field.Width / 2
  11. fieldCenterY := field.Height / 2
  12. switch direction {
  13. case Direction_Left:
  14. s = &Snake{
  15. Points: []*Point{
  16. &Point{X: fieldCenterX - 1, Y: fieldCenterY},
  17. &Point{X: fieldCenterX, Y: fieldCenterY},
  18. &Point{X: fieldCenterX + 1, Y: fieldCenterY},
  19. },
  20. }
  21. case Direction_Right:
  22. s = &Snake{
  23. Points: []*Point{
  24. &Point{X: fieldCenterX + 1, Y: fieldCenterY},
  25. &Point{X: fieldCenterX, Y: fieldCenterY},
  26. &Point{X: fieldCenterX - 1, Y: fieldCenterY},
  27. },
  28. }
  29. case Direction_Down:
  30. s = &Snake{
  31. Points: []*Point{
  32. &Point{X: fieldCenterX, Y: fieldCenterY - 1},
  33. &Point{X: fieldCenterX, Y: fieldCenterY},
  34. &Point{X: fieldCenterX, Y: fieldCenterY + 1},
  35. },
  36. }
  37. default:
  38. s = &Snake{
  39. Points: []*Point{
  40. &Point{X: fieldCenterX, Y: fieldCenterY + 1},
  41. &Point{X: fieldCenterX, Y: fieldCenterY},
  42. &Point{X: fieldCenterX, Y: fieldCenterY - 1},
  43. },
  44. }
  45. }
  46. return
  47. }
  48. func (s *Snake) NewHead(direction Direction) (newHead *Point) {
  49. newHead = s.Points[0].Copy()
  50. switch direction {
  51. case Direction_Up:
  52. newHead.Y -= 1
  53. case Direction_Down:
  54. newHead.Y += 1
  55. case Direction_Right:
  56. newHead.X += 1
  57. case Direction_Left:
  58. newHead.X -= 1
  59. }
  60. return
  61. }
  62. func (s *Snake) Move(newHead *Point) {
  63. s.Points = s.Points[:len(s.Points)-1]
  64. s.Points = append([]*Point{newHead}, s.Points...)
  65. }
  66. func (s *Snake) Feed(food *Point) {
  67. s.Points = append([]*Point{food}, s.Points...)
  68. }
  69. func (s *Snake) selfCollision(head *Point, direction Direction) bool {
  70. selfCollisionIndex := -1
  71. for index, point := range s.Points[:len(s.Points)-1] {
  72. if point.X == head.X && point.Y == head.Y {
  73. selfCollisionIndex = index
  74. break
  75. }
  76. }
  77. if selfCollisionIndex == 1 {
  78. switch direction {
  79. case Direction_Up:
  80. head.Y += 2
  81. case Direction_Down:
  82. head.Y -= 2
  83. case Direction_Left:
  84. head.X += 2
  85. default:
  86. head.X -= 2
  87. }
  88. return false
  89. }
  90. return selfCollisionIndex >= 0
  91. }
  92. func wallCollision(head *Point, field Field) bool {
  93. return head.X >= field.Width || head.Y >= field.Height || head.X < 0 || head.Y < 0
  94. }
  95. func foodCollision(head *Point, food *Point) bool {
  96. return head.X == food.X && head.Y == food.Y
  97. }