Design a Snake game that's played on a device with the screen size = width x height. Play the game onlineifYou is not a familiar with the game. The snake is initially positioned at the top left corner (0,0) with length = 1Unit. You is given a list of food' s positions in Row-column order. When a snake eats the food, it 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=NewSnake (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)
HashSet + Queue:
Keep your tail when you eat, and delete your tail when you don't eat.
When one case to notice snake turns, remove the tail first and add the new point in. If this is not the case, the result of not hitting the tail would have been judged to have hit
1 Public classSnakegame {2 intm;3 intN;4 int[] foods;5Queue<integer>queue;6Hashset<integer>set;7 int[] headpos;8 intFoodseq;9 Ten One /**Initialize your data structure here. A @paramWidth-screen Width - @paramHeight-screen Height - @paramfood-a list of food positions the e.g food = [[[]], [1,0]] means the first food was positioned at [a], the second is at [1,0].*/ - PublicSnakegame (intWidthintHeightint[] food) { - This. m =height; - This. N =width; + This. Foods =Food ; - This. Queue =NewLinkedlist<integer>(); + This. Set =NewHashset<integer>(); A This. Headpos =New int[]{0, 0}; at This. Foodseq = 0; -Queue.offer (0); -Set.add (0); - } - - /**Moves the snake. in @paramDirection-' U ' = up, ' L ' = left, ' R ' = right, ' D ' = down - @returnThe game ' s score after the move. Return-1 if game over. to Game over if snake crosses the screen boundary or bites its body.*/ + Public intMove (String direction) { - int[] dir; the Switch(direction) { * Case"U": dir =New int[]{-1, 0}; Break; $ Case"L": dir =New int[]{0,-1}; Break;Panax Notoginseng Case"R": dir =New int[]{0, 1}; Break; - Case"D": dir =New int[]{1, 0}; Break; the default: dir =New int[2]; + } A introw = Headpos[0] + dir[0];//new position Row the intCol = headpos[1] + dir[1];//New position Col + - //Delete Tail first, before push into a new cell $ if(Foodseq<foods.length && Calcposid (foods[foodseq][0], foods[foodseq][1]) = =calcposid (Row, col)) { $foodseq++; - } - Else { the Set.remove (Queue.poll ()); - }Wuyi the if(row<0 | | row>=m | | col<0 | | col>=n)return-1;//Hit border - if(Set.contains (Calcposid (Row, col)))return-1;//Hit its own body Wu - Set.add (Calcposid (Row, col)); About Queue.offer (Calcposid (Row, col)); $Headpos[0] =Row; -HEADPOS[1] =Col; - - returnSet.size ()-1; A } + the Public intCalcposid (intXinty) { - returnx*n+y; $ } the } the the /** the * Your Snakegame object would be instantiated and called as such: - * Snakegame obj = new Snakegame (width, height, food); in * int param_1 = obj.move (direction); the */
If there are no 39 lines, the program will say Dir is not initialize,
In fact, a better formulation should be
1 intRowhead = Body.peekfirst ()/width;2 intColhead = Body.peekfirst ()%width;3 Switch(direction) {4 Case"U": rowhead--;5 Break;6 Case"D": rowhead++;7 Break;8 Case"L": colhead--;9 Break;Ten default: colhead++; One}
Leetcode:design Snake Game