面向对象蚂蚁爬杆的问题

package cn.hncu.day5.ant;

public class Ant {
private int pos;
private boolean isLeft = true;// 默认朝左
private boolean isDown = false;// 默认在细杆上
private final static int LEFT_END=0;
private final static int RIGHT_END=27;

private static int numOfDown;
private static int num;
private static int minTime=Integer.MAX_VALUE,maxTime;
private static int time=0;

public Ant(int pos) {
this.pos = pos;
}
public void step(int i){
if(!isDown){
if(isLeft){
pos-=i;
}else{
pos+=i;
}
if(pos<=LEFT_END || pos>=RIGHT_END){
isDown = true;
numOfDown++;
if(numOfDown==num){
//System.out.println("全部掉下去了....");
if(time<minTime){
minTime = time;
}
if(time>maxTime){
maxTime=time;
}
}
}

}
}
public static int getNum() {
return num;
}
public static void setNum(int num) {
Ant.num = num;
}
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
public boolean isLeft() {
return isLeft;
}
public void setLeft(boolean isLeft) {
this.isLeft = isLeft;
}
public boolean isDown() {
return isDown;
}
public void setDown(boolean isDown) {
this.isDown = isDown;
}

public static int getNumOfDown() {
return numOfDown;
}
public static void setNumOfDown(int numOfDown) {
Ant.numOfDown = numOfDown;
}
public static boolean isOver(){
return numOfDown==num;
}
public void turnAround(){
isLeft = !isLeft;
}
public static void setTime(int n){
time=n;
}
public static void timeGo(){
time++;
}
public static int getMinTime() {
return minTime;
}
public static int getMaxTime() {
return maxTime;
}

}

----------------------------------------------------

package cn.hncu.day5.ant;

public class AntRun {
public static void main(String[] args) {
//创建蚂蚁
int a[]={3,7,11,18,23};
int numOfDirectionStates=1;//所有蚂蚁可能的朝向状态数
Ant ants[] = new Ant[a.length];
for(int i=0;i<ants.length;i++){
ants[i] = new Ant( a[i] );
numOfDirectionStates *=2;
}
Ant.setNum(ants.length);

//把所有蚂蚁可能的朝向记录到一个数组
boolean directionStates[][] = new boolean[numOfDirectionStates][ants.length];
// for(int i=0;i<directionStates.length;i++){
// directionStates[i][0] = ((i & 0x01) ==0) ;
// directionStates[i][1] = ((i & 0x02) ==0) ;
// directionStates[i][2] = ((i & 0x04) ==0) ;
// directionStates[i][3] = ((i & 0x08) ==0) ;
// directionStates[i][4] = ((i & 0x10) ==0) ;
// }
for(int i=0;i<directionStates.length;i++){
for(int j=0;j<directionStates[i].length;j++){
directionStates[i][j]= ((i & (1<<j)) ==0) ;
}
}


//让这些蚂蚁在细杆上爬

for (int i = 0; i < directionStates.length; i++) {//总共有a^ants.length-1即directionStates.length多种朝向状态,每种都用来跑一次
//对每只蚂蚁进行初始化
//给每只蚂蚁设置初始朝向,初始位置,是否掉下去
for(int j=0;j<directionStates[i].length;j++){
ants[j].setLeft(directionStates[i][j]);
ants[j].setPos(a[j]);
ants[j].setDown(false);
}
Ant.setNumOfDown(0);
Ant.setTime(0);
//开跑
print(ants);
while (!Ant.isOver()) {
Ant.timeGo();
for (int j = 0; j < ants.length; j++) {//先让每只蚂蚁往前走一步
ants[j].step(1);
}
//判断是否碰头
for(int j=0; j<ants.length-1;j++){
//碰头只可能发生在 左边那只向右,右边那只向左---且都在细杆上
if(!ants[j].isLeft() && ants[j+1].isLeft() &&
!ants[j].isDown()&&!ants[j+1].isDown() ){
//根据位置判断:碰头或撞头
//1碰头--换方向
if(ants[j].getPos()==ants[j+1].getPos()){
System.out.println("碰头...");
ants[j].turnAround();
ants[j+1].turnAround();
}
//2撞头--回退1步且换方向
if(ants[j].getPos()>ants[j+1].getPos()){
System.out.println("撞头....");
ants[j].step(-1);
ants[j+1].step(-1);
ants[j].turnAround();
ants[j+1].turnAround();
}
}

}

print(ants);
}
}

System.out.println("※※※minTime="+Ant.getMinTime()+",maxTime="+Ant.getMaxTime());
}

private static void print(Ant ants[]){
for(int i=0;i<ants.length;i++){
if(i<ants.length-1){
System.out.print(ants[i].getPos()+",");
}else{
System.out.println(ants[i].getPos());
}
}
}
}

posted @ 2016-06-20 12:30  折腾青春  阅读(668)  评论(0编辑  收藏  举报