//---主方法-----------------
publicclassGreedSnake {
publicstaticvoidmain(String[] args) {
SnakeModel model=newSnakeModel();
SnakeControl control=newSnakeControl(model);
SnakeView view=newSnakeView(model,control);
newThread(model).start();
newThread(view).start();
}
}
//---SnakeModel------------
importjava.util.LinkedList;
importjava.util.ListIterator;
importjava.util.Random;
publicclassSnakeModelimplementsRunnable {
staticboolean[][]matrix;
intmaxX,maxY;
staticintfreshTime;
intbufDir;
Nodehead;
Nodefood;
publicstaticfinalintUP=1;
publicstaticfinalintDOWN=3;
publicstaticfinalintLEFT=2;
publicstaticfinalintRIGHT=4;
LinkedList<Node>snakeList=newLinkedList<Node>();
publicNode createFood(){
Random r=newRandom();
intx,y;
while(true){
x=r.nextInt(maxX);
y=r.nextInt(maxY);
if(!matrix[x][y]){
break;
}
}
Node n=newNode(x,y,LEFT);
matrix[x][y]=true;
System.out.println("matrix "+x+" "+y);
returnn;
}
publicSnakeModel(){
head=newNode(2,3,RIGHT);
maxX=30;
maxY=30;
freshTime=200;
matrix=newboolean[maxX][maxY];
snakeList.add(head);
snakeList.add(newNode(2,2,DOWN));
food=createFood();
bufDir=head.drct;
}
publicvoidmove(){
ListIterator<Node> it=snakeList.listIterator();
//吃掉食物
System.out.println("head "+head.x+" "+head.y);
booleannear=false;
switch(head.drct){
caseUP:
if(food.x==head.x&&food.y==(head.y-1)){
near=true;
}
break;
caseDOWN:
if(food.x==head.x&&food.y==(head.y+1)){
near=true;
}
break;
caseLEFT:
if(food.x==(head.x-1)&&food.y==head.y){
near=true;
}
break;
caseRIGHT:
if(food.x==(head.x+1)&&food.y==head.y){
near=true;
}
break;
}
if(near){
System.out.println("eat");
snakeList.addFirst(food);
food.drct=head.drct;
head=food;
food=createFood();
near=false;
}
else{
it.next();
while(it.hasNext()){
Node n=it.next();
if(head.x==n.x&&head.y==n.y){
System.out.println("你输了");
System.exit(0);
}
}
while(it.hasPrevious()){
Node n=it.previous();
if(it.hasPrevious()){
if(snakeList.get(it.previousIndex()).x<n.x){
n.drct=LEFT;
}
elseif(snakeList.get(it.previousIndex()).x>n.x){
n.drct=RIGHT;
}
elseif(snakeList.get(it.previousIndex()).y<n.y){
n.drct=UP;
}
elseif(snakeList.get(it.previousIndex()).y>n.y){
n.drct=DOWN;
}
}
}
while(it.hasNext()){
Node n=it.next();
try{
nodeMove(n);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("你输了");
System.exit(0);
}
}
}
}
publicvoidchangeDirection(){
if(Math.abs(bufDir-head.drct)==2){
bufDir=head.drct;
}
else
head.drct=bufDir;
}
publicvoidnodeMove(Node n){
matrix[n.x][n.y]=false;
switch(n.drct){
caseUP:
n.y--;
break;
caseDOWN:
n.y++;
break;
caseLEFT:
n.x--;
break;
caseRIGHT:
n.x++;
break;
}
matrix[n.x][n.y]=true;
}
publicvoidrun(){
while(true){
changeDirection();
move();
try{
Thread.sleep(freshTime);
}catch(Exception e){}
}
}
}
classNode{
intx;
inty;
intdrct;
Node(intx,inty,intdirect){
this.x=x;
this.y=y;
drct=direct;
}
}
//---SnakeControl-------------------------
importjava.awt.Frame;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
publicclassSnakeControlextendsKeyAdapter{
SnakeModelmodel;
SnakeControl(SnakeModel model){
this.model=model;
}
publicvoidkeyPressed(KeyEvent e){
switch(e.getKeyCode()){
case38:
model.bufDir=SnakeModel.UP;
break;
case40:
model.bufDir=SnakeModel.DOWN;
break;
case37:
model.bufDir=SnakeModel.LEFT;
break;
case39:
model.bufDir=SnakeModel.RIGHT;
}
}
}
//---SnakeView----------------------
importjava.awt.Color;
importjava.awt.Frame;
importjava.awt.Graphics;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
publicclassSnakeViewextendsFrameimplementsRunnable{
intnodeWidth=10;
intnodeHeight=10;
SnakeView(SnakeModel model,SnakeControl control){
model.maxX=model.maxX;
model.maxY=model.maxY;
setSize(model.maxX*nodeWidth+20,model.maxY*nodeHeight+40);
setLocation(300,200);
setVisible(true);
addKeyListener(control);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvent e){
System.exit(0);
}
});
setBackground(Color.RED);
setResizable(false);
}
publicvoidrepaint(){
Graphics g=getGraphics();
for(inti=0;i<SnakeModel.matrix.length;i++){
for(intj=0;j<SnakeModel.matrix[i].length;j++){
if(SnakeModel.matrix[i][j]){
g.setColor(Color.BLACK);
}
else{
g.setColor(Color.YELLOW);
}
g.fillRect(i*nodeWidth+10, j*nodeHeight+30,nodeWidth-1,nodeHeight-1);
}
}
}
publicvoidrun(){
while(true){
try{
Thread.sleep(SnakeModel.freshTime);
}catch(Exception e){
System.out.println("refresh error");
}
repaint();
}
}
}