java程序之模拟人工生态环境
以前也贴过这些代码,由于world.java的代码,所以没有给出顶层类,
这次给出了predatorprey.java
但是整个程序还存在一个问题,world.java中的 eden()函数(文中有红色**号标记的部分),
在编译的时候给出错误 :不匹配的类型 imcompatible types
//PredatorPrey.java - top level class
class PredatorPrey {
public static void main(String[] args) {
World odd = new World(10), even = new World(10);
int i, cycles = 10;
even.eden(10); //generate initial World
System.out.println(even); //print initial state
for (i = 0; i < cycles; i++) {
System.out.println("Cycle = " + i + "\n\n");
if (i % 2 == 1) {
even.update(odd);
System.out.println(even);
}
else {
odd.update(even);
System.out.println(odd);
}
}
}
}
<------------------------------------------------------------------->
//World.java - square grid of life form cells
import tio.*;
class World {
World(int n) {
size = n; cells = new Living[n][n];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
cells<i>[j] =new Empty(i,j);
}
public void clearNeighborCounts() {
Fox.neighborCount.set(0);
Rabbit.neighborCount.set(0);
Grass.neighborCount.set(0);
Empty.neighborCount.set(0);
}
public void eden(int size){
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
char c = (char)Console.in.readChar();
while (c != 'F' && c != 'R'&& c != 'G'&& c != '.')
c = (char)Console.in.readChar();
**
cells<i> [j] = c;
}
}
public String toString() {
String t = "";
for (int i = 0; i < cells.length; i++)
for (int j = 0; j < cells<i>.length; j++) {
if ( (j + 1) % 10 == 0)
t = t + "\n" + cells<i>[j];
else
t = t + cells<i>[j];
}
return t;
}
public void update(World oldWorld) {
//borders are taboo
for (int i = 1; i < size - 1; i++)
for (int j = 1; j < size - 1; j++)
cells<i>[j] =
oldWorld.cells<i>[j].next(oldWorld);
}
Living[][] cells;
private int size ; //set in constructor
}
<------------------------------------------------------------------>
//Living.java - the superclass for all life forms
abstract class Living {
abstract Count getCount();
abstract Living next(World world);
abstract char toChar(); // character for this form
void computeNeighbors(World world) {
world.clearNeighborCounts();
world.cells[row][column].getCount().set(-1);
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
world.cells[row+i][column+j].getCount().click();
}
int row, column; //location
}
<-------------------------------------------------------------------->
//Grass.java - something for the rabbits to eat
class Grass extends Living {
public Grass(int r, int c) { row = r; column = c; }
public Living next(World world) {
computeNeighbors(world);
if (Grass.neighborCount.get() >
2 * Rabbit.neighborCount.get())
// rabbits move in to eat the grass
return (new Rabbit(row, column, 0));
else if (Grass.neighborCount.get() >
Rabbit.neighborCount.get())
// grass remains
return (new Grass(row, column));
else
// rabbits eat all the grass
return (new Empty(row, column));
}
public String toString() { return "Grass"; }
char toChar() { return 'G'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
}
<------------------------------------------------------------------>
//Rabbit.java - prey class
class Rabbit extends Living {
Rabbit(int r, int c, int a )
{ row = r; column = c; age = a;}
Living next(World world) {
computeNeighbors(world);
if (Fox.neighborCount.get() >=
Rabbit.neighborCount.get() )
return (new Empty(row, column)); // eat Rabbits
else if (age > LIFE_EXPECTANCY)
return (new Empty(row, column)); // too old
else if (Grass.neighborCount.get() == 0)
return (new Empty(row, column)); // starved
else
return (new Rabbit(row, column, age + 1));
}
public String toString() {return "Rabbit age "+age;}
char toChar() { return 'R'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
private int age;
private final int LIFE_EXPECTANCY = 3;
}
<------------------------------------------------------------------>
//Fox.java - prey class
class Fox extends Living {
Fox(int r, int c, int a )
{ row = r; column = c; age = a; }
Living next(World world) {
computeNeighbors(world);
if (Fox.neighborCount.get() > 5 ) //too many Foxes
return new Empty(row, column);
else if (age > LIFE_EXPECTANCY) //Fox is too old
return new Empty(row, column);
else if (Rabbit.neighborCount.get() == 0)
return new Empty(row, column); // starved
else
return new Fox(row, column, age + 1);
}
public String toString(){ return "Fox age " + age; }
char toChar() { return 'F'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
private int age;
private final int LIFE_EXPECTANCY = 5;
}
这次给出了predatorprey.java
但是整个程序还存在一个问题,world.java中的 eden()函数(文中有红色**号标记的部分),
在编译的时候给出错误 :不匹配的类型 imcompatible types
//PredatorPrey.java - top level class
class PredatorPrey {
public static void main(String[] args) {
World odd = new World(10), even = new World(10);
int i, cycles = 10;
even.eden(10); //generate initial World
System.out.println(even); //print initial state
for (i = 0; i < cycles; i++) {
System.out.println("Cycle = " + i + "\n\n");
if (i % 2 == 1) {
even.update(odd);
System.out.println(even);
}
else {
odd.update(even);
System.out.println(odd);
}
}
}
}
<------------------------------------------------------------------->
//World.java - square grid of life form cells
import tio.*;
class World {
World(int n) {
size = n; cells = new Living[n][n];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
cells<i>[j] =new Empty(i,j);
}
public void clearNeighborCounts() {
Fox.neighborCount.set(0);
Rabbit.neighborCount.set(0);
Grass.neighborCount.set(0);
Empty.neighborCount.set(0);
}
public void eden(int size){
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
char c = (char)Console.in.readChar();
while (c != 'F' && c != 'R'&& c != 'G'&& c != '.')
c = (char)Console.in.readChar();
**
cells<i> [j] = c;
}
}
public String toString() {
String t = "";
for (int i = 0; i < cells.length; i++)
for (int j = 0; j < cells<i>.length; j++) {
if ( (j + 1) % 10 == 0)
t = t + "\n" + cells<i>[j];
else
t = t + cells<i>[j];
}
return t;
}
public void update(World oldWorld) {
//borders are taboo
for (int i = 1; i < size - 1; i++)
for (int j = 1; j < size - 1; j++)
cells<i>[j] =
oldWorld.cells<i>[j].next(oldWorld);
}
Living[][] cells;
private int size ; //set in constructor
}
<------------------------------------------------------------------>
//Living.java - the superclass for all life forms
abstract class Living {
abstract Count getCount();
abstract Living next(World world);
abstract char toChar(); // character for this form
void computeNeighbors(World world) {
world.clearNeighborCounts();
world.cells[row][column].getCount().set(-1);
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
world.cells[row+i][column+j].getCount().click();
}
int row, column; //location
}
<-------------------------------------------------------------------->
//Grass.java - something for the rabbits to eat
class Grass extends Living {
public Grass(int r, int c) { row = r; column = c; }
public Living next(World world) {
computeNeighbors(world);
if (Grass.neighborCount.get() >
2 * Rabbit.neighborCount.get())
// rabbits move in to eat the grass
return (new Rabbit(row, column, 0));
else if (Grass.neighborCount.get() >
Rabbit.neighborCount.get())
// grass remains
return (new Grass(row, column));
else
// rabbits eat all the grass
return (new Empty(row, column));
}
public String toString() { return "Grass"; }
char toChar() { return 'G'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
}
<------------------------------------------------------------------>
//Rabbit.java - prey class
class Rabbit extends Living {
Rabbit(int r, int c, int a )
{ row = r; column = c; age = a;}
Living next(World world) {
computeNeighbors(world);
if (Fox.neighborCount.get() >=
Rabbit.neighborCount.get() )
return (new Empty(row, column)); // eat Rabbits
else if (age > LIFE_EXPECTANCY)
return (new Empty(row, column)); // too old
else if (Grass.neighborCount.get() == 0)
return (new Empty(row, column)); // starved
else
return (new Rabbit(row, column, age + 1));
}
public String toString() {return "Rabbit age "+age;}
char toChar() { return 'R'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
private int age;
private final int LIFE_EXPECTANCY = 3;
}
<------------------------------------------------------------------>
//Fox.java - prey class
class Fox extends Living {
Fox(int r, int c, int a )
{ row = r; column = c; age = a; }
Living next(World world) {
computeNeighbors(world);
if (Fox.neighborCount.get() > 5 ) //too many Foxes
return new Empty(row, column);
else if (age > LIFE_EXPECTANCY) //Fox is too old
return new Empty(row, column);
else if (Rabbit.neighborCount.get() == 0)
return new Empty(row, column); // starved
else
return new Fox(row, column, age + 1);
}
public String toString(){ return "Fox age " + age; }
char toChar() { return 'F'; }
Count getCount() { return neighborCount; }
static Count neighborCount = new Count();
private int age;
private final int LIFE_EXPECTANCY = 5;
}
air_tuyh
2005-04-28 19:16:13
评论:0
阅读:851
引用:0
