|
|
@ -9,13 +9,11 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
|
|
|
|
|
|
|
import com.github.drinkjava2.frog.brain.group.RandomConnectGroup;
|
|
|
|
|
|
|
|
import com.github.drinkjava2.frog.egg.Egg;
|
|
|
|
import com.github.drinkjava2.frog.egg.Egg;
|
|
|
|
import com.github.drinkjava2.frog.egg.EggTool;
|
|
|
|
import com.github.drinkjava2.frog.egg.EggTool;
|
|
|
|
import com.github.drinkjava2.frog.objects.Food;
|
|
|
|
import com.github.drinkjava2.frog.objects.LetterTester;
|
|
|
|
|
|
|
|
import com.github.drinkjava2.frog.objects.EnvObject;
|
|
|
|
import com.github.drinkjava2.frog.objects.Material;
|
|
|
|
import com.github.drinkjava2.frog.objects.Material;
|
|
|
|
import com.github.drinkjava2.frog.objects.Object;
|
|
|
|
|
|
|
|
import com.github.drinkjava2.frog.objects.SeeSaw;
|
|
|
|
|
|
|
|
import com.github.drinkjava2.frog.util.RandomUtils;
|
|
|
|
import com.github.drinkjava2.frog.util.RandomUtils;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -27,21 +25,29 @@ import com.github.drinkjava2.frog.util.RandomUtils;
|
|
|
|
@SuppressWarnings("all")
|
|
|
|
@SuppressWarnings("all")
|
|
|
|
public class Env extends JPanel {
|
|
|
|
public class Env extends JPanel {
|
|
|
|
/** Speed of test */
|
|
|
|
/** Speed of test */
|
|
|
|
public static final int SHOW_SPEED = 500; // 测试速度,-1000~1000,可调, 数值越小,速度越慢
|
|
|
|
public static final int SHOW_SPEED = 1; // 测试速度,-1000~1000,可调, 数值越小,速度越慢
|
|
|
|
|
|
|
|
|
|
|
|
/** Delete eggs at beginning of each run */
|
|
|
|
/** Delete eggs at beginning of each run */
|
|
|
|
public static final boolean DELETE_EGGS = false;// 每次运行是否先删除保存的蛋
|
|
|
|
public static final boolean DELETE_EGGS = true;// 每次运行是否先删除保存的蛋
|
|
|
|
|
|
|
|
|
|
|
|
public static final int EGG_QTY = 25; // 每轮下n个蛋,可调,只有最优秀的前n个青蛙们才允许下蛋
|
|
|
|
public static final int EGG_QTY = 10; // 每轮下n个蛋,可调,只有最优秀的前n个青蛙们才允许下蛋
|
|
|
|
|
|
|
|
|
|
|
|
public static final int FROG_PER_EGG = 4; // 每个蛋可以孵出几个青蛙
|
|
|
|
public static final int FROG_PER_EGG = 4; // 每个蛋可以孵出几个青蛙
|
|
|
|
|
|
|
|
|
|
|
|
public static final int SCREEN = 100; // 分几屏测完
|
|
|
|
public static final int SCREEN = 1; // 分几屏测完
|
|
|
|
|
|
|
|
|
|
|
|
public static final int FROG_PER_SCREEN = EGG_QTY * FROG_PER_EGG / SCREEN; // 每屏上显示几个青蛙,这个数值由上面三个参数计算得来
|
|
|
|
public static final int FROG_PER_SCREEN = EGG_QTY * FROG_PER_EGG / SCREEN; // 每屏上显示几个青蛙,这个数值由上面三个参数计算得来
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Frog's brain size is a 3D array of Cell */ // 脑空间是个三维Cell数组,为节约内存,仅在用到数组元素时才去初始化这维,按需分配内存
|
|
|
|
|
|
|
|
public static final int FROG_BRAIN_XSIZE = 30; // frog的脑在X方向长度
|
|
|
|
|
|
|
|
public static final int FROG_BRAIN_YSIZE = 20; // frog的脑在Y方向长度
|
|
|
|
|
|
|
|
public static final int FROG_BRAIN_ZSIZE = 20; // frog的脑在Z方向长度
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** SHOW first frog's brain structure */
|
|
|
|
|
|
|
|
public static boolean SHOW_FIRST_FROG_BRAIN = true; // 是否显示脑图在Env区的右侧
|
|
|
|
|
|
|
|
|
|
|
|
/** Draw first frog's brain after some steps */
|
|
|
|
/** Draw first frog's brain after some steps */
|
|
|
|
public static int DRAW_BRAIN_AFTER_STEPS = 50; // 以此值为间隔动态画出脑图,设为0则关闭这个动态脑图功能,只显示一个静态、不闪烁的脑图
|
|
|
|
public static int DRAW_BRAIN_AFTER_STEPS = 1; // 以此值为间隔动态画出脑图,设为0则关闭这个动态脑图功能,只显示一个静态、不闪烁的脑图
|
|
|
|
|
|
|
|
|
|
|
|
/** Environment x width, unit: pixels */
|
|
|
|
/** Environment x width, unit: pixels */
|
|
|
|
public static final int ENV_WIDTH = 400; // 虚拟环境的宽度, 可调
|
|
|
|
public static final int ENV_WIDTH = 400; // 虚拟环境的宽度, 可调
|
|
|
@ -50,30 +56,31 @@ public class Env extends JPanel {
|
|
|
|
public static final int ENV_HEIGHT = ENV_WIDTH; // 虚拟环境高度, 可调,通常取正方形
|
|
|
|
public static final int ENV_HEIGHT = ENV_WIDTH; // 虚拟环境高度, 可调,通常取正方形
|
|
|
|
|
|
|
|
|
|
|
|
/** Frog's brain display width on screen, not important */
|
|
|
|
/** Frog's brain display width on screen, not important */
|
|
|
|
public static final int FROG_BRAIN_DISP_WIDTH = 400; // Frog的脑图在屏幕上的显示大小,可调
|
|
|
|
public static final int FROG_BRAIN_DISP_WIDTH = 600; // Frog的脑图在屏幕上的显示大小,可调
|
|
|
|
|
|
|
|
|
|
|
|
/** Steps of one test round */
|
|
|
|
/** Steps of one test round */
|
|
|
|
public static final int STEPS_PER_ROUND = 2000;// 每轮测试步数,可调
|
|
|
|
public static final int STEPS_PER_ROUND = LetterTester.STR.length() * 2 * LetterTester.TIME;// 每轮测试步数,可调
|
|
|
|
|
|
|
|
public static int step;// 当前测试步数
|
|
|
|
/** Frog's brain width, fixed to 1000 unit */
|
|
|
|
|
|
|
|
public static final float FROG_BRAIN_WIDTH = 1000; // frog的脑宽度固定为1000,不要随便调整,因为器官的相对位置和大小是按脑大小设定的
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final int FOOD_QTY = 1500; // 食物数量, 可调
|
|
|
|
public static final int FOOD_QTY = 100; // 食物数量, 可调
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 以下是程序内部变量,不要手工修改它们
|
|
|
|
public static boolean pause = false; // 暂停按钮按下将暂停测试
|
|
|
|
public static boolean pause = false; // 暂停按钮按下将暂停测试
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[][] bricks = new byte[ENV_WIDTH][ENV_HEIGHT];// 组成环境的材料,0=无, 1=食, 其它=其它...
|
|
|
|
public static byte[][] bricks = new byte[ENV_WIDTH][ENV_HEIGHT];// 组成环境的材料,见Material.java
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Frog> frogs = new ArrayList<>(); // 这里存放所有待测的青蛙,可能分几次测完,由FROG_PER_SCREEN大小来决定
|
|
|
|
public static List<Frog> frogs = new ArrayList<>(); // 这里存放所有待测的青蛙,可能分几次测完,由FROG_PER_SCREEN大小来决定
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Egg> eggs = new ArrayList<>(); // 这里存放从磁盘载入或上轮下的蛋,每个蛋可能生成1~n个青蛙,
|
|
|
|
public static List<Egg> eggs = new ArrayList<>(); // 这里存放新建或从磁盘载入上轮下的蛋,每个蛋可能生成几个青蛙,
|
|
|
|
|
|
|
|
|
|
|
|
public static Object[] things = new Object[] { new SeeSaw() };
|
|
|
|
public static EnvObject[] things = new EnvObject[] {};// 所有外界物体,如食物、字母测试工具都放在这个things里面
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
System.out.println("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8
|
|
|
|
System.out.println("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8
|
|
|
|
|
|
|
|
System.out.println("脑图快捷键: T:顶视 F:前视 L:左视 R:右视 X:斜视 方向键:剖视 空格:暂停 鼠标:缩放旋转平移");
|
|
|
|
if (DELETE_EGGS)
|
|
|
|
if (DELETE_EGGS)
|
|
|
|
EggTool.deleteEggs();
|
|
|
|
EggTool.deleteEggs();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Env() {
|
|
|
|
public Env() {
|
|
|
@ -148,7 +155,7 @@ public class Env extends JPanel {
|
|
|
|
int leftfood = 0;
|
|
|
|
int leftfood = 0;
|
|
|
|
for (int x = 0; x < ENV_WIDTH; x++)
|
|
|
|
for (int x = 0; x < ENV_WIDTH; x++)
|
|
|
|
for (int y = 0; y < ENV_HEIGHT; y++)
|
|
|
|
for (int y = 0; y < ENV_HEIGHT; y++)
|
|
|
|
if (bricks[x][y] == 1)
|
|
|
|
if (bricks[x][y] == Material.FOOD)
|
|
|
|
leftfood++;
|
|
|
|
leftfood++;
|
|
|
|
return FOOD_QTY - leftfood;
|
|
|
|
return FOOD_QTY - leftfood;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -163,7 +170,18 @@ public class Env extends JPanel {
|
|
|
|
.append(foodFound * 1.0f / FROG_PER_SCREEN).append(",最多:").append(maxFound).toString();
|
|
|
|
.append(foodFound * 1.0f / FROG_PER_SCREEN).append(",最多:").append(maxFound).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void sleep(long millis) {
|
|
|
|
public static void checkIfPause(Frog f) {
|
|
|
|
|
|
|
|
if (pause)
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
if (f != null) {
|
|
|
|
|
|
|
|
Application.brainPic.drawBrainPicture(f);
|
|
|
|
|
|
|
|
Application.brainPic.requestFocus();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(100);
|
|
|
|
|
|
|
|
} while (pause);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void sleep(long millis) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Thread.sleep(millis);
|
|
|
|
Thread.sleep(millis);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
} catch (InterruptedException e) {
|
|
|
@ -181,16 +199,18 @@ public class Env extends JPanel {
|
|
|
|
rebuildFrogs();
|
|
|
|
rebuildFrogs();
|
|
|
|
for (int screen = 0; screen < SCREEN; screen++) {// 分屏测试,每屏FROG_PER_SCREEN个蛙
|
|
|
|
for (int screen = 0; screen < SCREEN; screen++) {// 分屏测试,每屏FROG_PER_SCREEN个蛙
|
|
|
|
time0 = System.currentTimeMillis();
|
|
|
|
time0 = System.currentTimeMillis();
|
|
|
|
if (pause)
|
|
|
|
for (EnvObject thing : things) // 创建食物、陷阱等物体
|
|
|
|
do {
|
|
|
|
|
|
|
|
sleep(300);
|
|
|
|
|
|
|
|
} while (pause);
|
|
|
|
|
|
|
|
for (Object thing : things) // 创建食物、陷阱等物体
|
|
|
|
|
|
|
|
thing.build();
|
|
|
|
thing.build();
|
|
|
|
boolean allDead = false;
|
|
|
|
boolean allDead = false;
|
|
|
|
Frog firstFrog = frogs.get(screen * FROG_PER_SCREEN);
|
|
|
|
Frog firstFrog = frogs.get(screen * FROG_PER_SCREEN);
|
|
|
|
for (int i = 0; i < STEPS_PER_ROUND; i++) {
|
|
|
|
checkIfPause(firstFrog);
|
|
|
|
for (Object thing : things)// 调用食物、陷阱等物体的动作
|
|
|
|
for (int j = 0; j < FROG_PER_SCREEN; j++) {
|
|
|
|
|
|
|
|
Frog f = frogs.get(screen * FROG_PER_SCREEN + j);
|
|
|
|
|
|
|
|
f.initFrog(); // 初始化器官延迟到这一步,是因为脑细胞太占内存,而且当前屏测完后会清空
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (step = 0; step < STEPS_PER_ROUND; step++) {
|
|
|
|
|
|
|
|
for (EnvObject thing : things)// 调用食物、陷阱等物体的动作
|
|
|
|
thing.active(screen);
|
|
|
|
thing.active(screen);
|
|
|
|
if (allDead)
|
|
|
|
if (allDead)
|
|
|
|
break; // 青蛙全死光了就直接跳到下一轮,以节省时间
|
|
|
|
break; // 青蛙全死光了就直接跳到下一轮,以节省时间
|
|
|
@ -199,14 +219,9 @@ public class Env extends JPanel {
|
|
|
|
Frog f = frogs.get(screen * FROG_PER_SCREEN + j);
|
|
|
|
Frog f = frogs.get(screen * FROG_PER_SCREEN + j);
|
|
|
|
if (f.active(this))
|
|
|
|
if (f.active(this))
|
|
|
|
allDead = false;
|
|
|
|
allDead = false;
|
|
|
|
if (f.alive && RandomUtils.percent(0.5f)) {// 有很小的机率在青蛙活着时就创建新的器官
|
|
|
|
|
|
|
|
RandomConnectGroup newConGrp = new RandomConnectGroup();
|
|
|
|
|
|
|
|
newConGrp.initFrog(f);
|
|
|
|
|
|
|
|
f.organs.add(newConGrp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SHOW_SPEED > 0 && i % SHOW_SPEED != 0) // 用画青蛙的方式来拖慢速度
|
|
|
|
if (SHOW_SPEED > 0 && step % SHOW_SPEED != 0) // 用画青蛙的方式来拖慢速度
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (SHOW_SPEED < 0) // 如果speed小于0,人为加入延迟
|
|
|
|
if (SHOW_SPEED < 0) // 如果speed小于0,人为加入延迟
|
|
|
@ -223,23 +238,26 @@ public class Env extends JPanel {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (firstFrog.alive) { // 开始显示第一个Frog的动态脑图
|
|
|
|
if (firstFrog.alive) { // 开始显示第一个Frog的动态脑图
|
|
|
|
if (Application.SHOW_FIRST_FROG_BRAIN) {
|
|
|
|
if (SHOW_FIRST_FROG_BRAIN) {
|
|
|
|
g.setColor(Color.red);
|
|
|
|
g.setColor(Color.red);
|
|
|
|
g.drawArc(firstFrog.x - 15, firstFrog.y - 15, 30, 30, 0, 360);
|
|
|
|
g.drawArc(firstFrog.x - 15, firstFrog.y - 15, 30, 30, 0, 360);
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DRAW_BRAIN_AFTER_STEPS > 0 && i % DRAW_BRAIN_AFTER_STEPS == 0)
|
|
|
|
if (DRAW_BRAIN_AFTER_STEPS > 0 && step % DRAW_BRAIN_AFTER_STEPS == 0)
|
|
|
|
Application.brainPic.drawBrainPicture(firstFrog);
|
|
|
|
Application.brainPic.drawBrainPicture(firstFrog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Graphics g2 = this.getGraphics();
|
|
|
|
Graphics g2 = this.getGraphics();
|
|
|
|
g2.drawImage(buffImg, 0, 0, this);
|
|
|
|
g2.drawImage(buffImg, 0, 0, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Application.brainPic.drawBrainPicture(firstFrog);
|
|
|
|
Application.brainPic.drawBrainPicture(firstFrog);
|
|
|
|
|
|
|
|
for (int j = 0; j < FROG_PER_SCREEN; j++) {
|
|
|
|
|
|
|
|
Frog f = frogs.get(screen * FROG_PER_SCREEN + j);
|
|
|
|
|
|
|
|
f.cells = null; // 清空frog脑细胞所占用的内存
|
|
|
|
|
|
|
|
}
|
|
|
|
Application.mainFrame.setTitle(new StringBuilder("Round: ").append(round).append(", screen:")
|
|
|
|
Application.mainFrame.setTitle(new StringBuilder("Round: ").append(round).append(", screen:")
|
|
|
|
.append(screen).append(", ").append(foodFoundCountText()).append(", 用时: ")
|
|
|
|
.append(screen).append(", ").append(foodFoundCountText()).append(", 用时: ")
|
|
|
|
.append(System.currentTimeMillis() - time0).append("ms").append(", energy:")
|
|
|
|
.append(System.currentTimeMillis() - time0).append("ms").toString());
|
|
|
|
.append(firstFrog.energy).toString());
|
|
|
|
for (EnvObject thing : things)// 去除食物、陷阱等物体
|
|
|
|
for (Object thing : things)// 去除食物、陷阱等物体
|
|
|
|
|
|
|
|
thing.destory();
|
|
|
|
thing.destory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
round++;
|
|
|
|
round++;
|
|
|
|