353. Design Snake Game

Source: Internet
Author: User

Design a Snake game that's played on a device with the screen size = width x height. Play The game online if you is not a familiar with the game.

The snake is initially positioned at the top left corner (0,0) with length = 1 unit.

You is 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 in the screen. For example, the second food would not be appear until the first food is eaten by the snake.

When a food does appear in the screen, it's guaranteed that it'll 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 (.) | 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, 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)

classposition{intx; inty;  PublicPosition (intXinty) {          This. x =x;  This. y =y; }      Public Booleanisequal (Position p) {return(p.x = = This. x && P.y = = This. Y); }    } Public classSnakegame {/**Initialize your data structure here. @paramWidth-screen Width@paramHeight-screen Height@paramfood-a List of food positions e.g food = [[[[]], [1,0]] means the first food was positioned at [+], the SE Cond is at [1,0]. */    intwidth; intheight; int[] food; intLen; LinkedList<Position>Snake; intscore;  PublicSnakegame (intWidthintHeightint[] food) {          This. width =width;  This. Height =height;  This. Food =Food ; Snake=NewLinkedlist<position>(); Snake.add (NewPosition (0,0)); Len= 0; }        /**Moves the snake. @paramDirection-' U ' = up, ' L ' = left, ' R ' = right, ' D ' = down@returnThe game ' s score after the move.         Return-1 if game over. Game over if snake crosses the screen boundary or bites its body. */     Public intMove (String direction) {Position cur=NewPosition (Snake.get (0). x, Snake.get (0). Y);//Current position is the head of snack;        if(Direction.equals ("R")) cur.y++; if(Direction.equals ("L")) cur.y--; if(Direction.equals ("U")) cur.x--; if(Direction.equals ("D")) cur.x++; if(!isValid (cur))return-1; //Case 1 Eat food        if(Len < food.length && Cur.isequal (NewPosition (Food[len][0], food[len][1])) {Snake.addfirst (cur); Len++; }        Else{//just add head and remove the tailSnake.addfirst (cur);        Snake.removelast (); }        returnLen; }     Public BooleanisValid (Position cur) {//System.out.print ("x" + cur.x + "Y" + cur.y + "height" + height + "width" + width);        if(Cur.x < 0 | | cur.x >= Height | | cur.y < 0 | | cur.y >= width) {//The snack meet the boundry;            return false; }                     for(inti = 1; I < Snake.size ()-1; i++){              //the snack bit itself;!!!! do not take into account head and tail new head is legal to being in old tail ' s position, remove from set temporarily Position next =Snake.get (i); if(next.isequal (cur)) {return false; }        }        return true; }}/*** Your Snakegame object is instantiated and called as such: * snakegame obj = new Snakegame (width, height, food) ; * int param_1 = obj.move (direction); */

353. Design Snake Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.