From 91cd4ad1283a474fcf19c5a04a2edb6d40478f37 Mon Sep 17 00:00:00 2001 From: drinkjava2 Date: Mon, 16 Sep 2019 08:23:37 -0700 Subject: [PATCH] Done 3d frame show --- .../github/drinkjava2/frog/Application.java | 10 +- .../java/com/github/drinkjava2/frog/Env.java | 16 +- .../java/com/github/drinkjava2/frog/Frog.java | 2 +- .../drinkjava2/frog/brain/BrainPicture.java | 214 ++++++++++++------ .../github/drinkjava2/frog/brain/Cuboid.java | 89 ++++++++ .../drinkjava2/frog/brain/MouseAction.java | 99 ++++++++ .../github/drinkjava2/frog/brain/Organ.java | 33 +-- .../github/drinkjava2/frog/brain/Point.java | 30 +++ .../github/drinkjava2/frog/brain/Zone.java | 84 ------- .../drinkjava2/frog/brain/group/Group.java | 34 --- .../frog/brain/organ/BrainFrame.java | 36 +++ .../frog/brain/organ/PictureEye.java | 36 +++ .../com/github/drinkjava2/frog/egg/Egg.java | 15 +- .../github/drinkjava2/frog/egg/EggTool.java | 3 +- .../drinkjava2/frog/objects/Material.java | 6 +- .../drinkjava2/frog/objects/Pictures.java | 39 ++++ .../drinkjava2/frog/objects/SeeSaw.java | 2 +- .../drinkjava2/frog/util/RandomUtils.java | 27 +-- eggs3d.ser | Bin 1145 -> 0 bytes 19 files changed, 513 insertions(+), 262 deletions(-) create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java delete mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/Zone.java delete mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/group/Group.java create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java create mode 100644 core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java delete mode 100644 eggs3d.ser diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Application.java b/core3d/src/main/java/com/github/drinkjava2/frog/Application.java index d1b0d0a..76d44f0 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Application.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Application.java @@ -27,7 +27,7 @@ public class Application { } public static JFrame mainFrame = new JFrame(); public static Env env = new Env(); - public static BrainPicture brainPic = new BrainPicture(Env.ENV_WIDTH + 5, 0, Env.FROG_BRAIN_WIDTH, + public static BrainPicture brainPic = new BrainPicture(Env.ENV_WIDTH + 5, 0, Env.FROG_BRAIN_RADIUS, Env.FROG_BRAIN_DISP_WIDTH); public static void main(String[] args) throws InterruptedException { @@ -38,8 +38,8 @@ public class Application { mainFrame.add(brainPic); - JButton button = new JButton("Show first frog's brain"); - int buttonWidth = 180; + JButton button = new JButton("Show brain"); + int buttonWidth =100; int buttonHeight = 22; int buttonXpos = Env.ENV_WIDTH / 2 - buttonWidth / 2; button.setBounds(buttonXpos, Env.ENV_HEIGHT + 8, buttonWidth, buttonHeight); @@ -48,13 +48,13 @@ public class Application { public void actionPerformed(ActionEvent arg0) { SHOW_FIRST_FROG_BRAIN = !SHOW_FIRST_FROG_BRAIN; if (SHOW_FIRST_FROG_BRAIN) { - button.setText("Hide first frog's brain"); + button.setText("Hide brain"); int y = Env.ENV_HEIGHT + 100; if (Env.FROG_BRAIN_DISP_WIDTH + 41 > y) y = Env.FROG_BRAIN_DISP_WIDTH + 41; mainFrame.setSize(Env.ENV_WIDTH + Env.FROG_BRAIN_DISP_WIDTH + 25, y); } else { - button.setText("Show first frog's brain"); + button.setText("Show brain"); mainFrame.setSize(Env.ENV_WIDTH + 20, Env.ENV_HEIGHT + 100); } } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Env.java b/core3d/src/main/java/com/github/drinkjava2/frog/Env.java index 41b47af..8b36184 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Env.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Env.java @@ -26,7 +26,7 @@ import com.github.drinkjava2.frog.util.RandomUtils; @SuppressWarnings("all") public class Env extends JPanel { /** Speed of test */ - public static final int SHOW_SPEED = 5; // 测试速度,-1000~1000,可调, 数值越小,速度越慢 + public static final int SHOW_SPEED = 3; // 测试速度,-1000~1000,可调, 数值越小,速度越慢 /** Delete eggs at beginning of each run */ public static final boolean DELETE_EGGS = true;// 每次运行是否先删除保存的蛋 @@ -35,12 +35,12 @@ public class Env extends JPanel { public static final int FROG_PER_EGG = 4; // 每个蛋可以孵出几个青蛙 - public static final int SCREEN = 1; // 分几屏测完 + public static final int SCREEN = 4; // 分几屏测完 public static final int FROG_PER_SCREEN = EGG_QTY * FROG_PER_EGG / SCREEN; // 每屏上显示几个青蛙,这个数值由上面三个参数计算得来 /** Draw first frog's brain after some steps */ - public static int DRAW_BRAIN_AFTER_STEPS = 50; // 以此值为间隔动态画出脑图,设为0则关闭这个动态脑图功能,只显示一个静态、不闪烁的脑图 + public static int DRAW_BRAIN_AFTER_STEPS = 20; // 以此值为间隔动态画出脑图,设为0则关闭这个动态脑图功能,只显示一个静态、不闪烁的脑图 /** Environment x width, unit: pixels */ public static final int ENV_WIDTH = 400; // 虚拟环境的宽度, 可调 @@ -52,12 +52,12 @@ public class Env extends JPanel { public static final int FROG_BRAIN_DISP_WIDTH = 400; // Frog的脑图在屏幕上的显示大小,可调 /** Steps of one test round */ - public static final int STEPS_PER_ROUND = 2000;// 每轮测试步数,可调 + public static final int STEPS_PER_ROUND = 50000;// 每轮测试步数,可调 - /** Frog's brain width, fixed to 1000 unit */ - public static final float FROG_BRAIN_WIDTH = 1000; // frog的脑宽度固定为1000,不要随便调整,因为器官的相对位置和大小是按脑大小设定的 + /** Frog's brain radius */ + public static final float FROG_BRAIN_RADIUS = 20; // frog的脑半径先固定为20个单元 - public static final int FOOD_QTY = 1500; // 食物数量, 可调 + public static final int FOOD_QTY = 100; // 食物数量, 可调 public static boolean pause = false; // 暂停按钮按下将暂停测试 @@ -67,7 +67,7 @@ public class Env extends JPanel { public static List eggs = new ArrayList<>(); // 这里存放从磁盘载入或上轮下的蛋,每个蛋可能生成1~n个青蛙, - public static Object[] things = new Object[] { new Food(), new Trap() }; + public static Object[] things = new Object[] { }; static { System.out.println("唵缚悉波罗摩尼莎诃!"); // 杀生前先打印往生咒,见码云issue#IW4H8 diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java b/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java index cf8be8d..0172422 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/Frog.java @@ -41,7 +41,7 @@ public class Frog { public int x; // frog在Env中的x坐标 public int y; // frog在Env中的y坐标 - public long energy = 100000; // 青蛙的能量为0则死掉 + public long energy = 10000000; // 青蛙的能量为0则死掉 public boolean alive = true; // 设为false表示青蛙死掉了,将不参与计算和显示,以节省时间 public int ateFood = 0; diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java index 44de23f..0c20e0a 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/BrainPicture.java @@ -7,6 +7,9 @@ import static java.awt.Color.MAGENTA; import static java.awt.Color.ORANGE; import static java.awt.Color.RED; import static java.awt.Color.YELLOW; +import static java.lang.Math.cos; +import static java.lang.Math.round; +import static java.lang.Math.sin; import java.awt.Color; import java.awt.Graphics; @@ -14,6 +17,7 @@ import java.awt.Graphics; import javax.swing.JPanel; import com.github.drinkjava2.frog.Application; +import com.github.drinkjava2.frog.Env; import com.github.drinkjava2.frog.Frog; /** @@ -24,65 +28,111 @@ import com.github.drinkjava2.frog.Frog; */ @SuppressWarnings("serial") public class BrainPicture extends JPanel { - private float brainWidth; // real brain width - private int brainDispWidth; // screen display width - private Color color = Color.BLACK; - - public void setColor(Color c) { - color = c; - } + Color color = Color.red; + int brainDispWidth; // screen display piexls width + float scale; // brain scale + int xOffset = 0; // brain display x offset compare to screen + int yOffset = 0; // brain display y offset compare to screen + // float xAngle = (float) (Math.PI / 4); // brain rotate on x axis + // float yAngle = (float) (Math.PI / 4); // brain rotate on y axis + // float zAngle = (float) (Math.PI / 4);// brain rotate on z axis + float xAngle = 0; // brain rotate on x axis + float yAngle = 0; // brain rotate on y axis + float zAngle = 0;// brain rotate on z axis public BrainPicture(int x, int y, float brainWidth, int brainDispWidth) { super(); this.setLayout(null);// 空布局 this.brainDispWidth = brainDispWidth; - this.brainWidth = brainWidth; + scale = 0.6f * brainDispWidth / brainWidth; this.setBounds(x, y, brainDispWidth + 1, brainDispWidth + 1); + MouseAction act = new MouseAction(this); + this.addMouseListener(act); + this.addMouseWheelListener(act); + this.addMouseMotionListener(act); } - public void drawZone( Zone z) {Graphics g = this.getGraphics(); - g.setColor(color); - float rate = brainDispWidth / brainWidth; - int x = Math.round(z.x * rate); - int y = Math.round(z.y * rate); - int radius = Math.round(z.r * rate); - g.drawRect(x - radius, y - radius, radius * 2, radius * 2); + public void drawCube(Cuboid c) { + float x = c.x; + float y = c.y; + float z = c.z; + float xr = c.xr; + float yr = c.yr; + float zr = c.zr; + + setColor(Color.BLUE); + drawTopViewLine(x - xr, y - yr, z - zr, x - xr, y + yr, z - zr);// 画立方体的下面边 + drawTopViewLine(x - xr, y + yr, z - zr, x + xr, y + yr, z - zr); + drawTopViewLine(x + xr, y + yr, z - zr, x + xr, y - yr, z - zr); + drawTopViewLine(x + xr, y - yr, z - zr, x - xr, y - yr, z - zr); + + setColor(Color.green); + drawTopViewLine(x - xr, y - yr, z + zr, x - xr, y - yr, z - zr);// 画立方体的中间边 + setColor(Color.YELLOW); + drawTopViewLine(x + xr, y - yr, z + zr, x + xr, y - yr, z - zr); + setColor(Color.YELLOW); + drawTopViewLine(x + xr, y + yr, z + zr, x + xr, y + yr, z - zr); + setColor(Color.green); + drawTopViewLine(x - xr, y + yr, z + zr, x - xr, y + yr, z - zr); + + setColor(Color.red); + drawTopViewLine(x - xr, y - yr, z + zr, x - xr, y + yr, z + zr); // 画立方体的上面边 + drawTopViewLine(x - xr, y + yr, z + zr, x + xr, y + yr, z + zr); + drawTopViewLine(x + xr, y + yr, z + zr, x + xr, y - yr, z + zr); + drawTopViewLine(x + xr, y - yr, z + zr, x - xr, y - yr, z + zr); } - public void drawCircle( Zone z) {Graphics g = this.getGraphics(); - g.setColor(color); - float rate = brainDispWidth / brainWidth; - int x = Math.round(z.x * rate); - int y = Math.round(z.y * rate); - g.drawArc(x - 5, y - 5, 10, 10, 0, 360); - } - - public void fillZone( Zone z) {Graphics g = this.getGraphics(); - g.setColor(color); - float rate = brainDispWidth / brainWidth; - int x = Math.round(z.x * rate); - int y = Math.round(z.y * rate); - int radius = Math.round(z.r * rate); - g.fillRect(x - radius, y - radius, radius * 2, radius * 2); - } + /*- + 画线,固定以top视角的角度,所以只需要从x1,y1画一条到x2,y2的直线 + 绕 x 轴旋转 θ + x, y.cosθ-zsinθ, y.sinθ+z.cosθ + + 绕 y 轴旋转 θ + z.sinθ+x.cosθ, y, z.cosθ-x.sinθ + + 绕 z 轴旋转 θ + x.cosθ-y.sinθ, x.sinθ+y.consθ, z + -*/ + private void drawTopViewLine(float px1, float py1, float pz1, float px2, float py2, float pz2) { + double x1 = px1 * scale, y1 = py1 * scale, z1 = pz1 * scale, x2 = px2 * scale, y2 = py2 * scale, + z2 = pz2 * scale; + double x, y, z; + y = y1 * cos(xAngle) - z1 * sin(xAngle);// 绕x轴转 + z = y1 * sin(xAngle) + z1 * cos(xAngle); + y1 = y; + z1 = z; + + x = z1 * sin(yAngle) + x1 * cos(yAngle);// 绕y轴转 + z = z1 * cos(yAngle) - x1 * sin(yAngle); + x1 = x; + z1 = z; + + x = x1 * cos(zAngle) - y1 * sin(zAngle);// 绕z轴转 + y = x1 * sin(zAngle) + y1 * cos(zAngle); + x1 = x; + y1 = y; + + y = y2 * cos(xAngle) - z2 * sin(xAngle);// 绕x轴转 + z = y2 * sin(xAngle) + z2 * cos(xAngle); + y2 = y; + z2 = z; + + x = z2 * sin(yAngle) + x2 * cos(yAngle);// 绕y轴转 + z = z2 * cos(yAngle) - x2 * sin(yAngle); + x2 = x; + z2 = z; + + x = x2 * cos(zAngle) - y2 * sin(zAngle);// 绕z轴转 + y = x2 * sin(zAngle) + y2 * cos(zAngle); + x2 = x; + y2 = y; - public void drawLine(Zone z1, Zone z2) { Graphics g = this.getGraphics(); g.setColor(color); - float rate = brainDispWidth / brainWidth; - int x1 = Math.round(z1.x * rate); - int y1 = Math.round(z1.y * rate); - int x2 = Math.round(z2.x * rate); - int y2 = Math.round(z2.y * rate); - g.drawLine(x1, y1, x2, y2); - } - - public void drawText( Zone z, String text) {Graphics g = this.getGraphics(); - g.setColor(color); - float rate = brainDispWidth / brainWidth; - int x = Math.round(z.x * rate); - int y = Math.round(z.y * rate); - g.drawString(text, x - text.length() * 3 - 2, y); + g.drawLine((int) round(x1) + Env.FROG_BRAIN_DISP_WIDTH / 2 + xOffset, + (int) round(y1) + Env.FROG_BRAIN_DISP_WIDTH / 2 + yOffset, + (int) round(x2) + Env.FROG_BRAIN_DISP_WIDTH / 2 + xOffset, + (int) round(y2) + Env.FROG_BRAIN_DISP_WIDTH / 2 + yOffset); } private static final Color[] rainbow = new Color[] { RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, MAGENTA }; @@ -94,28 +144,10 @@ public class BrainPicture extends JPanel { return rainbow[nextColor++]; } - public static Color color(float i) { - if (i == 0) - return Color.black; - if (i == 1) - return Color.RED; - if (i <= 3) - return Color.ORANGE; - if (i <= 10) - return Color.YELLOW; - if (i <= 20) - return Color.GREEN; - if (i <= 50) - return Color.CYAN; - if (i <= 100) - return Color.BLUE; - return Color.MAGENTA; - } - public void drawBrainPicture(Frog frog) { if (!Application.SHOW_FIRST_FROG_BRAIN) return; - Graphics g = this.getGraphics();// border + Graphics g = this.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, brainDispWidth, brainDispWidth); g.setColor(Color.black); @@ -125,4 +157,56 @@ public class BrainPicture extends JPanel { organ.drawOnBrainPicture(frog, this); // each organ draw itself } + public float getScale() { + return scale; + } + + public void setScale(float scale) { + this.scale = scale; + } + + public float getxAngle() { + return xAngle; + } + + public void setxAngle(float xAngle) { + this.xAngle = xAngle; + } + + public float getyAngle() { + return yAngle; + } + + public void setyAngle(float yAngle) { + this.yAngle = yAngle; + } + + public float getzAngle() { + return zAngle; + } + + public void setzAngle(float zAngle) { + this.zAngle = zAngle; + } + + public void setColor(Color color) { + this.color = color; + } + + public int getxOffset() { + return xOffset; + } + + public void setxOffset(int xOffset) { + this.xOffset = xOffset; + } + + public int getyOffset() { + return yOffset; + } + + public void setyOffset(int yOffset) { + this.yOffset = yOffset; + } + } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java new file mode 100644 index 0000000..8b40e2b --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Cuboid.java @@ -0,0 +1,89 @@ +/* + * Copyright 2018 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.github.drinkjava2.frog.brain; + +import java.io.Serializable; + +import com.github.drinkjava2.frog.Env; + +/** + * Cuboid represents a rectangular prism zone in brain + * + * @author Yong Zhu + * @since 1.0 + */ +public class Cuboid implements Serializable { + private static final long serialVersionUID = 1L; + + public float x; + public float y; + public float z; + public float xr;// xr为这个矩形体x边长的一半 + public float yr;// yr为这个矩形体y边长的一半 + public float zr;// zr为这个矩形体z边长的一半 + + public Cuboid() { + // 空构造器不能省 + } + + public Cuboid(float x, float y, float z, float r) {// 用x,y,z,r来构造 + this.x = x; + this.y = y; + this.z = z; + this.xr = r; + this.yr = r; + this.zr = r; + if (this.x < 0) + this.x = 0; + if (this.y < 0) + this.y = 0; + if (this.x > Env.FROG_BRAIN_RADIUS) + this.x = Env.FROG_BRAIN_RADIUS; + if (this.y > Env.FROG_BRAIN_RADIUS) + this.y = Env.FROG_BRAIN_RADIUS; + if (this.z > Env.FROG_BRAIN_RADIUS) + this.z = Env.FROG_BRAIN_RADIUS; + } + + public Cuboid(float x, float y, float z, float xr, float yr, float zr) {// 用x,y,z,r来构造 + this.x = x; + this.y = y; + this.z = z; + this.xr = xr; + this.yr = yr; + this.zr = zr; + } + + public Cuboid(Cuboid c) {// 用另一个Cube来构造 + this.x = c.x; + this.y = c.y; + this.z = c.z; + this.xr = c.xr; + this.yr = c.yr; + this.zr = c.zr; + } + + public static void copyXYZ(Cuboid from, Cuboid to) { + to.x = from.x; + to.y = from.y; + to.z = from.z; + } + + public static void copyXYZR(Cuboid from, Cuboid to) { + to.x = from.x; + to.y = from.y; + to.z = from.z; + to.xr = from.xr; + to.xr = from.xr; + to.xr = from.xr; + } + +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java new file mode 100644 index 0000000..92e00f7 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/MouseAction.java @@ -0,0 +1,99 @@ +package com.github.drinkjava2.frog.brain; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; + +/** + * BrainPicture show first frog's brain structure, for debug purpose only + * + * @author Yong Zhu + * @since 1.0 + */ +public class MouseAction implements MouseListener, MouseWheelListener, MouseMotionListener { + private BrainPicture brainPic; + private int buttonPressed = 0; + private int x; + private int y; + + public MouseAction(BrainPicture brainPic) { + this.brainPic = brainPic; + } + + @Override + public void mousePressed(MouseEvent e) { + if (e.getButton() == 1) + buttonPressed = 1; + else if (e.getButton() == 2) + buttonPressed = 2; + else + buttonPressed = 0; + x = e.getPoint().x; + y = e.getPoint().y; + } + + @Override + public void mouseReleased(MouseEvent e) { + buttonPressed = 0; + } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + if (e.getWheelRotation() < 0) + brainPic.scale *= 1.1; + else + brainPic.scale /= 1.1; + } + + @Override + public void mouseDragged(MouseEvent e) {// do nothing + if (buttonPressed == 1) { + if (e.getX() > x && e.getY() > y) + brainPic.zAngle -= .00f; + else if (e.getX() < x && e.getY() < y) + brainPic.zAngle += .00f; + else { + if (e.getX() > x) + brainPic.yAngle += .02f; + if (e.getX() < x) + brainPic.yAngle -= .02f; + if (e.getY() > y) + brainPic.xAngle -= .02f; + if (e.getY() < y) + brainPic.xAngle += .02f; + } + x = e.getX(); + y = e.getY(); + } + if (buttonPressed == 2) { + if (e.getX() > x) + brainPic.xOffset++; + if (e.getX() < x) + brainPic.xOffset--; + if (e.getY() > y) + brainPic.yOffset++; + if (e.getY() < y) + brainPic.yOffset--; + x = e.getX(); + y = e.getY(); + } + } + + @Override + public void mouseClicked(MouseEvent e) {// do nothing + } + + @Override + public void mouseEntered(MouseEvent e) {// do nothing + } + + @Override + public void mouseExited(MouseEvent e) {// do nothing + } + + @Override + public void mouseMoved(MouseEvent e) { // do nothing + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java index 657ca17..41147c7 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Organ.java @@ -18,36 +18,26 @@ import com.github.drinkjava2.frog.Frog; /** * Organ is a part of frog, organ can be saved in egg * - * 器官是脑的一部分,多个器官在脑内可以允许重叠出现在同一位置。 + * 器官是脑的一部分,多个器官在脑内可以允许重叠出现在同一位置,器官有小概率随机生成,但大多数时候是稳定的。 有些器官会负 * * @author Yong Zhu * @since 1.0.4 */ -public class Organ extends Zone { +public class Organ extends Cuboid { private static final long serialVersionUID = 1L; - public String name; // 显示在脑图上的器官名称,可选 public long fat = 0; // 如果活跃多,fat值高,则保留(及变异)的可能性大,反之则很可能丢弃掉 - // public float organWasteEnergy = 0.05f; // - // 器官在每个测试循环中需要消耗青蛙多少能量,可以通过调节这个参数抑制器官数量无限增长 - public float organActiveEnergy = 1; // 输出器官激活需要消耗每个脑细胞多少能量 - public float organOutputEnergy = 2; // 感觉器官激活会给每个脑细胞增加多少能量 public boolean initilized; // 通过这个标记判断是否需要手工给定它的参数初值 public boolean allowBorrow() { // 是否允许在精子中将这个器官借出 return false; } - /** Each loop step call active method, Child class can override this method */ - public void active(Frog f) { // 每一步都会调用器官的active方法 ,缺省啥也不干 + /** Only call once when frog created , Child class can override this method */ + public void initFrog(Frog f) { // 仅在Frog生成时这个方法会调用一次,缺省啥也不干,通常用于在这一步播种脑细胞 } - /** If active in this organ's zone? */ - - /** Set X, Y, Radius, name of current Organ */ - public Organ setXYRN(float x, float y, float r, String name) { - this.setXYR(x, y, r); - this.name = name; - return this; + /** Each loop step call active method, Child class can override this method */ + public void active(Frog f) { // 每一步都会调用器官的active方法 ,缺省啥也不干 } /** Child class can override this method to drawing picture */ @@ -55,13 +45,7 @@ public class Organ extends Zone { if (!Application.SHOW_FIRST_FROG_BRAIN) return; pic.setColor(Color.BLACK); // 缺省是黑色 - pic.drawZone(this); - if (this.name != null) - pic.drawText(this, String.valueOf(this.name)); - } - - /** Only call once when frog created , Child class can override this method */ - public void initFrog(Frog f) { // 仅在Frog生成时这个方法会调用一次,缺省啥也不干,通常用于Group子类的初始化 + pic.drawCube(this); } /** Only call once after organ be created by new() method */ @@ -72,8 +56,7 @@ public class Organ extends Zone { } catch (Exception e) { throw new UnknownError("Can not make new Organ copy for " + this); } - copyXYR(this, newOrgan); - newOrgan.name = this.name; + copyXYZR(this, newOrgan); newOrgan.fat = this.fat; return new Organ[] { newOrgan }; } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java new file mode 100644 index 0000000..1e72fea --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Point.java @@ -0,0 +1,30 @@ +/* + * Copyright 2018 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.github.drinkjava2.frog.brain; + +/** + * Point has x, y, z value + * + * @author Yong Zhu + * @since 2.0.2 + */ +@SuppressWarnings("all") +public class Point { + public float x; + public float y; + public float z; + + public Point(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Zone.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/Zone.java deleted file mode 100644 index d8ae70e..0000000 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/Zone.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2018 the original author or authors. - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by - * applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS - * OF ANY KIND, either express or implied. See the License for the specific - * language governing permissions and limitations under the License. - */ -package com.github.drinkjava2.frog.brain; - -import java.io.Serializable; - -import com.github.drinkjava2.frog.Env; - -/** - * Zone represents a rectangle zone in brain - * - * @author Yong Zhu - * @since 1.0 - */ -public class Zone implements Serializable { - private static final long serialVersionUID = 1L; - - public float x; - public float y; - public float r;// r为这个矩形区边长的一半 - - public Zone() { - // 空构造器不能省 - } - - public Zone(float x, float y, float r) {// 用x,y,r来构造 - this.x = x; - this.y = y; - this.r = r; - if (this.x < 0) - this.x = 0; - if (this.y < 0) - this.y = 0; - if (this.x > Env.FROG_BRAIN_WIDTH) - this.x = Env.FROG_BRAIN_WIDTH; - if (this.y > Env.FROG_BRAIN_WIDTH) - this.y = Env.FROG_BRAIN_WIDTH; - } - - public Zone(Zone z) {// 用另一个Zone来构造 - this.x = z.x; - this.y = z.y; - this.r = z.r; - } - - public boolean nearby(Zone z) { - float dist = r + z.r; - return (Math.abs(x - z.x) < dist && Math.abs(y - z.y) < dist); - } - - public int roundX() { - return Math.round(x); - } - - public int roundY() { - return Math.round(y); - } - - public static void copyXY(Zone from, Zone to) { - to.x = from.x; - to.y = from.y; - } - - public static void copyXYR(Zone from, Zone to) { - to.x = from.x; - to.y = from.y; - to.r = from.r; - } - - public void setXYR(float x, float y, float r) { - this.x = x; - this.y = y; - this.r = r; - } - -} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/group/Group.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/group/Group.java deleted file mode 100644 index a26f7ec..0000000 --- a/core3d/src/main/java/com/github/drinkjava2/frog/brain/group/Group.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2018 the original author or authors. - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by - * applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS - * OF ANY KIND, either express or implied. See the License for the specific - * language governing permissions and limitations under the License. - */ -package com.github.drinkjava2.frog.brain.group; - -import com.github.drinkjava2.frog.brain.Organ; - -/** - * Group presents a rectangle organ zone, Group only arranges a group of cells - * - * 在2.0之前,CellGroup是线型,不具备可进化性。在2.0之后引入Group概念,Group是矩形,大小可以覆盖整个大脑,也可以小到单个细胞。可以进行位置、大小变动、复制、分裂等多种形式的变异 - * - * 为了与旧版CellGroup区分,新版的这个类命名为Group,它代表了一组分布于一个正方形内的细胞群,细胞数量、每个细胞的触突连接方式等参数由当前Group决定, - * 可以说每一种Group代表了一种神经网络算法, 通过无数个Group随机的分布、进化、变异,达到最终脑结构适应环境的变化 - * Group会参与遗传和进化,但是它生成的细胞不会参与遗传。 各个Group生成的细胞相加总和就是脑细胞总数。 - * - * Group在脑活动中不起作用,可以把Group比作播种机,把种子排列好后,就撒手不管了,在遗传过程中有一个fat参数,如果细胞活跃多,则Group保留及变异的可能性大,反之则舍弃掉。 - * Group是器官的一种,所以蛋里存放着所有Group的位置、大小、内部参数等信息,但是蛋里面不保存具体的细胞。这样通过控制有多少个"播种机",就可以控制大脑的结构了,这样可以缩小蛋的尺寸。 - * 原则上Group遗传的一下代与父代是同一个播种(算法)类型,但不排除也有可能突变成另一个类型的Group。 - * - * @author Yong Zhu - * @since 1.0 - */ -public abstract class Group extends Organ { - private static final long serialVersionUID = 1L; - -} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java new file mode 100644 index 0000000..69ff0e9 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/BrainFrame.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.github.drinkjava2.frog.brain.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.brain.Organ; + +/** + * BrainFrame one used to drawing the brain frame in BrainPicture + * + * @author Yong Zhu + */ +public class BrainFrame extends Organ { + private static final long serialVersionUID = 1L; + + public BrainFrame() { + x = 0; + y = 0; + z = 0; + xr = Env.FROG_BRAIN_RADIUS / 2; + yr = xr; + zr = xr; + } + + public boolean allowBorrow() { // 是否允许在精子中将这个器官借出 + return false; + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java new file mode 100644 index 0000000..3f1b926 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/brain/organ/PictureEye.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.github.drinkjava2.frog.brain.organ; + +import com.github.drinkjava2.frog.Env; +import com.github.drinkjava2.frog.brain.Organ; + +/** + * PictureEye can only see the picture in env + * + * @author Yong Zhu + */ +public class PictureEye extends Organ { + private static final long serialVersionUID = 1L; + + public PictureEye() { + x = 0; + y = 0; + z = Env.FROG_BRAIN_RADIUS / 2 - .5f; + xr = Env.FROG_BRAIN_RADIUS / 4; + yr = xr; + zr = 0.5f; + } + + public boolean allowBorrow() { // 是否允许在精子中将这个器官借出 + return false; + } +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java b/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java index 544bc71..1dc2407 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/egg/Egg.java @@ -16,12 +16,13 @@ import java.util.List; import com.github.drinkjava2.frog.Frog; import com.github.drinkjava2.frog.brain.Organ; -import com.github.drinkjava2.frog.brain.group.Group; +import com.github.drinkjava2.frog.brain.organ.BrainFrame; +import com.github.drinkjava2.frog.brain.organ.PictureEye; import com.github.drinkjava2.frog.util.RandomUtils; /** - * Egg is the static structure description of frog, can save as text file, to - * build a frog, first need build a egg.
+ * Egg is the static structure description of frog, can save as file, to build a + * frog, first need build a egg.
* * 蛋存在的目的是为了以最小的字节数串行化存储Frog,它是Frog的生成算法描述,而不是Frog本身,这样一来Frog就不能"永生"了,因为每一个egg都不等同于 * 它的母体, 而且每一次测试,大部分条件反射的建立都必须从头开始训练,类似于人类,无论人类社会有多聪明, 婴儿始终是一张白纸,需要花大量的时间从头学习。 @@ -30,19 +31,15 @@ import com.github.drinkjava2.frog.util.RandomUtils; * @since 1.0 */ public class Egg implements Serializable { - // 为了缩短时间,这个程序随机生成的联结将只落在固定的器官上而不是漫天撒网(见4.12提交),这是程序的优化,实现的逻辑和随机漫天撒网定是相同的。 - // 但是这个优化带来的问题是这是一个硬编码逻辑,不利于器官的优胜劣汰, 而且下面这个 FIXED_ORGAN_QTY必须每次手工设定,以后需要重构这块的代码 public static int FIXED_ORGAN_QTY = 11; private static final long serialVersionUID = 1L; public List organs = new ArrayList<>(); - public List groups = new ArrayList<>(); - public Egg() {// 无中生有,创建一个蛋,先有蛋,后有蛙 - - + organs.add(new BrainFrame()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 + organs.add(new PictureEye()); // BrainCube是固有的,用来画一个脑的立方,什么都不做 } /** Create egg from frog */ diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/egg/EggTool.java b/core3d/src/main/java/com/github/drinkjava2/frog/egg/EggTool.java index a96d539..58681ee 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/egg/EggTool.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/egg/EggTool.java @@ -52,8 +52,9 @@ public class EggTool { ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(Env.eggs); so.close(); + System.out.print("Fist frog has " + first.organs.size() + " organs, energy=" + first.energy); System.out.println(", Last frog has " + last.organs.size() + " organs, energy=" + last.energy); - System.out.println("Saved "+Env.eggs.size() +" eggs to file '" + Application.CLASSPATH + "eggs.ser'"); + System.out.println("Saved " + Env.eggs.size() + " eggs to file '" + Application.CLASSPATH + "eggs3d.ser'"); } catch (IOException e) { System.out.println(e); } diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java index ca66cc7..bf9a1c6 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Material.java @@ -26,10 +26,10 @@ public class Material { public static final byte SEESAW_BASE = 1; // 1~9 is invisible to frog public static final byte FOOD = VISIBLE + 1; - public static final byte SEESAW = VISIBLE + 2; // if <0 will not cause frog die + public static final byte SEESAW = VISIBLE + 2; - public static final byte BRICK = KILLFROG + 1; - public static final byte TRAP = KILLFROG + 2; + public static final byte BRICK = KILLFROG + 1;// brick will kill frog + public static final byte TRAP = KILLFROG + 2; // trap will kill frog public static Color color(byte material) { if (material == TRAP) diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java new file mode 100644 index 0000000..d4f9bb3 --- /dev/null +++ b/core3d/src/main/java/com/github/drinkjava2/frog/objects/Pictures.java @@ -0,0 +1,39 @@ +/* Copyright 2018-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.github.drinkjava2.frog.objects; + +/** + * Object means some thing in Env + * + * @author Yong Zhu + * @since 1.0 + */ +public class Pictures implements Object { + + @Override + public void build() { + // TODO Auto-generated method stub + + } + + @Override + public void destory() { + // TODO Auto-generated method stub + + } + + @Override + public void active(int screen) { + + } + + +} diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/objects/SeeSaw.java b/core3d/src/main/java/com/github/drinkjava2/frog/objects/SeeSaw.java index 5f220ae..18ece1c 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/objects/SeeSaw.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/objects/SeeSaw.java @@ -40,7 +40,7 @@ public class SeeSaw implements Object { @Override public void destory() { - + // do nothing } @Override diff --git a/core3d/src/main/java/com/github/drinkjava2/frog/util/RandomUtils.java b/core3d/src/main/java/com/github/drinkjava2/frog/util/RandomUtils.java index 6c85556..48518a2 100644 --- a/core3d/src/main/java/com/github/drinkjava2/frog/util/RandomUtils.java +++ b/core3d/src/main/java/com/github/drinkjava2/frog/util/RandomUtils.java @@ -12,10 +12,6 @@ package com.github.drinkjava2.frog.util; import java.util.Random; -import com.github.drinkjava2.frog.Frog; -import com.github.drinkjava2.frog.brain.Zone; -import com.github.drinkjava2.frog.egg.Egg; - /** * Random Utilities used in this project * @@ -32,28 +28,7 @@ public class RandomUtils { public static float nextFloat() { return rand.nextFloat(); } - - /** Return a random zone inside of a zone */ - public static Zone randomZoneInZone(Zone z) { // 在一个区内随机取一个小小区 - return new Zone(z.x - z.r + z.r * 2 * rand.nextFloat(), z.y - z.r + z.r * 2 * rand.nextFloat(), - z.r * rand.nextFloat() * .04f); - } - - /** Return a random zone inside of frog's random organ */ - public static Zone randomPosInAnyFrogOrgan(Frog f) { - if (f.organs == null || f.organs.size() == 0) - throw new IllegalArgumentException("Can not call randomPosInRandomOrgan method when frog has no organ"); - return randomZoneInZone(f.organs.get(RandomUtils.nextInt(Egg.FIXED_ORGAN_QTY))); - } - - /** Return a random zone inside of frog's random organ */ - public static Zone randomPosMostInNewEye(Frog f) { - if (f.organs == null || f.organs.size() == 0) - throw new IllegalArgumentException("Can not call randomPosInRandomOrgan method when frog has no organ"); - if (RandomUtils.percent(95)) - return randomZoneInZone(f.organs.get(7));// 这是一个硬编码,大部分新联接建立在newEye中 - return randomZoneInZone(f.organs.get(RandomUtils.nextInt(Egg.FIXED_ORGAN_QTY))); - } + public static boolean percent(float percent) { return rand.nextFloat() * 100 < percent; diff --git a/eggs3d.ser b/eggs3d.ser deleted file mode 100644 index 8efa45ade5622ec8089eff4acce57bb55db83aec..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1145 zcmZ4UmVvdnh(S0ju`E%qv?Mb}&#|Z|vC=2AxTK=-lI+amiF2757(E$SiZiQHD+(AG z7$nPCKqOF;QgVK-UV3IpMro2>N>OHBHpmboy|kkIbiLH{bUoMfbf8KQU}R!o@?l_0 zFUl`1C@x_T@ByjR2U({NvdJ1KmtT~gm{(lLP{+VrQ2?gFI>DAPRA3ii!Y+)Vaa0}l RfEo>#(QpCfxB*B96#&!}!{`72