//定义一个节点类
public class Node {
private int i;
private int j;
public Node() {
}
public Node(int i, int j) {
super();
this.i = i;
this.j = j;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public boolean equals(Object obj) {
if(obj==null)
return false;
if (this==obj)
return true;
if(obj instanceof Node){
Node other=(Node)obj;
return i==other.i&&j==other.j;
}
return false;
}
public int hashCode() {
return i*10000+j;
}
public String toString() {
return "#";
}
}
//创建一个蛇的类,蛇身为一个list集合,定义走一步的方法以及随机生成一个不在蛇身上的点 控制蛇去吃
public class Worm{
List<Node> body=new ArrayList<Node>();
public static final int DOWN=10;
public static final int UP=-10;
public static final int LEFT=-1;
public static final int RIGHT=1;
private int dir;//当前方向
public Worm() {
}
public Worm(Node[] nodes,int dir) {
List<Node> list=Arrays.asList(nodes);
body.addAll(list);
this.dir=dir;
}
//向前走一步
public void spet(){
Node head=body.get(0);
int i=head.getI()+dir/10;
int j=head.getJ()+dir%10;
Node newhead=new Node(i,j);
body.add(0, newhead);
if(eatSomething(dir)){
return;
}else{
body.remove(body.size()-1);
}
}
public void spet(int dir){
if(this.dir+dir==0){
throw new IllegalArgumentException("方向错误");
}
this.dir=dir;
spet();
}
static int m;
static int n;
Timer time=new Timer();
//生成一个随机点,不在蛇身上
public Node newNode(){
time.schedule(new TimerTask() {
public void run() {
Random r=new Random();
m=r.nextInt(30);
n=r.nextInt(60);
}
}, 0, 10000);
if(contains(m, n)){
newNode();
}
return new Node(m,n);
}
//运行随机生成点的方法,每十秒执行一次
Node node2=newNode();
private boolean eatSomething(int dir){
Node node=body.get(0);
if((node.getI()+dir/10)==m&&(node.getJ()+dir%10)==n){
return true;
}
return false;
}
public boolean contains(int i,int j){
for (int idx = 0; idx < body.size(); idx++) {
Node node=body.get(idx);
if(node.getI()==i&&node.getJ()==j){
return true;
}
}
return false;
}
public void chnange() {
for (int idx = 0; idx < body.size(); idx++) {
Node node=body.get(idx);
if(node.getI()<=0){
node.setI(node.getI()+30);
}else if(node.getJ()<=0){
node.setJ(node.getJ()+60);
}else if (node.getI()>=30) {
node.setI(node.getI()-30);
}else if(node.getJ()>=60){
node.setJ(node.getJ()-60);
}
}
}
}
//定义一个蛇场(打印一个区域),把蛇放进去,用打印方法打印出蛇的位置
public class WormPane {
private Worm worm;
public WormPane() {
}
public WormPane(Worm worm) {
this.worm=worm;
}
public void print(){
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 60; j++) {
if(i==0||i==29){
System.out.print("-");
}else if(j==0||j==58){
System.out.print("|");
}else if(worm.contains(i, j)){
System.out.print("#");
}else if(i==worm.m&&j==worm.n){
System.out.print("#");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
//初始化一条蛇,把蛇放在蛇场里。打印出来,自己控制让蛇跑起来去吃东西
public class WormDemo {
public static void main(String[] args) {
Node[] nodes={new Node(4,2),new Node(4,2),new Node(4,3),
new Node(4,4),new Node(5,4),new Node(6,4),new Node(7,4)};
Worm worm=new Worm(nodes, Worm.LEFT);
WormPane pane=new WormPane(worm);
//pane.print();
Scanner sc=new Scanner(System.in);
while(true){
worm.chnange();
pane.print();
String str=sc.nextLine();
if(str.trim().equals("")){
worm.spet();
}else if(str.equalsIgnoreCase("s")){
worm.spet(worm.DOWN);
}else if(str.equalsIgnoreCase("w")){
worm.spet(worm.UP);
}else if(str.equalsIgnoreCase("a")){
worm.spet(worm.LEFT);
}else if(str.equalsIgnoreCase("d")){
worm.spet(worm.RIGHT);
}else if(str.equalsIgnoreCase("q")){
break;
}
}
}
}