嗯。这是我写的最简单的贪吃蛇了。代码一部分参考一位大神的。但是我现在找不到了。非原创供大家参考用。另外我加了一些功能。
package com;
import java.util.*;
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 int hashCode() {
return 6;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
} else if (obj == this) {
return true;
} else {
Node n = (Node) obj;
return n.i == this.i && n.j == this.j;
}
}
}
class WormPanel {
private Worm worm;
private HashSet<Node> food;
public Worm getWorm() {
return worm;
}
class Worm implements Runnable {
private int dirInt = -10;
private LinkedList<Node> nodes;// 蛇由点组成
public Worm() {
nodes = new LinkedList<Node>();// 初始化蛇
nodes.add(new Node(2, 9));
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));
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
step();
shiTu();
try {
Thread.sleep(600);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void step() {
Node n, new1;
Scanner sc = new Scanner(System.in);
// while(true){
n = nodes.get(0);
int x = n.getI() + dirInt / 10;
int y = n.getJ() + dirInt % 10;
switch(x){
case 0:
x=9;break;
case 9:
x=0;break;
}
switch(y){
case 0:
y=31;break;
case 31:
y=0;break;
}
new1 = new Node(x,y );
if (nodes.contains(new1)) {
throw new RuntimeException("吃自己了");
}
nodes.add(0, new1);
if (food.remove(new1)) {
randFood();
return;
}
nodes.remove(nodes.size() - 1);
}
public void step(char dir) {
Node n, new1;
switch (dir) {
case 'a':
if ((this.dirInt - 1) == 0) {
break;
}
this.dirInt = -1;
step();
break;
case 'w':
if ((this.dirInt - 10) == 0) {
break;
}
this.dirInt = -10;
step();
break;
case 's':
if ((this.dirInt + 10) == 0) {
break;
}
this.dirInt = 10;
step();
break;
case 'd':
if ((this.dirInt + 1) == 0) {
break;
}
this.dirInt = 1;
step();
break;
default:
break;
}
}
public boolean contains(int x, int y) {// 判断蛇体包含该座标点吗
return nodes.contains(new Node(x, y));
}
}
public WormPanel() {
worm = new Worm();
Thread t = new Thread(worm);
t.start();
food = new HashSet<Node>();
randomFood();// 随机生成食物
}
public void randFood() {
Random rand = new Random();
int x = rand.nextInt(9);
int y = rand.nextInt(31);
food.add(new Node(x, y));
}
public void randomFood() {
Random rand = new Random();
int i = 0;
int x = rand.nextInt(9);
int y = rand.nextInt(31);
while (i < 3) {
food.add(new Node(x, y));
x = rand.nextInt(9);
y = rand.nextInt(31);
i++;
}
}
public void shiTu() {// 用来绘画出当前的情况
for (int j = 0; j < 31; j++) {
System.out.print("-");
}
System.out.println();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 32; j++) {
if (j == 0 || j == 31) {
System.out.print("|");
} else if (worm.contains(i, j)) {
System.out.print("#");
} else if (food.contains(new Node(i, j))) {
System.out.print("0");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int j = 0; j < 31; j++) {
System.out.print("-");
}
System.out.println();
}
}
class demo1 {
public static void main(String[] args) {
WormPanel w = new WormPanel();
w.shiTu();
Scanner sc = new Scanner(System.in);
while (true) {
char c = sc.next().charAt(0);
w.getWorm().step(c);
w.shiTu();
}
}
}