12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package snakesimulator
- func (p Point) Copy() (pCopy *Point) {
- pCopy = &Point{
- X: p.X,
- Y: p.Y,
- }
- return
- }
- func (s *Snake) NewHead(direction Direction) (newHead *Point) {
- newHead = s.Points[0].Copy()
- switch direction {
- case Direction_Up:
- newHead.Y -= 1
- case Direction_Down:
- newHead.Y += 1
- case Direction_Right:
- newHead.X += 1
- case Direction_Left:
- newHead.X -= 1
- }
- return
- }
- func (s *Snake) Move(newHead *Point) {
- s.Points = s.Points[:len(s.Points)-1]
- s.Points = append([]*Point{newHead}, s.Points...)
- }
- func (s *Snake) Feed(food *Point) {
- s.Points = append([]*Point{food}, s.Points...)
- }
- func (s *Snake) SelfCollision(head *Point) int {
- for index, point := range s.Points[:len(s.Points)-1] {
- if point.X == head.X && point.Y == head.Y {
- return index
- }
- }
- return 0
- }
|