📄 anagent.java
字号:
import java.awt.Color;
import java.awt.Image;
import java.util.Vector;
import javax.swing.ImageIcon;
import java.lang.Object;
import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.space.Object2DTorus;
import uchicago.src.sim.util.Random;
import uchicago.src.sim.util.SimUtilities;
public class anAgent implements Drawable {
public final static int police=0;
public final static int thief=1;
public int X, Y;
public Object2DTorus space;
public int type;
public Color color=Color.blue;
public int direction;
public int step;
public anAgent(int x, int y, Object2DTorus space){
this.space = space;
this.X=x;
this.Y=y;
}
public anAgent(Object2DTorus space,int spSz){
this.space=space;
this.step=0;
double tempD=Math.random();
System.out.println(tempD);
do {
this.X=Random.uniform.nextIntFromTo(0,space.getSizeX()-1);
this.Y=Random.uniform.nextIntFromTo(0,space.getSizeY()-1);
}
while(space.getObjectAt(this.X,this.Y)!=null);
space.putObjectAt(this.X,this.Y,this);
if( Math.random() <= 0.77)
type=this.type=0;
else
type=this.type=1;
}
@Override
public void draw(SimGraphics arg0) {
// TODO Auto-generated method stub
//
if (this.type==police){
// arg0.drawImage(this.policePicture);
arg0.drawCircle(Color.green);
// this.loadpolicePicture();
}
else {
// arg0.drawImage(this.thiefPicture);
arg0.drawCircle(Color.red);
// this.loadthiefPicture();}
}}
/*
// private static Image policePicture;
// private static Image thiefPicture;
//
// private static void loadpolicePicture() {
// if (policePicture == null) {
// java.net.URL policePicURL =
// anAgent.class.getResource("33.jpg");
// policePicture = new ImageIcon(policePicURL).getImage();
// }
// }
// private static void loadthiefPicture() {
// if (thiefPicture == null) {
// java.net.URL thiefPicURL =
// anAgent.class.getResource("34.jpg");
// thiefPicture = new ImageIcon(thiefPicURL).getImage();
// }
// }*/
@Override
public int getX() {
// TODO Auto-generated method stub
return 0;
}
public void move (){
int direction,newx,newy,counter;
newx=X;
newy=Y;
counter=0;
do
{
counter++;
//if the agent is inspired its direction is set in getinspired
//else set random.
if(isInspired())
getInspired();
else{
this.direction= Random.uniform.nextIntFromTo(0, 7);}
Vector nbors=null;
nbors=space.getMooreNeighbors(this.X, this.Y, false);
if (nbors.size()==8)break;
if (counter>1)this.direction = Random.uniform.nextIntFromTo(0, 7);
newx=X;
newy=Y;
switch( this.direction )
{
case 0: ++newx; ++newy; break;
case 1: ++newx; ++newy; break;
case 2: ++newy; break;
case 3: --newx; ++newy; break;
case 4: --newx; break;
case 5: --newx; --newy; break;
case 6: --newy; break;
case 7: ++newx; --newy; break;
default:
}
}while(space.getObjectAt(newx, newy)!=null);
space.putObjectAt(X,Y,null);
X=newx;
Y=newy;
space.putObjectAt(X, Y, this);
}
public boolean isInspired(){
Vector nbors=null;
boolean mybool=false;
if (this.type==police){
nbors=space.getMooreNeighbors(this.X, this.Y,5,5, false);
for (int i=0; i<(nbors.size()-1);i++){
anAgent nghbAgent=(anAgent) nbors.get(i);
if(nghbAgent.type==thief)mybool=true;
else
direction = Random.uniform.nextIntFromTo(0, 7);
}
}
else {
for (int k = 0 ; k<10; k++){
if(this.direction==6){
nbors=space.getMooreNeighbors(this.Y,k,false);
for (int i=0; i<(nbors.size()-1);i++){
anAgent nghbAgent=(anAgent) nbors.get(i);
if(nghbAgent.type!=police)mybool=true;
}}
else if
(this.direction==2){
nbors=space.getMooreNeighbors(this.X, this.Y,k,k,false);
for (int i=0; i<(nbors.size()-1);i++){
anAgent nghbAgent=(anAgent) nbors.get(i);
if(nghbAgent.type!=police)mybool=true;
else repel(nghbAgent.direction);
}}}}
return mybool;
}
// if (nbors.size()>0)
// return true;
// else 431
// return false;}
public void getInspired(){
Vector nbors=null;
if (this.type==police)nbors=space.getMooreNeighbors(this.X, this.Y,5,5, false);
else nbors=space.getVonNeumannNeighbors(this.X, this.Y,10,10, false);
SimUtilities.shuffle(nbors);
if (this.type==thief){
for (int i=0; i<(nbors.size()-1);i++){
anAgent nghbAgent=(anAgent) nbors.get(i);
if (nghbAgent.type==police){
repel((nghbAgent.direction));
break;
}
}
}
else {
for (int i=0; i<(nbors.size()-1);i++){
anAgent nghbAgent=(anAgent) nbors.get(i);
if (nghbAgent.type==thief){
attract(this.X,this.Y, nghbAgent.X,nghbAgent.Y);
if((this.X-5 < nghbAgent.X) ||(nghbAgent.X < this.X+5) || (this.Y-5 < nghbAgent.Y) || (nghbAgent.Y < this.Y+5)){
space.putObjectAt(nghbAgent.X, nghbAgent.Y, null);
System.out.println("try");}
}
}
}
}
// 3 2 1
// 4 A 0
// 5 6 7
private void attract(int x1, int y1, int x2, int y2) {
if (x2>x1){
if(y2>y1)this.direction=1;
else if(y2==y1)this.direction=0;
else this.direction=7;
}
if (x2==x1){
if(y2>y1)this.direction=2;
//IMPOSSIBLE!:else if(y2==y1)this.direction=???;
else this.direction=6;
}
if (x2<x1){
if(y2>y1)this.direction=3;
else if(y2==y1)this.direction=4;
else this.direction=5;
}
}
private void repel(int direction2 ) {
if (this.direction==7)
this.direction=3;
else if (direction2==5)
this.direction=1;
else if(direction2==3)
this.direction=7;
else if(direction2==1)
this.direction=5;
else if (direction2==2)
this.direction=6;
else if(direction2==4)
this.direction=0;
else
this.direction=4;
}
// 3 2 1
// 4 A 0
// 5 6 7
@Override
public int getY() {
// TODO Auto-generated method stub
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -