I'm programming a Snake game
and the logic of the snake's movement dictates that if I have a Javascript array
var links = [elem_0, elem_1, ..., elem_n];
of elements representing links of the snake, then the way for the snake to move is to pop out elem_n
, change its position to be that of the elem_0
plus the translation units dx
and dy
, and then put it at the beginning of the array:
[elem_0, elem_1, ..., elem_n]
---> [elem_n, elem_0, ..., elem_(n-1)]
(with some internal properties of elem_n
changed in the process)
What is the way to do this that makes no compromise between
- optimally efficient in number of operations and memory usage
- readable
- maintainable
- clever (optional)
- elegant
- compact
????