I've been tasked with creating a Snake game
https://i.sstatic.net/KXwNK.png
and the logic for moving the snake is to rearrange elements in a JavaScript array.
var links = [elem_0, elem_1, ..., elem_n];
To move the snake, I need to pop out elem_n
, update its position using dx
and dy
, then put it at the beginning of the array:
[elem_0, elem_1, ..., elem_n]
--->
[elem_n, elem_0, ..., elem_(n-1)]
(while adjusting internal properties of elem_n
)
What's the most efficient way to achieve this while ensuring readability, maintainability, cleverness (if possible), elegance, and compactness?
- optimally efficient in number of operations and memory usage
- readable
- maintainable
- clever (optional)
- elegant
- compact
????