diff --git a/history/008_frog3d_shape/src/main/java/com/gitee/drinkjava2/frog/judge/BrainShapeJudge.java b/history/008_frog3d_shape/src/main/java/com/gitee/drinkjava2/frog/judge/BrainShapeJudge.java new file mode 100644 index 0000000..1620423 --- /dev/null +++ b/history/008_frog3d_shape/src/main/java/com/gitee/drinkjava2/frog/judge/BrainShapeJudge.java @@ -0,0 +1,72 @@ +package com.gitee.drinkjava2.frog.judge; + +import java.awt.Font; +import java.util.ArrayList; +import java.util.List; + +import com.gitee.drinkjava2.frog.Animal; +import com.gitee.drinkjava2.frog.Env; +import com.gitee.drinkjava2.frog.brain.BrainPicture; +import com.gitee.drinkjava2.frog.brain.Cell; +import com.gitee.drinkjava2.frog.util.Point3D; +import com.gitee.drinkjava2.frog.util.StringPixelUtils; + +/** + * execute method be called after animal's initAnimal method + * + * 这个类的judge方法在动物的初始化后被调用,根据脑细胞群的三维结构形状来对动物进行奖罚,即加减它的能量值,这是一个临时类,只是用来检验细胞三维成形功能,以后可能改名或删除 + * 这个类的show方法在绘脑图时调用,在脑图里显示脑细胞群的三维形状,用空心圆来表示,这个三维形状就像是一个模子,细胞长在这个模子里的有奖,否则扣分 + */ +public class BrainShapeJudge {//NOSONAR + private static Point3D C = new Point3D(Env.BRAIN_XSIZE / 2, Env.BRAIN_YSIZE / 2, Env.BRAIN_ZSIZE / 2); + private static boolean[][][] shape = new boolean[Env.BRAIN_XSIZE][Env.BRAIN_YSIZE][Env.BRAIN_ZSIZE]; + private static List pointList = new ArrayList<>(); //pointList存放上面shape的所有有效点,用来加快显示循环而不用遍历三维数组 + static { + putPixiel("FROG"); + } + + private static void putPixiel(String str) { + byte[][] c = StringPixelUtils.getStringPixels(Font.SANS_SERIF, Font.PLAIN, 12, str); //要把frog二维像素变成立体的三维点放到points里和pointsList里供使用 + int w = c.length; + int h = c[0].length; + for (int z = 0; z < 5; z++) { + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + if (c[x][y] > 0) { + Point3D p = new Point3D(C.x + x, C.y + y, C.z + z); + if (!Animal.outBrainRange(p.x, p.y, p.z)) { + shape[p.x][p.y][p.z] = true; + pointList.add(p); + } + } + } + } + } + + for (int z = 0; z < 2; z++) + for (int x = 0; x < 33; x++) { //再划一条线把字连起来,目前细胞不能隔空分裂,只能在紧邻位置分裂 + Point3D p = new Point3D(C.x + x, C.y, C.z + z); + if (!Animal.outBrainRange(p.x, p.y, p.z)) { + shape[p.x][p.y][p.z] = true; + pointList.add(p); + } + } + + } + + public static void judge(Animal animal) {//检查animal的脑细胞是否位于brainShape的范围内 + for (Cell c : animal.cells) { + if (shape[c.x][c.y][c.z]) { + animal.normalAward(); + } else { + animal.normalPenalty(); + } + } + } + + public static void show(BrainPicture pic) {// 在脑图上显示当前形状 + for (Point3D p : pointList) + pic.drawCircle(p.x, p.y, p.z, 1); + } + +}