Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game.
The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
You are given a list of food’s positions in row-column order. When a snake eats the food, its length and the game’s score both increase by 1.
Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
Example:
解法1: Deque + HashMap
设计题在于选用合适的数据结构来完成要求的feature。
用一个hashmap存每一个snake的位置是便于查询是否头咬到了身体
用deque来表示一个snake,方便从头从尾巴更新。
每次移动的时候先要把tail移除,然后判断是否溢出,是否咬到了身体。
然后再判断是否吃到了果实,如果是那么再把tail给放回去。
|
|