继续之前的内容
新增
- HatchEgg中添加vNumber()方法用来查看单个Pokemon是几V,统计0V~6V宝可梦数量
- test1()方法添加形参N,用来设定孵蛋数量。并输出孵蛋统计信息。
package pokemonHatch;
import java.util.HashSet;
import java.util.Set;
import pokemonHatch.Pokemon;
public class HatchEgg {
//不变之石 暂定
//private boolean isStone;
private Pokemon maleParent;
private Pokemon femaleParent;
private static Pokemon offspring;
private int[] osattribute=new int[6];
private static int[] osvNumber=new int[7];
/**
* 构造器需要传入父本和母本
* @param p1 父本宝可梦
* @param p2 母本宝可梦
*/
public HatchEgg(Pokemon p1,Pokemon p2) {
//当传入的宝可梦对象为空时 抛出空指针异常 并附加提示
if(p1==null||p2==null) {
throw new NullPointerException("It takes two Pokemons to hatch eggs");
}
maleParent=p1;
femaleParent=p2;
}
/**
* 随机选择六项属性中的N项
* @param N
* @param attribute
* @return
*/
public int[] selector(int N,int[] attribute) {
Set<Integer> set=new HashSet<Integer>();
while(true){
set.add((int)(Math.random()*6));
if(set.size()==N)
break;
}
for(Integer value: set){
attribute[(int)value]=generator();
}
return attribute;
}
/**
* 随机个体生成器 生成一个 0~31 之间的数值
* @return
*/
public int generator() {
return (int)(Math.random()*32);}
/**
* 遗传六项来自父母本的个体值
* @param attribute
* @return
*/
public int[] heredity(int[] attribute) {
for(int i=0;i<6;i++) {
if(tf()==true)
attribute[i]=maleParent.getAttribute()[i];
else
attribute[i]=femaleParent.getAttribute()[i];
}
return attribute;
}
/**
* 通过这个方法确定继承哪一方的个体
* @return
*/
private boolean tf() {
int i=(int)(Math.random()*2);
if(i==1) {
return true;
}
return false;
}
/**
* 带红绳的孵蛋方式
* 1 先通过 heredity()遗传全部六项个体
* 2 通过selector() 使其中的随机一项随机
*
*/
public Pokemon hatchWithRope(){
offspring=new Pokemon(selector(1,heredity(osattribute)));
return offspring;
}
/**
* 不带红绳的孵蛋方式
* 1 先通过heredity()遗传全部六项个体
* 2 通过selector() 使其中的随机三项随机
*
*/
public Pokemon hatchNotWithRope() {
offspring=new Pokemon(selector(3,heredity(osattribute)));
return offspring;
}
/**
* 判断宝可梦是几V
* @return
*/
public static int vNumber(Pokemon p) {
int count=0;
for(int i:p.attribute) {
if(i==31)
count++;
}
switch(count) {
case 0:osvNumber[0]++;break;
case 1:osvNumber[1]++;break;
case 2:osvNumber[2]++;break;
case 3:osvNumber[3]++;break;
case 4:osvNumber[4]++;break;
case 5:osvNumber[5]++;break;
case 6:osvNumber[6]++;break;
}
return count;
}
//遗传5 项
public static void test1(int N) {
if(N<=0) {
System.out.println("警告:请输入一个正确的孵蛋次数");
return;
}
int count=1;
//父母本个体值
int[] attribute1= {
31,31,31,24,31,31};
int[] attribute2= {
31,31,31,22,31,31};
Pokemon p1=new Pokemon(attribute1);
Pokemon p2=new Pokemon(attribute2);
HatchEgg h=new HatchEgg(p1, p2);
System.out.println("子代列表");
for(int i=0;i<N;i++) {
System.out.print(count++);
h.hatchWithRope();
//System.out.print(h.hatchWithRope());
//System.out.print(" " +h.hatchWithRope().vNumber()+"V");
System.out.print(" " +offspring);
System.out.print(" " +vNumber(offspring)+"V");
System.out.println();
}
//输出父母本个体值表
System.out.println("--------------------------------------------------------------");
System.out.println("父本宝可梦:"+p1);
System.out.println("母本宝可梦:"+p2);
System.out.println("孵蛋次数:"+N+"次");
//统计N v个数
for(int i=0;i<7;i++) {
System.out.println(i+"V个数为:"+osvNumber[i]+" 概率:"+((float)osvNumber[i]/N)*100+"%");
}
}
//遗传3项
public static void test2() {
int count=1;
//父母本个体值
int[] attribute1= {
31,31,31,31,31,0};
int[] attribute2= {
0,31,31,31,31,31};
Pokemon p1=new Pokemon(attribute1);
Pokemon p2=new Pokemon(attribute2);
HatchEgg h=new HatchEgg(p1, p2);
for(int i=0;i<100;i++) {
System.out.print(count++);
System.out.println(h.hatchNotWithRope());
}
}
public static void main(String[] args) {
test1(100);
//test2();
}
}
本次博客未完成,下一步将test1 进一步函数化,添加不变之石(性格遗传)
package pokemonHatch;
import java.util.HashSet;
import java.util.Set;
import pokemonHatch.Pokemon;
public class HatchEgg {
//不变之石 暂定
//private boolean isStone;
private Pokemon maleParent;
private Pokemon femaleParent;
private static Pokemon offspring;
//子代个体列表
private int[] osattribute=new int[6];
//子代统计
private static int[] osvNumber=new int[7];
//性格列表
private static String[] characterList={
"勤奋","怕寂寞","固执","顽皮","勇敢","大胆","坦率","淘气","乐天","悠闲","内敛","慢吞吞",
"害羞","马虎","冷静","温和","温顺","慎重","浮躁","自大","胆小","急躁","爽朗","天真","认真"
};
//子代性格
private static String osCharacter;
/**
* 构造器需要传入父本和母本
* @param p1 父本宝可梦
* @param p2 母本宝可梦
*/
public HatchEgg(Pokemon p1,Pokemon p2) {
//当传入的宝可梦对象为空时 抛出空指针异常 并附加提示
if(p1==null||p2==null) {
throw new NullPointerException("It takes two Pokemons to hatch eggs");
}
maleParent=p1;
femaleParent=p2;
}
/**
* 随机选择六项属性中的N项
* @param N
* @param attribute
* @return
*/
public int[] selector(int N,int[] attribute) {
Set<Integer> set=new HashSet<Integer>();
while(true){
set.add((int)(Math.random()*6));
if(set.size()==N)
break;
}
for(Integer value: set){
attribute[(int)value]=generator();
}
return attribute;
}
/**
* 随机个体生成器 生成一个 0~31 之间的数值
* @return
*/
public int generator() {
return (int)(Math.random()*32);}
/**
* 遗传六项来自父母本的个体值
* @param attribute
* @return
*/
public int[] heredity(int[] attribute) {
for(int i=0;i<6;i++) {
if(tf()==true)
attribute[i]=maleParent.getAttribute()[i];
else
attribute[i]=femaleParent.getAttribute()[i];
}
return attribute;
}
/**
* 通过这个方法确定继承哪一方的个体
* @return
*/
private boolean tf() {
int i=(int)(Math.random()*2);
if(i==1) {
return true;
}
return false;
}
/**
* 带红绳的孵蛋方式
* 1 先通过 heredity()遗传全部六项个体
* 2 通过selector() 使其中的随机一项随机
*
*/
public Pokemon hatchWithRope(){
offspring=new Pokemon(selector(1,heredity(osattribute)),osCharacter);
return offspring;
}
/**
* 不带红绳的孵蛋方式
* 1 先通过heredity()遗传全部六项个体
* 2 通过selector() 使其中的随机三项随机
*
*/
public Pokemon hatchNotWithRope() {
offspring=new Pokemon(selector(3,heredity(osattribute)),osCharacter);
return offspring;
}
/**
* 判断宝可梦是几V
* @return
*/
public static int vNumber(Pokemon p) {
int count=0;
for(int i:p.attribute) {
if(i==31)
count++;
}
switch(count) {
case 0:osvNumber[0]++;break;
case 1:osvNumber[1]++;break;
case 2:osvNumber[2]++;break;
case 3:osvNumber[3]++;break;
case 4:osvNumber[4]++;break;
case 5:osvNumber[5]++;break;
case 6:osvNumber[6]++;break;
}
return count;
}
/**
*
* @param isRope 是否用红绳
* @param isStone 是否用不变之石
* @param number 孵蛋数量
*/
public void HatchMultipleEgg(boolean isRope,boolean isStone,int number) {
if(number<=0) {
System.out.println("警告:请输入一个正确的孵蛋次数");
return;
}
int count=1;
//System.out.println();
for(int i=0;i<number;i++) {
System.out.print(count++);
if(isStone==true) {
//暂定这种随机性格的继承方式
if(tf()==true) {
osCharacter=maleParent.character;
}else {
osCharacter=femaleParent.character;
}
}else {
//否则随机一种性格
int cNumber=(int)(Math.random()*25);
osCharacter=characterList[cNumber];
}
if(isRope==true) {
hatchWithRope();
}else {
hatchNotWithRope();
}
System.out.print(" " +offspring);
System.out.print(" " +vNumber(offspring)+"V");
System.out.println();
}
//输出父母本个体值表
System.out.println("--------------------------------------------------------------");
System.out.println("父本宝可梦:"+maleParent);
System.out.println("母本宝可梦:"+femaleParent);
System.out.println("孵蛋次数:"+number+"次");
//统计N v个数
for(int i=0;i<7;i++) {
System.out.println(i+"V个数为:"+osvNumber[i]+" 概率:"+((float)osvNumber[i]/number)*100+"%");
}
}
public static void main(String[] args) {
//父母本个体值
int[] attribute1= {
31,31,31,24,31,31};
int[] attribute2= {
31,31,31,22,31,31};
Pokemon p1=new Pokemon(attribute1,"固执");
Pokemon p2=new Pokemon(attribute2,"内敛");
HatchEgg h=new HatchEgg(p1, p2);
h.HatchMultipleEgg(true,false,100);
}
}
package pokemonHatch;
public class Pokemon {
//个体值表对应 hp(生命) atk(攻击) def(防御) satk(特攻) sdef(特防) speed(速度)
protected int[] attribute;
//性格
protected String character;
public String getCharacter() {
return character;
}
public void setCharacter(String character) {
this.character = character;
}
public int[] getAttribute() {
return attribute;
}
public void setAttribute(int[] attribute) {
this.attribute = attribute;
}
public Pokemon(int[] attribute,String character) {
this.attribute = attribute;
this.character = character;
}
/**
* 输出宝可梦各项个体值
*/
@Override
public String toString() {
return "Pokemon [hp:"+attribute[0]+
",atk:"+attribute[1]+
",def:"+attribute[2]+
",satk:"+attribute[3]+
",sdef:"+attribute[4]+
",speed:"+attribute[5]+"]"
+ " character:"+character;
}
}
下一步将不变之石和红绳加入进宝可梦携带物。加宝可梦种类遗传,特性遗传。
QQ:872742416