admin管理员组文章数量:1404340
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 promise between
- optimally efficient in number of operations and memory usage
- readable
- maintainable
- clever (optional)
- elegant
- pact
????
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 promise between
- optimally efficient in number of operations and memory usage
- readable
- maintainable
- clever (optional)
- elegant
- pact
????
Share Improve this question asked Jul 28, 2015 at 5:57 user5124106user5124106 4032 gold badges4 silver badges11 bronze badges 4-
3
links.unshift(links.pop());
– dandavis Commented Jul 28, 2015 at 6:03 - Is your list of requirements in order of importance? – user663031 Commented Jul 28, 2015 at 6:10
- 3 Clever is never a requirement of mine. "Optimally efficient" is an ill-defined target. What is generally needed is the least amount of effort to achieve a solution that meets the requirements. This leaves more time to work on other areas of the problem that matter. Since you've not defined what would be sufficient, we can't code to a particular goal. Plus, maximum efficiency often requires adding plication which conflicts with many of your other goals so one must know how to make a tradeoff between them. – jfriend00 Commented Jul 28, 2015 at 6:18
- possible duplicate of JavaScript Array rotate() – user663031 Commented Jul 28, 2015 at 6:41
3 Answers
Reset to default 9optimally efficient in number of operations and memory usage
You're asking for two optimisations that usually counter one another. e.g. more speed == more memory.
That said, I'd probably choose a (doubly) linked list to store my snake, because removal or addition at the front or tail are very cheap, and with games, faster is way preferable to less memory (within reason, but I wonder how long your snake would have to be before you run into memory issues... well beyond what's playable... and some).
Of course, I assume you've measured and found the standard array based methods to be too slow (seems unlikely).
You can rotate an array in two ways :
links.unshift(links.pop()); or
links.push(links.shift());
first method solves your issue.
For any version of Javascript from ES3 forward:
links.unshift(links.pop());
本文标签:
版权声明:本文标题:In Javascript, what is the most compact, elegant and efficent way to bring the last element of an array to the beginning? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742241355a2438854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论