java编程例子源码(狐狸与兔子)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

-------------------------------------------- animal package animal;

import java.util.ArrayList;

import field.Location;

public abstract class Animal {

private int ageLimit;

private int breedableAge;

private int age;

private boolean isAlive = true;

public Animal(int ageLimit, int breedableAge) { this.ageLimit = ageLimit;

this.breedableAge = breedableAge;

}

protected int getAge() {

return age;

}

protected double getAgePercent() {

return (double)age/ageLimit;

}

public abstract Animal breed();

public void grow() {

age++;

if ( age >= ageLimit ) {

die();

}

}

public void die() {

isAlive = false;

}

public boolean isAlive() {

return isAlive;

}

public boolean isBreedable() {

return age >= breedableAge;

}

public Location move(Location[] freeAdj) {

Location ret = null;

if ( freeAdj.length>0 && Math.random() < 0.02 ) { ret =

freeAdj[(int)(Math.random()*freeAdj.length)];

}

return ret;

}

@Override

public String toString() {

return ""+age+":"+(isAlive?"live":"dead");

}

public Animal feed(ArrayList neighbour) { return null;

}

protected void longerLife(int inc) {

ageLimit += inc;

}

}

----------------------------------------------- Cell

package cell;

import java.awt.Graphics;

public interface Cell {

void draw(Graphics g, int x, int y, int size);

}

----------------------------------------------- Location package field;

public class Location {

private int row;

private int col;

public Location(int row, int col) {

this.row = row;

this.col = col;

}

public int getRow() { return row; }

public int getCol() { return col; }

}

----------------------------------------------- Fox

package animal;

import java.awt.Color;

import java.awt.Graphics;

import java.util.ArrayList;

import cell.Cell;

public class Fox extends Animal implements Cell { public Fox() {

super(20,4);

}

@Override

public void draw(Graphics g, int x, int y, int size) { int alpha = (int)((1-getAgePercent())*255);

g.setColor(new Color(0, 0, 0,

alpha));//(int)((20-getAge())/20.0*255)));

g.fillRect(x, y, size, size);

}

@Override

public Animal breed() {

Animal ret = null;

if ( isBreedable() && Math.random() < 0.05 ) {

ret = new Fox();

}

return ret;

}

@Override

public String toString() {

return "Fox:"+super.toString();

}

@Override

public Animal feed(ArrayList neighbour) { Animal ret = null;

if ( Math.random()< 0.2 ) {

ret =

neighbour.get((int)(Math.random()*neighbour.size()));

longerLife(2);

}

return ret;

}

}

---------------------------------------------- Rabbit package animal;

import java.awt.Color;

import java.awt.Graphics;

import cell.Cell;

public class Rabbit extends Animal implements Cell { public Rabbit() {

super(10,2);

}

@Override

public void draw(Graphics g, int x, int y, int size) { int alpha = (int)((1-getAgePercent())*255);

g.setColor(new Color(255, 0, 0, alpha));

g.fillRect(x, y, size, size);

}

@Override

public Animal breed() {

Animal ret = null;

if ( isBreedable() && Math.random() < 0.12 ) {

ret = new Rabbit();

}

return ret;

}

@Override

public String toString() {

return "Rabbit:"+super.toString();

}

}

相关文档
最新文档