()
| 224 | } |
| 225 | |
| 226 | initializeSnake_() { |
| 227 | /** |
| 228 | * @private {Array<[number, number]>} Squares currently occupied by the |
| 229 | * snake. |
| 230 | * |
| 231 | * Each element is a length-2 array representing the [y, x] coordinates of |
| 232 | * the square. The array is ordered such that the first element is the |
| 233 | * head of the snake and the last one is the tail. |
| 234 | */ |
| 235 | this.snakeSquares_ = []; |
| 236 | |
| 237 | // Currently, the snake will start from a completely-straight and |
| 238 | // horizontally-posed state. |
| 239 | const y = getRandomInteger(0, this.height_); |
| 240 | let x = getRandomInteger(this.initLen_ - 1, this.width_); |
| 241 | for (let i = 0; i < this.initLen_; ++i) { |
| 242 | this.snakeSquares_.push([y, x - i]); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Current snake direction {'l' | 'u' | 'r' | 'd'}. |
| 247 | * |
| 248 | * Currently, the snake will start from a completely-straight and |
| 249 | * horizontally-posed state. The initial direction is always right. |
| 250 | */ |
| 251 | this.snakeDirection_ = 'r'; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Generate a number of new fruits at a random locations. |
no test coverage detected