Design Snake Game
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)
public class SnakeGame {
Map<String, int[]> map;
int idx = ;
int[][] food;
LinkedList<int[]> snake;
int width;
int height;
Set<String> position = new HashSet<>();
public SnakeGame(int width, int height, int[][] food) {
this.food = food;
this.width = width;
this.height = height;
map = createMap();
snake = new LinkedList<>();
snake.add(new int[] { , });
position.add( + "_" + );
}
/**
* Moves the snake.
*
* @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
* @return The game's score after the move. Return -1 if game over. Game over
* when snake crosses the screen boundary or bites its body.
*/
public int move(String direction) {
int[] dir = map.get(direction);
int[] head = snake.get();
int[] tail = snake.get(snake.size() - );
position.remove(tail[] + "_" + tail[]);
int nextR = head[] + dir[];
int nextC = head[] + dir[];
String posiKey = nextR + "_" + nextC;
if (nextR < || nextC < || nextR >= height || nextC >= width || position.contains(posiKey)) {
return -;
}
if (idx < food.length && nextR == food[idx][] && nextC == food[idx][]) {
idx++;
} else {
snake.removeLast();
}
snake.addFirst(new int[] { nextR, nextC });
position.add(posiKey);
return snake.size() - ;
}
private Map<String, int[]> createMap() {
Map<String, int[]> map = new HashMap<>();
map.put("U", new int[] { -, });
map.put("L", new int[] { , - });
map.put("R", new int[] { , });
map.put("D", new int[] { , });
return map;
}
}
Design Snake Game的更多相关文章
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- Leetcode: Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [Swift]LeetCode353. 设计贪吃蛇游戏 $ Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- 353. Design Snake Game
贪食蛇. GAME OVER有2种情况,1是咬到自己,2是出界. 1)用QUEUE来保留占据的格子,每走一格就添加1个,然后POll()最后一个. 做一个一样的SET来check要走的格子是不是已经在 ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
随机推荐
- window上git bash运行错误记录
错误现象:每次启动git bash报出如下错误gitbash 0 [main] bash 11928 fork: child -1 - CreateProcessW failed for 'D:\P ...
- Codeforces 576D Flights for Regular Customers (图论、矩阵乘法、Bitset)
题目链接 http://codeforces.com/contest/576/problem/D 题解 把边按\(t_i\)从小到大排序后枚举\(i\), 求出按前\((i-1)\)条边走\(t_i\ ...
- [CSP-S模拟测试]:C(倍增+数学)
题目传送门(内部题152) 输入格式 第一行两个整数$N,Q$. 接下来一行$N$个整数,第$i$个为$a_i$. 接下来的$N-1$行,每行两个整数$u,v$.表示$u,v$之间有一条边. 接下来的 ...
- svg简单的应用
1.可以直接在html内写svg (1)width宽度,height高度 (2)xmlns svg的规则 <svg xmlns="http://www.w3.org/2000/svg& ...
- ubuntu 18.04 64bit下如何启动向日葵远程控制端软件?
一. 背景 从向日葵官网下载了linux版向日葵远程控制端软件,解压后直接执行Sunlloginremote发现以下错误: jello@jello:~/sunlogin_remote_linux$ . ...
- JDK1.8为什么废弃永久代【一篇就够】[z]
https://blog.csdn.net/sjmz30071360/article/details/89456177 (Metaspace) 1.背景 2.为什么废弃永久代(PermGen) 3.深 ...
- hk clearing participant & Exchange Participant of SEHK
https://www.hkex.com.hk/-/media/HKEX-Market/Services/Clearing/Securities/Infrastructure/CCASS-3-Term ...
- kafka default partitioner java版本和scala版本的不同
scala import kafka.utils._ class DefaultPartitioner(props: VerifiableProperties = null) extends Part ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响
笔记 4.SpringCloud微服务核心组件Eureka介绍和闭源后影响 简介: SpringCloud体系介绍 官方地址:http://projec ...
- UITableView+FDTemplateLayoutCell计算行高显示<二>
之前记录过一篇UITableView+FDTemplateLayoutCell计算行高不成功的博客... 传送门:http://www.cnblogs.com/pengsi/p/6571311.htm ...