1. 程式人生 > >[Swift]LeetCode353. 設計貪吃蛇遊戲 $ Design Snake Game

[Swift]LeetCode353. 設計貪吃蛇遊戲 $ Design Snake Game

play scree column appear ros elf 屏幕 turn color

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:

Given width = 3, height = 2, and food = [[1,2],[0,1]].

Snake snake = new Snake(width, height, food);

Initially the snake appears at position (0,0) and the food at (1,2).

|S| | |
| | |F|

snake.move("R"); -> Returns 0

| |S| |
| | |F|

snake.move("D"); -> Returns 0

| | | |
| |S|F|

snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )

| |F| |
| |S|S|

snake.move("U"); -> Returns 1

| |F|S|
| | |S|

snake.move("L"); -> Returns 2 (Snake eats the second food)

| |S|S|
| | |S|

snake.move("U"); -> Returns -1 (Game over because snake collides with border)

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.


設計一個在屏幕大小=寬x高的設備上玩的蛇遊戲。如果您不熟悉遊戲,請在線玩遊戲。

蛇最初位於左上角(0,0),長度=1單位。

你會得到一份食物位置列表,按行、列順序排列。當蛇吃了食物,它的長度和遊戲的得分都增加了1。

每種食物一個一個地出現在屏幕上。例如,第二種食物在第一種食物被蛇吃之前不會出現。

當一種食物出現在屏幕上時,它保證不會出現在被蛇占據的街區上。

例子:

給定width = 3, height = 2, and food = [[1,2],[0,1]].

Snake snake = new Snake(width, height, food);

最初snake出現在位置(0,0),food出現在位置(1,2)。

|S| | |
| | |F|

snake.move("R"); -> Returns 0

| |S| |
| | |F|

snake.move("D"); -> Returns 0

| | | |
| |S|F|

snake.move("R"); -> Returns 1 (snake吃第一種食物,之後第二種食物出現在(0,1))。

| |F| |
| |S|S|

snake.move("U"); -> Returns 1

| |F|S|
| | |S|

snake.move("L"); -> Returns 2 (snake吃第二種食物)
| |S|S|
| | |S|

snake.move("U"); -> Returns -1 (遊戲結束,因為snake與邊界碰撞)

Credits:
特別感謝@elmirap添加此問題並創建所有測試用例。


Solution:

 1 class Snake {
 2     /** Initialize your data structure here.
 3         @param width - screen width
 4         @param height - screen height 
 5         @param food - A list of food positions
 6         E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
 7     
 8     var width:Int
 9     var height:Int
10     var score:Int
11     var food:[[Int]]
12     var snake:[[Int]]
13     
14     init(_ width:Int,_ height:Int,_ food:[[Int]])
15     {
16         self.width = width
17         self.height = height
18         self.food = food
19         self.score = 0
20         self.snake = [[0,0]]
21     }
22     
23         /** Moves the snake.
24         @param direction - ‘U‘ = Up, ‘L‘ = Left, ‘R‘ = Right, ‘D‘ = Down 
25         @return The game‘s score after the move. Return -1 if game over. 
26         Game over when snake crosses the screen boundary or bites its body. */
27     
28     func move(_ direction:String) -> Int
29     {
30         var head:[Int] = snake.first!
31         var tail:[Int] = snake.last!
32         snake.removeLast()
33         if direction == "U" { head[0] -= 1}
34         else if direction == "L" { head[1] -= 1}
35         else if direction == "R" { head[1] += 1}
36         else if direction == "D" { head[0] += 1}
37         if snake.contains(head) || head[0] < 0 || head[0] >= height || head[1] < 0 || head[1] >= width
38         {
39             return -1
40         }
41         snake.insert(head,at:0)
42         if !food.isEmpty && head == food.first!
43         {
44             food.removeFirst()
45             snake.append(tail)
46             score += 1
47         }
48         return score
49     }    
50 }

點擊:Playground測試

 1 let width:Int = 3
 2 let height:Int = 2
 3 let food:[[Int]] = [[1,2],[0,1]]
 4 var snake = Snake(width, height, food)
 5 
 6 //snake.move("R"); -> Returns 0
 7 print(snake.move("R"))
 8 //Print 0
 9 
10 //snake.move("D"); -> Returns 0
11 print(snake.move("D"))
12 //Print 0
13 
14 //snake.move("R"); -> Returns 1
15 print(snake.move("R"))
16 //Print 1
17 
18 //snake.move("U"); -> Returns 1
19 print(snake.move("U"))
20 //Print 1
21 
22 //snake.move("L"); -> Returns 2 
23 print(snake.move("L"))
24 //Print 2
25 
26 //snake.move("U"); -> Returns -1
27 print(snake.move("U"))
28 //Print -1

[Swift]LeetCode353. 設計貪吃蛇遊戲 $ Design Snake Game