The idea of realizing snake game:
Class Node (I,J)//representation coordinates
Class Worm (list<node> nodes)//Number of lines: 10///use LinkedList to store the snake's coordinate points. The current in is to add a coordinate point using the AddFirst (Node node) method (which can be obtained by GetFirst () with a previous coordinate of 1), and then delete the tail node. If food is encountered, the tail node is not deleted.
//Number of columns: 32 columns
Step ()//Take a step forward
Step (int dir)//Take a step forward according to the direction
Class Wormpanel//Artboards
---hashset store food//when randomly obtaining the position of a food coordinate point, determine if its coordinate point repeats with the coordinate point of the snake (if repeated, continue to get the random number, knowing that the position is not repeated).
Internal class--worm (LinkedList)
I J
J UP-1 0
01234567890123456789012345678901 down 1 0
I 0--------------------------------left 0-1
1| O | Right 0 1
2| ^ | 2,9
3| <-#-> |3,8 3,9 3,10
4| # | 4,9
5| ### | 5,9 5,10 5,11
6| # o O | 6,11
7| # | 7,11
8| | Nodes.remove (Nodes.size ()-1)
9--------------------------------
Import Java.util.scanner;class Wormdemo {public static void main (string[] args) {Scanner sc = new Scanner (system.in); Wormpanel Wormpanel = new Wormpanel (); Wormpanel.worm Worm =wormpanel.getworm (); while (true) {wormpanel.print (); String dir = Sc.nextline (),//u, D, L, R for the upper and lower left direction if ("U". Equalsignorecase (dir)) Worm.step (WormPanel.Worm.UP); else if ("D". Equalsignorecase (dir)) Worm.step (WormPanel.Worm.DOWN); else if ("L". Equalsignorecase (dir)) Worm.step ( WormPanel.Worm.LEFT); else if ("R". Equalsignorecase (dir)) Worm.step (WormPanel.Worm.RIGHT); else if ("Q". Equalsignorecase (dir)) { System.out.println ("Bye ^_^"); break;} Elseworm.step ();}}
<span style= "font-family:arial, Helvetica, Sans-serif;" >}</span>
<pre name= "code" class= "java" >package snakegame;import java.util.*;class wormpanel {//Snake private Worm worm;// Number of rows private int rows = 10;//columns private int cols = 32;//food private hashset<node> foods = new hashset<node> ();p ublic Wormpanel () {worm = new worm (); Initfoods (6);} Public Worm Getworm () {return this.worm;} print function public void print () {for (int. i=0;i<rows;i++) {for (int j=0;j<cols;j++) {if (i==0 | | i==rows-1) System.out.print ("-"); else if (j==0 | | j==cols-1) System.out.print ("|"); else if (Worm.contains (i,j)) System.out.print ("#"), Else if (Foods.contains (new Node (I,J))) System.out.print ("0"); else System.out.print ("");} System.out.println ();}} Randomly generated food public void initfoods (int num) {Random random = new random (), while (true) {int i = Random.nextint (rows-2) +1;int j = r Andom.nextint (cols-2) +1;//first determine if the coordinates that make up the snake are (worm.contains (i,j)) {continue;} Foods.add (New Node (I,J));//Do not add duplicate coordinates if (foods.size () ==num) break;}} public class Worm{public static final int up = -10;public static final int down = 10;public static final int left = -1;public static final int right = 1;private Linkedlist<node > nodes = new linkedlist<node> ();p rivate int direction;public Worm () {Nodes.Add (new Node (3,9)); Nodes.Add (New Node (4,9)); Nodes.Add (New Node (5,9)); Nodes.Add (New Node (5,10)); Nodes.Add (New Node (5,11)); Nodes.Add (New Node (6,11)); Nodes.Add (New Node (7,11)); This.direction = right;} public void Step () {//get the coordinates corresponding to the first # number Node head = Nodes.getfirst (); Computes the coordinates of the new node int i = Head.geti () +DIRECTION/10; Int J = HEAD.GETJ () +direction%10; Head = new Node (I,J); Add a Node Nodes.addfirst (head) to the head of the snake; Determine if you encounter a food if (Foods.remove (head)) {return; }//delete tail node nodes.removelast ();} public void Step (int direction) {if (this.direction+direction==0) throw new RuntimeException ("Do not allow U-turn"); This.direction = direction; Step ();} A method that determines whether a coordinate is included public boolean contains (int I,int j) {return Nodes.contaiNS (new Node (I,J));}}}
Package Snakegame;class Node {private int i;private int j;public node () {}public node (int i,int j) {this.i = I;THIS.J = j;} public void SetI (int i) {this.i = i;} public void Setj (int j) {this.j = j;} public int Geti () {return this.i;} public int Getj () {return THIS.J;} Public String toString () {return i+ "," +J;} public int hashcode () {return (i<<16) |j;} public boolean equals (Object obj) {if (obj==null) return false;if (this==obj) return true;if (obj instanceof Node) {node node = (Node) obj;return this.i==node.i && THIS.J==NODE.J;} return false;}}
Comments:
public void Step () { //get the coordinates corresponding to the first # number Node head = Nodes.getfirst (); Computes the coordinates of the new node int i = Head.geti () +DIRECTION/10; Int J = HEAD.GETJ () +direction%10; Head = new Node (i,j); Add a node Nodes.addfirst (head) to the head of the snake; Determine if you encounter a food if (Foods.remove (head)) { return; } Delete Tail node nodes.removelast ();}
Randomly generated food public void initfoods (int num) {Random random = new random (), while (true) {int i = Random.nextint (rows-2) +1;int j = r Andom.nextint (cols-2) +1;//first determine if the coordinates that make up the snake are (worm.contains (i,j)) {continue;} Foods.add (New Node (I,J));//Do not add duplicate coordinates if (foods.size () ==num) break ;}}
The Foods.remove () method in the above two pieces of code requires overriding the Hashcode () and Equals () methods. The comparison is their coordinate values. Remove them in the same coordinates.
Snake game under the Java console