Using bidirectional linked list to realize greedy snake program
1. Linked list node definition:
Package snake;
public class Snakenode {
private int x;
private int y;
Private Snakenode Next;
Private snakenode ahead;
Public Snakenode () {
} public
snakenode (int x, int y) {
super ();
this.x = x;
This.y = y;
}
public int GetX () {return
x;
}
public void SetX (int x) {
this.x = x;
}
public int GetY () {return
y;
}
public void sety (int y) {
this.y = y;
}
Public Snakenode GetNext () {return
next;
}
public void Setnext (Snakenode next) {
this.next = next;
}
Public Snakenode Getahead () {return
ahead;
}
public void Setahead (Snakenode ahead) {
this.ahead = ahead;
}
}
Main program:
Package snake;
Import Java.awt.BorderLayout;
Import Java.awt.GridLayout;
Import Java.awt.KeyEventPostProcessor;
Import Java.awt.KeyboardFocusManager;
Import java.awt.event.KeyEvent;
Import Java.util.Random;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
/** * Created by Hackcoder on 15-3-11.
* * public class Snake extends JFrame {private static final int rows = 60;
private static final int columns = 80;
Direction private static final int up = 1;
private static final int right = 2;
private static final int down = 3;
private static final int left = 4;
private static int driection_now = right;
private static Boolean iseat = false;
private static int tailx;
private static int taily;
private static Snakenode Snakeheader = new Snakenode ();
private static Snakenode Snaketailer = Snakeheader;
private static Snakenode food = new Snakenode (); private static jlabel[] images = new Jlabel[rows * columns];
public static void Main (String args[]) {snakeheader.setx (New Random (). Nextint (rows-1));
Snakeheader.sety (New Random (). Nextint (columns-1));
Snake Snake = new Snake ();
Food = Getfood ();
while (true) {try {next ();
Eat the Food if (food.getx () = = Snakeheader.getx () && food.gety () = = Snakeheader.gety ()) {addtail ();
Iseat = true;
//Eat food, regenerate a food if (iseat) {food = Getfood (); //Decide whether to end the game if (Judgeend ()) {Joptionpane.showmessagedialog (null, "Game Over!") "The game is over!"
", joptionpane.error_message);
Break
} snakenode pnow = Snakeheader; while (Pnow!= null) {Images[columns * PNOW.GETX () + pnow.gety ()]. SetIcon (New ImageIcon ("Image/black.jpg", "
"));
Pnow = Pnow.getnext ();
} Images[columns * FOOD.GETX () + food.gety ()]. SetIcon (New ImageIcon ("Image/black.jpg", ""));
Thread.Sleep (100);
Clean Pnow = Snakeheader; while (Pnow!= null) {Images[columns * PNOW.GETX () + pnow.gety ()]. SetIcon (New ImageIcon ("Image/white.jpg", ""));
Pnow = Pnow.getnext ();
} Images[columns * FOOD.GETX () + food.gety ()]. SetIcon (New ImageIcon ("Image/white.jpg", ""));
Iseat = false;
catch (Interruptedexception e) {e.printstacktrace ();
}} public Snake () {init ();
This.setbounds (80, 80, 400, 400);
This.setvisible (TRUE);
Setdefaultcloseoperation (Javax.swing.WindowConstants.EXIT_ON_CLOSE);
Add global keyboard listener Keyboardfocusmanager Manager = Keyboardfocusmanager. Getcurrentkeyboardfocusmanager ();
Manager.addkeyeventpostprocessor ((keyeventpostprocessor) this. Getmykeyeventhandler ());
}/** * Initialize map/public void init () {JPanel p = new JPanel (new GridLayout (Rows, columns, 1, 1));
SetLayout (New BorderLayout ());
for (int x = 0; x < rows x + +) {for (int y = 0; y < columns; y++) {ImageIcon ImageIcon; if (x = = 0 | | | x = = Rows-1 | | y = 0 | | y = = columns-1) {ImageIcon = new ImageIcon ("Image/red.jpg", "");
else {ImageIcon = new ImageIcon ("Image/white.jpg", "");
} images[columns * x + y] = new JLabel (ImageIcon);
P.add (images[columns * x + y]);
} getcontentpane (). Add (P, borderlayout.center); /** * Keyboard Monitor * * @return/public keyeventpostprocessor Getmykeyeventhandler () {return new Keyeventpostpro Cessor () {public Boolean postprocesskeyevent (KeyEvent e) {if (E.getid ()!= keyevent.key_pressed) {return FA
Lse
int keycode = E.getkeycode (); if (keycode = = keyevent.vk_up) {if (Snakeheader.getnext ()!= null) {//To determine whether the direction can be turned int x1 = SNAKEHEADER.G
EtX ();
int y1 = snakeheader.gety ();
int x2 = Snakeheader.getnext (). GetX ();
int y2 = Snakeheader.getnext (). GetY ();
if (y1 = = y2 && x1-x2 = 1) {return true;
} Driection_now = up; else if (keycode = = keyevent.vk_right) {iF (snakeheader.getnext ()!= null) {int x1 = SNAKEHEADER.GETX ();
int y1 = snakeheader.gety ();
int x2 = Snakeheader.getnext (). GetX ();
int y2 = Snakeheader.getnext (). GetY ();
if (x1 = = x2 && y2-y1 = 1) {return true;
} Driection_now = right;
else if (keycode = = Keyevent.vk_down) {if (Snakeheader.getnext ()!= null) {int x1 = SNAKEHEADER.GETX ();
int y1 = snakeheader.gety ();
int x2 = Snakeheader.getnext (). GetX ();
int y2 = Snakeheader.getnext (). GetY ();
if (y1 = = y2 && x2-x1 = 1) {return true;
} Driection_now = down;
else if (keycode = = Keyevent.vk_left) {if (Snakeheader.getnext ()!= null) {int x1 = SNAKEHEADER.GETX ();
int y1 = snakeheader.gety ();
int x2 = Snakeheader.getnext (). GetX ();
int y2 = Snakeheader.getnext (). GetY ();
if (x1 = = x2 && y1-y2 = 1) {return true; }} DriecTion_now = left;
return true;
}
};
/** * Calculate the direction and displacement of the snake * * @param header/public static void next () {if (Snakeheader = null) return;
TAILX = Snaketailer.getx ();
taily = Snaketailer.gety ();
Snakenode Pnow = Snaketailer;
while (Pnow!= null) {if (Pnow = = Snakeheader) {break;
} pnow.setx (Pnow.getahead (). GetX ());
Pnow.sety (Pnow.getahead (). GetY ());
Pnow = Pnow.getahead ();
} if (Driection_now = = right) {snakeheader.sety (snakeheader.gety () + 1);
else if (Driection_now = left) {snakeheader.sety (Snakeheader.gety ()-1);
else if (Driection_now = up) {Snakeheader.setx (Snakeheader.getx ()-1);
else if (Driection_now = down) {Snakeheader.setx (Snakeheader.getx () + 1);
The public static void AddTail () {Snakenode tail = new Snakenode (TAILX, taily);
Snaketailer.setnext (tail);
Tail.setahead (Snaketailer);
Snaketailer = Snaketailer.getnext (); public static Snakenode Getfood () {SnakeNode food = new Snakenode ();
Boolean flag = true;
while (true) {int x = new Random (). Nextint (rows);
int y = new Random (). Nextint (columns);
if (x = = 0 | | | x = = ROWS-1 | | | y = 0 | | y = = columns-1) {continue;
} snakenode pnow = Snakeheader;
while (Pnow!= null) {if (x = = Pnow.getx () && y = = pnow.gety ()) {flag = false;
} Pnow = Pnow.getnext ();
} if (flag) {food = new Snakenode (x, y);
Break
} return food; public static Boolean Judgeend () {//Touch Wall judgment if (snakeheader.getx () = 0 | | snakeheader.getx () = rows-1 | | sna Keheader.gety () = = 0 | |
Snakeheader.gety () = = Columns-1) {return true;
//Touch Body judgment snakenode Pnow = Snakeheader.getnext (); while (Pnow!= null) {if (snakeheader.getx () = = Pnow.getx () && snakeheader.gety () = = Pnow.gety ()) {S
Ystem.out.println ("========= touches body ===========");
return true;
} Pnow = Pnow.getnext ();
return false; }
}