|
|
@@ -55,29 +55,21 @@ func drawSnake(screen tcell.Screen, style tcell.Style, snake *Snake) {
|
|
|
|
|
|
func snakeDirection(press int, snake *Snake) {
|
|
|
if press == Left {
|
|
|
- if snake.direction != Right {
|
|
|
- snake.head.x--
|
|
|
- snake.tail = append(snake.tail, Position{x: snake.head.x + 1, y: snake.head.y})
|
|
|
- snake.direction = press
|
|
|
- }
|
|
|
+ snake.head.x--
|
|
|
+ snake.tail = append(snake.tail, Position{x: snake.head.x + 1, y: snake.head.y})
|
|
|
+ snake.direction = press
|
|
|
} else if press == Right {
|
|
|
- if snake.direction != Left {
|
|
|
- snake.head.x++
|
|
|
- snake.tail = append(snake.tail, Position{x: snake.head.x - 1, y: snake.head.y})
|
|
|
- snake.direction = press
|
|
|
- }
|
|
|
+ snake.head.x++
|
|
|
+ snake.tail = append(snake.tail, Position{x: snake.head.x - 1, y: snake.head.y})
|
|
|
+ snake.direction = press
|
|
|
} else if press == Up {
|
|
|
- if snake.direction != Down {
|
|
|
- snake.head.y--
|
|
|
- snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y + 1})
|
|
|
- snake.direction = press
|
|
|
- }
|
|
|
+ snake.head.y--
|
|
|
+ snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y + 1})
|
|
|
+ snake.direction = press
|
|
|
} else if press == Down {
|
|
|
- if snake.direction != Up {
|
|
|
- snake.head.y++
|
|
|
- snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y - 1})
|
|
|
- snake.direction = press
|
|
|
- }
|
|
|
+ snake.head.y++
|
|
|
+ snake.tail = append(snake.tail, Position{x: snake.head.x, y: snake.head.y - 1})
|
|
|
+ snake.direction = press
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -101,3 +93,19 @@ func ReverseSlice[T comparable](s []T) []T {
|
|
|
}
|
|
|
return r
|
|
|
}
|
|
|
+
|
|
|
+func validateDirection(snake *Snake, direction int) bool {
|
|
|
+ if snake.direction == direction {
|
|
|
+ return true
|
|
|
+ } else if snake.direction == Left && direction == Right {
|
|
|
+ return false
|
|
|
+ } else if snake.direction == Right && direction == Left {
|
|
|
+ return false
|
|
|
+ } else if snake.direction == Up && direction == Down {
|
|
|
+ return false
|
|
|
+ } else if snake.direction == Down && direction == Up {
|
|
|
+ return false
|
|
|
+ } else {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+}
|