Design Sanke Game

 1 public class SnakeGame {
 2     private Deque<Integer> snake;
 3     private int[][] food;
 4     private int width;
 5     private int height;
 6     private boolean isGameOver;
 7     private int index;
 8
 9     /** Initialize your data structure here.
10         @param width - screen width
11         @param height - screen height
12         @param food - A list of food positions
13         E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
14     public SnakeGame(int width, int height, int[][] food) {
15         this.food = food;
16         this.width = width;
17         this.height = height;
18         snake = new LinkedList<>();
19         snake.offer(0);
20         isGameOver = false;
21         index = 0;
22     }
23
24     /** Moves the snake.
25         @param direction - ‘U‘ = Up, ‘L‘ = Left, ‘R‘ = Right, ‘D‘ = Down
26         @return The game‘s score after the move. Return -1 if game over.
27         Game over when snake crosses the screen boundary or bites its body. */
28     public int move(String direction) {
29         if (isGameOver) {
30             return -1;
31         }
32         int head = snake.getFirst();
33         int tail = snake.pollLast();
34         int x = head / width;
35         int y = head % width;
36
37         switch (direction) {
38             case "U":
39                 x--;
40                 break;
41             case "D":
42                 x++;
43                 break;
44             case "L":
45                 y--;
46                 break;
47             case "R":
48                 y++;
49                 break;
50         }
51
52         if (x < 0 || x >= height || y < 0 || y >= width || snake.contains(x*width + y)) {
53             isGameOver = true;
54             return -1;
55         }
56         snake.addFirst(x*width + y);
57         if (index < food.length && food[index][0] == x && food[index][1] == y) {
58             index++;
59             snake.offer(tail);
60         }
61         return snake.size() - 1;
62     }
63 }
64
65 /**
66  * Your SnakeGame object will be instantiated and called as such:
67  * SnakeGame obj = new SnakeGame(width, height, food);
68  * int param_1 = obj.move(direction);
69  */

1. The score got is not original size of snake since starting score is 0 when snake size is 1.

2. Do not forget to add the head after check death.

时间: 2024-12-06 22:18:44

Design Sanke Game的相关文章

如何获取Expression Design 4工具与Expression Blend 4工具

在VS2010+C#+WPF 开发项目过程中涉及到界面的布局与设计,网上有人讲采用Expression Design 4与Expression Blend 4工具相当方便, 于是决定试看看,下面将这个过程与大家分享. 一.安装目的 尽管程序员可以使用VS编写XAML代码的方式来构造用户界面,但是对于有设计爱好的用户来说,使用类似Photoshop一样的Expression套件能将 软件美工最大化.设计过程是先使用了Expression Design来设计图形,然后将其导入到Expression

POJ2100 Graveyard Design(尺取法)

POJ2100 Graveyard Design 题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式: 循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6; 尺取时一定要注意循环终止条件的判断. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <

【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

译者地址:[翻]Android Design Support Library 的 代码实验--几行代码,让你的 APP 变得花俏 原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fancy with few lines of code 原文项目 demo: Lab-Android-DesignLibrary 双语对照地址: [翻-双语]Android D

DP什么意思 design pattern 设计模式

DP  design pattern 大话设计模式  中的DP 是设计模式的意思 设计模式的书 ,最经典最原始的就是 GOF 的<设计模式>了. 设计模式的书基本上大多是以这 20 多个模式分开讲.含<大话设计模式> 学了 OOL 写的程序基本上是 OB 的. 只有慢慢掌握了 DP 才能写出真正的 OO 程序. 思想 -> 设计原则 -> DP -> OOD

创建Material Design风格的Android应用--应用主题

昨天正式发布了android 5,同时android developer网站也更新了,增加了创建Material Design风格的Android应用指南,也更新了Support Library,在support library增加了一些Material Design风格的控件和动画等,这里给大家简单介绍一下怎样开发material design风格的Android应用. android 5使用Material Design风格 android提供了三种Material Design风格Them

Android Design 1: Back键和Up键在App导航中的表现

一,概念 1, Back键一直存在android系统中 1-1 任何页面下的返回 1-2 Floating window 1-3 Contexual Action bar/highlight select 1-4 Keyboard 2, Up键是随Android Design出来的. 2-1 android Design 定义的parent container 2-2 app的主界面是不存在Up键的 二,情景分析 1, App内部 1-1 沿逐级深入路径 Back:按照activity在栈中的顺

高煥堂 Design Thinking繁體版Blog區

高煥堂 Design ThinkingADT的繁體版Blog區 高煥堂 Design Thinking繁體版Blog區,布布扣,bubuko.com

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

poj 2100 Graveyard Design

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 7357   Accepted: 1816 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m