小弟在做一个java的作业,要求实现一个模拟器,在m*n个格子patch中有白蚁termite,捕食者predator,和木头woodchip.模拟器随时间的变化,产生新的白蚁,比如说产生的概率为0.1,平均10秒内有一只新的白蚁出生,随机的分配到格子patch里面。
木头,白蚁,和捕食者都随机的生成在某个格子patch里面,白蚁可以朝上下左右4个方向随机的移动,1次能移动一个格子,到达格子边缘后不能越界,碰到木头后,如果白蚁没有拿着木头,就把木头捡起来,如果白蚁拿着木头,就把拿着的木头扔掉,捕食者和白蚁的移动情况一样,只能朝4个方向随机移动一个格子,如果目的地格子内有白蚁,就把白蚁吃掉。要求是每个格子,patch 都必须是1个对象,而且每个patch都得有个参数指向它周围的参数(each patch instance should have an attribute that is a list of neighbouring patches)。整个过程要求用swing画出文本界面或者图形表示。空的格子用“.”来表示;每个格子里如果有带着木头的白蚁(或者还有没带木头的白蚁),就用"*"表示,如果白蚁没有带木头,就用“X”表示,如果只有木头,用木头的数量表示,例如有2根木头,就用“2”表示,如果有捕食者,就用“#”表示。我的问题是定义了类Patch,怎样在里面设置list? 还有,如何在一个绘图界面里可以刷新输出一段字符,如:
X.....1...#
.....3..***
1.....*....
我的Patch类定义如下:
import java.util.*;
public class Patch {
private int woodchipnumber;
private int carryingwoodchiptermitenumber;
private int totaltermitenumber;
private static int patchx;
private static int patchy;
private int patchsizem;
private int patchsizen;
private int predatornumber;
private static int empty;
private int visible;
private List<Patch> list;public Patch(int x, int y){
this.patchx=x;
this.patchy=y;
//this.empty=empty;
//this.visible=0;
}
public int getpatchX(){
return patchx;
}
public int getpatchY(){
return patchy;
}
public int setpatchX(int i){
patchx=i;
return patchx;
}
public int setpatchY(int i){
patchy=i;
return patchy;
}
public int getwoodchipnumber(){
return woodchipnumber;
}
public int getcarryingwoodchiptermitenumber(){
return carryingwoodchiptermitenumber;
}
public int gettotaltermitenumber(){
return totaltermitenumber;
}
public int stecarryingwoodchiptermitenumber(int i){
carryingwoodchiptermitenumber+=i;
return carryingwoodchiptermitenumber;
}
public int stetotaltermitenumber(int i){
totaltermitenumber+=i;
return totaltermitenumber;
}
public int getpredatornumber(){
return predatornumber;
}
public int setpatchsizem(int m){
patchsizem=m;
return patchsizem;
}
public int setpatchsizen(int n){
patchsizem=n;
return patchsizen;
}
public int getEmpty(){
return empty;
}
public int setExmpty(int i){
empty=i;
return empty;
}
//List<Patch>list=new LinkedList<Patch>();
ArrayList<Patch>patch=new ArrayList<Patch>();
public Patch findnext(List<Patch>list){
ListIterator<Patch> it=list.listIterator();
Patch next=null;
while(it.hasNext()){
next=it.next();
}
return next;
}
public static Patch findprevious(List<Patch>list)
{ ListIterator<Patch> it=list.listIterator();
Patch previous=null;
if(it.previous()!=null)
{previous= it.previous();
}
return previous;
}public int checkpatch(int x,int y){
ListIterator<Patch> it=list.listIterator();
//int visible=0;
while(it.hasNext()){
Patch p=it.next();
it.add(p);
if((x==p.getpatchX())&&(y==p.getpatchY())){
visible=1;
}else
{visible=0;}
break;
}
return visible;
}
}
现在都不知道该怎么办好了。哪位老大能帮忙看看,给指点下,小弟不胜感激!
木头,白蚁,和捕食者都随机的生成在某个格子patch里面,白蚁可以朝上下左右4个方向随机的移动,1次能移动一个格子,到达格子边缘后不能越界,碰到木头后,如果白蚁没有拿着木头,就把木头捡起来,如果白蚁拿着木头,就把拿着的木头扔掉,捕食者和白蚁的移动情况一样,只能朝4个方向随机移动一个格子,如果目的地格子内有白蚁,就把白蚁吃掉。要求是每个格子,patch 都必须是1个对象,而且每个patch都得有个参数指向它周围的参数(each patch instance should have an attribute that is a list of neighbouring patches)。整个过程要求用swing画出文本界面或者图形表示。空的格子用“.”来表示;每个格子里如果有带着木头的白蚁(或者还有没带木头的白蚁),就用"*"表示,如果白蚁没有带木头,就用“X”表示,如果只有木头,用木头的数量表示,例如有2根木头,就用“2”表示,如果有捕食者,就用“#”表示。我的问题是定义了类Patch,怎样在里面设置list? 还有,如何在一个绘图界面里可以刷新输出一段字符,如:
X.....1...#
.....3..***
1.....*....
我的Patch类定义如下:
import java.util.*;
public class Patch {
private int woodchipnumber;
private int carryingwoodchiptermitenumber;
private int totaltermitenumber;
private static int patchx;
private static int patchy;
private int patchsizem;
private int patchsizen;
private int predatornumber;
private static int empty;
private int visible;
private List<Patch> list;public Patch(int x, int y){
this.patchx=x;
this.patchy=y;
//this.empty=empty;
//this.visible=0;
}
public int getpatchX(){
return patchx;
}
public int getpatchY(){
return patchy;
}
public int setpatchX(int i){
patchx=i;
return patchx;
}
public int setpatchY(int i){
patchy=i;
return patchy;
}
public int getwoodchipnumber(){
return woodchipnumber;
}
public int getcarryingwoodchiptermitenumber(){
return carryingwoodchiptermitenumber;
}
public int gettotaltermitenumber(){
return totaltermitenumber;
}
public int stecarryingwoodchiptermitenumber(int i){
carryingwoodchiptermitenumber+=i;
return carryingwoodchiptermitenumber;
}
public int stetotaltermitenumber(int i){
totaltermitenumber+=i;
return totaltermitenumber;
}
public int getpredatornumber(){
return predatornumber;
}
public int setpatchsizem(int m){
patchsizem=m;
return patchsizem;
}
public int setpatchsizen(int n){
patchsizem=n;
return patchsizen;
}
public int getEmpty(){
return empty;
}
public int setExmpty(int i){
empty=i;
return empty;
}
//List<Patch>list=new LinkedList<Patch>();
ArrayList<Patch>patch=new ArrayList<Patch>();
public Patch findnext(List<Patch>list){
ListIterator<Patch> it=list.listIterator();
Patch next=null;
while(it.hasNext()){
next=it.next();
}
return next;
}
public static Patch findprevious(List<Patch>list)
{ ListIterator<Patch> it=list.listIterator();
Patch previous=null;
if(it.previous()!=null)
{previous= it.previous();
}
return previous;
}public int checkpatch(int x,int y){
ListIterator<Patch> it=list.listIterator();
//int visible=0;
while(it.hasNext()){
Patch p=it.next();
it.add(p);
if((x==p.getpatchX())&&(y==p.getpatchY())){
visible=1;
}else
{visible=0;}
break;
}
return visible;
}
}
现在都不知道该怎么办好了。哪位老大能帮忙看看,给指点下,小弟不胜感激!
1. Introduction
The aim of this assignment is to design, implement and test a Java simulator that could be
used for artificial life research. Specifically, an application is required that will allow
investigation of the global behaviour that emerges in an environment where simulated termites
and predators act according to simple local rules. The main tasks for this project are:
1. To design an object oriented model of the structure and the behaviour of the simulation
using UML diagrams;
2. To implement your design in Java.
You should aim to spend between 10 and 15 hours working on this assignment.
2. Requirements
Your simulator will maintain an environment, which consists of a collection of patches arranged
in a rectangular grid (Figure 1). Each patch contains zero or more wood chips. A patch may be
occupied by one or more termites or predators, which are mobile entities that live within the
world and behave according to simple rules.
A termite can pick up a wood chip from the patch that it is currently on, or drop a wood chip
that it is carrying. Termites travel around the grid by moving randomly from their current
patch to a neighbouring patch, in one of four possible directions (Figure 2). Termites can only
move within the boundary of the environment. New termites may hatch from eggs, and this is
simulated by the appearance of a new termite at a random patch within the environment.
A predator moves in a similar way to termites, and if a predator moves onto a patch that is
occupied by a termite, then the predator eats the termite.
To initialise a simulation, the user specifies the following parameters either on the command
line, or in a simple text file which is read by the program:
The size of the environment, which is an (n × m) integer number of patches.
The number of predators and wood chips.
The initial number of termites.
The probability that a new termite will hatch from an egg at each timestep or iteration
of the model. Thus if this value is 0.1, then on average we would expect a new termite
to hatch every 10 timesteps.
Richard Clayton - 2 – Last updated 15-Nov-07
X.1......
.....1..*
..*......
.1.....2.
1....1...
.X.......
...1..X..
Figure 3: text
display of the
environment
shown in
Figure 1.
At initialization, the termites, predators, and wood chips are distributed randomly in the
environment. Simulation then proceeds in a loop, in which the new state of the environment is
obtained at each iteration. Typically, several thousand iterations will be required before
interesting behaviour emerges. The new state of the environment is obtained by updating each
termite and predator in turn (for simplicity, we assume that the behaviour of the termites is
serialized in this implementation rather than using a separate thread for each termite).
When each termite is updated, it obeys the following behavioural rules:
1. Move in a random direction to another patch.
2. If the termite enters a patch containing any wood chips and the termite is not carrying
a chip, it picks up the chip.
3. If the termite enters a patch containing any wood chips and the termite is already
carrying a chip, it drops the chip.
When each predator is updated, it obeys the following behavioural rules:
1. Move in a random direction to another patch.
2. If the destination patch contains a termite, then the termite is eaten and if the termite
is carrying a wood chip then the wood chip remains on the patch.
At each iteration, new termites may hatch, and the probability of a new termite hatching in
each iteration is a specified parameter. If a new termite does hatch, then it is placed on a
randomly chosen patch.
The state of the grid is observed on a display. It should be possible
for the user to choose whether the display is a simple text-based
output on the command widow or terminal, or is graphical. A
graphical display could take a number of forms, e.g. an image in
which the state of each patch in the grid is mapped to pixel colour,
or an image in which each termite and wood chip is drawn. In
general, for each patch, the display could illustrate whether it
contains any termites, whether any of those termites is carrying a
wood chip, and the number of wood chips on the patch. Figure 3
shows a text display corresponding to the environment shown in
Figure 1, in which the number of wood chips is shown as an
integer, termites carrying wood chips are ed with a ‘*’ and
termites carrying nothing are ed with an ‘X’. Empty patches
are ed with a dot. You can see that since only one character can be displayed in a cell,
the ‘highest ranking’ information for a cell is displayed, e.g. if a cell contains termites carrying
a wood chip and termites not carrying a wood chip and wood chips, the only thing displayed is
an *.
Certain constraints have been placed on the design of the software. Because at some future
stage large simulations may be run on a multiple processor machine, and because the notion
of a patch may be extended to include time-varying state (such as the amount of food present
on a patch), it is a requirement that each patch should be implemented as a separate object.
(Hint: each patch instance should have an attribute that is a list of neighbouring patches.)
Hence, termites and predators must select a random direction in which to move, and should
then interrogate the patch on which they are currently located in order to determine whether it
has a neighbour in the required direction. (Note: You are not being asked to run your system
on a multiple processor machine, nor include time-varying state, e.g. food decaying. You just
have to meet the constraints specified.)
As part of your design, you need to consider how to support the ability to change the kind of
display used to show the simulation, e.g. as a text display or a graphical display using Swing
components. (Hint: An interface may be appropriate here.) You should also consider whether
you can abstract common behaviour of termites and predators.
3. Tasks
1. Develop an object-oriented design for the termite simulator that separates out the
control of the simulation from the display of the world;
Richard Clayton - 3 – Last updated 15-Nov-07
2. Model your design using UML class diagrams to show the classes and their
relationships.
3. Implement your UML design in Java. As noted above, you should implement both a text
display and a graphical display using Swing components. (Note: If you elect to only
implement a text display, you will not be able to receive full s.)
4. Test the behaviour of each class.
5. Answer the following questions:
a. Run a simulation with an environment containing 400 patches, 30 wood chips,
10 termites, 0 predators and 0 probability of termite hatch. What behaviour do
you expect? What behaviour do you see?
b. What happens if you then introduce 1, 2, and 5 predators?
c. What happens if you then increase the hatch probability to 0.1, 0.2, and 0.5?
d. Using the same environment, run simulations with 10 wood chips and 30
termites. Do you get the same behaviour? Why or why not?
先把需要更新的文本放在一个String数组里,再单开个线程,在run方法中每隔一定的时间去从数组中取下一个文本,然后更新界面