关于Pokemon精灵孵蛋系统的模拟(一)

今天心血来潮想着模拟一下宝可梦中孵蛋,并用java实现一下,简单写了一个小程序。

需要考虑的要点

  • 每只精灵只有一个携带物
  • 不变之石锁定性格
  • 红绳增加遗传项
  • 考虑父本母本
  • 考虑生出子代雌雄概率
  • 6项数值个体0~31变化

将上述规则中的部分进行考虑,携带物暂时不作为Pokemon类的属性,不变之石因为直接可以观察咋找你事不做模拟,考虑带红绳和不带红绳两种方式的遗传。
下面是代码和包结构。
在这里插入图片描述

package pokemonHatch;

public class Pokemon {
     

	//个体值表对应  hp(生命)   atk(攻击)   def(防御)  satk(特攻)  sdef(特防)  speed(速度)
	private int[] attribute;
	
	public int[] getAttribute() {
     
		return attribute;
	}

	public void setAttribute(int[] attribute) {
     
		this.attribute = attribute;
	}

	public Pokemon(int[] attribute) {
     
		this.attribute = attribute;
	}

	/**
	 * 输出宝可梦各项个体值
	 */
	@Override
	public String toString() {
     
		
		return "Pokemon [hp:"+attribute[0]+
				",atk:"+attribute[1]+
				",def:"+attribute[2]+
				",satk:"+attribute[3]+
				",sdef:"+attribute[4]+
				",speed:"+attribute[5]+"]";
	}
	
	/**
	 * 判断宝可梦是几V
	 * @return
	 */
	public int vNumber() {
     
		
		int count=0;
		
		for(int i:attribute) {
     
			if(i==31)
				count++;
		}
		
		return count;
	}
	
	
}

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 Pokemon offspring;
	
	private int[] osattribute=new int[6];
	
	/**
	 * 构造器需要传入父本和母本
	 * @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;
	}
	
	
	
	//遗传5 项  
	public static void test1() {
     
		int count=1;
		//父母本个体值
		int[] attribute1= {
     31,31,0,31,0,0};
		int[] attribute2= {
     31,0,31,0,31,31};
		
		Pokemon p1=new Pokemon(attribute1);
		Pokemon p2=new Pokemon(attribute2);
		HatchEgg h=new HatchEgg(p1, p2);
		for(int i=0;i<10;i++) {
     
			System.out.print(count++);
			System.out.println(h.hatchWithRope());
		}
	}
	
	//遗传3项
	public static void test2() {
     
		int count=1;
		//父母本个体值
		int[] attribute1= {
     31,31,0,31,0,0};
		int[] attribute2= {
     31,0,31,0,31,31};
		
		Pokemon p1=new Pokemon(attribute1);
		Pokemon p2=new Pokemon(attribute2);
		HatchEgg h=new HatchEgg(p1, p2);
		for(int i=0;i<10;i++) {
     
			System.out.print(count++);
			System.out.println(h.hatchNotWithRope());
		}
	}
	
	public static void main(String[] args) {
     
		
		//test1();
		test2();
		
	}
}

带红绳遗传模拟结果(test1())

1Pokemon [hp:31,atk:0,def:31,satk:31,sdef:0,speed:31]
2Pokemon [hp:31,atk:0,def:31,satk:12,sdef:31,speed:31]
3Pokemon [hp:17,atk:0,def:0,satk:31,sdef:0,speed:31]
4Pokemon [hp:31,atk:0,def:31,satk:28,sdef:0,speed:31]
5Pokemon [hp:31,atk:31,def:0,satk:0,sdef:22,speed:31]
6Pokemon [hp:31,atk:31,def:18,satk:31,sdef:0,speed:0]
7Pokemon [hp:31,atk:0,def:31,satk:0,sdef:31,speed:10]
8Pokemon [hp:31,atk:0,def:8,satk:0,sdef:31,speed:31]
9Pokemon [hp:31,atk:0,def:31,satk:31,sdef:0,speed:0]
10Pokemon [hp:5,atk:31,def:0,satk:0,sdef:31,speed:0]

不带红绳遗传模拟结果(test2())

1Pokemon [hp:28,atk:0,def:0,satk:27,sdef:31,speed:31]
2Pokemon [hp:31,atk:0,def:28,satk:7,sdef:1,speed:0]
3Pokemon [hp:18,atk:0,def:3,satk:16,sdef:31,speed:0]
4Pokemon [hp:23,atk:31,def:0,satk:31,sdef:0,speed:13]
5Pokemon [hp:31,atk:29,def:0,satk:3,sdef:5,speed:0]
6Pokemon [hp:31,atk:0,def:23,satk:9,sdef:0,speed:12]
7Pokemon [hp:31,atk:27,def:28,satk:31,sdef:29,speed:0]
8Pokemon [hp:31,atk:25,def:0,satk:8,sdef:31,speed:0]
9Pokemon [hp:7,atk:0,def:0,satk:0,sdef:25,speed:9]
10Pokemon [hp:21,atk:26,def:31,satk:30,sdef:0,speed:31]

刚玩没几天,秀一下我的对性格5V小火龙

暂时编写这么多功能,未来有空继续添加一些特性,继续完善。
QQ:872742416

你可能感兴趣的:(宝可梦)