[Leetcode] Design Snake Game Designs Snake Games

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)

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

Feel recently leetcode often out some design class of topic Ah, is the problem of the algorithm class is out, this problem let us design a snake game, this is a simplified version, but the game rules still remain unchanged, snakes can go up and down about four directions, eat food will grow 1, If you touch a wall or your body, the game will end. We need a one-dimensional array to save the position of the snake, as the snake moves the process of the snake head forward one step, the tail also followed, the middle of the body is still in the original position, so the result of the move is that the snake head into a new position, remove the position of the tail of the snake. It is important to note that the position of the tail of the snake is removed before or after the collision of the snake body, and if it does not pass the test case after that:[[3,3,[[2,0],[0,0]]],[] D "],[" D "],[" U "], If there is no problem before, overall is not a problem, see the code is as follows:

classSnakegame { Public:    /** Initialize your data structure here.  @param width-screen width @param height-screen height @param food-a List of food positions e.g food = [[[[]], [1,0]] means the first food was positioned at [a], the second is at [1,0]. */Snakegame (intWidthintHeight, vector<pair<int,int>>Food ) {         This->width =width;  This->height =height;  This->food =Food ; Score=0; Pos.push_back ({0,0}); }        /** 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 if snake crosses the screen boundary or bites its body. */    intMovestringdirection) {Auto Head= Pos.front (), tail =Pos.back ();        Pos.pop_back (); if(Direction = ="U") --Head.first; Else if(Direction = ="L") --Head.second; Else if(Direction = ="R") ++Head.second; Else if(Direction = ="D") ++Head.first; if(Count (Pos.begin (), Pos.end (), head) | | Head.first <0|| Head.first >= Height | | Head.second <0|| Head.second >=width) {            return-1;        } pos.insert (Pos.begin (), head); if(!food.empty () && head = =Food.front ())            {Food.erase (Food.begin ());            Pos.push_back (tail); ++score; }        returnscore; }Private:    intwidth, height, score; Vector<pair<int,int>>Food , POS;}; 

Resources:

Https://leetcode.com/discuss/106235/c-solution-seems-test-case-didnt-consider-food-on-body

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Design Snake Game Designs Snake Games

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.