大概意思就是:
这个程序类似于生命游戏。这游戏有三种情况,比如;
牛 草 空。
如果格子是 空的,
邻居有一个是草,那么在下一次就成为草。如果不是,那么还是草。
如果格子是 牛,
如果牛 过多(邻居在大于等于4个以上) 或者 饥饿(四周没有任何草),下一次都死光。
如果格子是 草
邻居相邻一个牛的话,那么下一次就成为牛。
后面这个老师还给了我们要用probalility的,我看不懂,那块不用上也没关系,只想求主要的怎么做。副本在后面
Write a program to simulate a population of grazers and food in a simple 2D world, similar
to Conway’s classic Game of Life. For the sake of this assignment, food and grazers are both creatures.
This is an example of using cellular automata.
Each tick of the clock, the next state of the cell will be determined by its current state and the states
of its neighbours, according to the following rules:
1. If the cell is EMPTY:
If it is adjacent to at least one Food, it will become Food next turn; Otherwise it will stay as it was.
2. If the cell is a Grazer:
If the Grazer is overcrowded or starved then it will die next turn (and the cell will become empty);
Otherwise it will stay as it was.
3. If the cell is a Food:
If it is next to a Grazer then it will become a Grazer next turn; Otherwise it will stay the same
Overcrowded means there aremore than 3 adjacent cells of the same kind as this one.
Starved means, for a predator, that there is no food in any adjacent cell.
We will represent food with dots ‘.’ and grazers with ‘@’.
The program must be called LifeAndDeath and must implement the Livable interface provided.
NOTE: the LifeAndDeath class does not have to implement the Livable interface. TheWorld class must
implement the Livable interface. Do not add any methods to the Livable interface. You may add any
other methods you like to your program though.
The program must have the class LifeAndDeath.java which contains the main method, and another
class file called World.java which implements the Livable interface. You must put all your own
code (not the Livable interface) into a package called assignment2.
The World class must have a constructor that takes as its arguments the size of the grid, the probability
(as a double) of each cell initially containing food, and the probability (as a double) of each cell
initially containing a grazer, like this:
1 public World (int size , double probFood , double probGrazer ) {
2 // ... fill in this
3 }
The programmust be able to be run as follows:
java LifeAndDeath 25 0.1 0.2 100
Which would create a 25£25 grid of cells, and each cell would have a probability of 0.1 of being a
grazer or of 0.2 of being food (so probability of 0.7 of being empty). The program should run with the
above arguments for 100 ticks of the clock or until everything dies.
The LifeAndDeath program should print out the grid as above with each grid on a new line (not
side by side as I showed in the last part of the example).
If the program is given another argument, the string “summary” as the last String, then it should
only print out the total number of creatures after each tick of the clock, not the whole grid.
这是interface:
/**
*
*/
package constraints;
/**
* @author mac
*/
public interface Livable {
public char getNextCellState(int x, int y);
/**
* TODO Calculate the next state of the cell at position x, y
*
* according to the rules below:
* "lonely" means no neighbours with the same type
* "overcrowded" means at least 4 neighbours of the same type
* A Grazer is "starved" when none of its neighbours are Food
*
* If the cell is EMPTY then
* If it is adjacent to at least one Food,
* it will become Food next turn
* Otherwise it will stay as it was.
*
* If the cell is a Grazer then
* If the Grazer is overcrowded or starved
* then it will die next turn.
* Otherwise it will stay as it was.
*
* If the cell is a Food then
* If it is next to a Grazer
* then it will become a Grazer next turn
* Otherwise it will stay the same
*
*/
public char getContents(int i, int j);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return the char at this cell, representing whether it is empty
* or contains a grazer or food.
*/
public boolean isValidCoordinate(int x, int y);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return true if and only if the (x,y) coordinates fit in the world
*/
public int getNumNeighboursWithType(char type, int i, int j);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return the number of neighbours of the cell at position (i,j)
* which are of the type given.
*/
public int getNumCreatures();
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* count and return the number of creatures in the world
*/
public boolean isEmpty();
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Return true if and only if the complete world is empty
*/
public void setContents(int i, int j, char ch);
/**
* TODO YOU MUST IMPLEMENT THIS METHOD
*
* Set the contents of the cell (i,j) to be the character ch
* if the coordinates are valid. If they are not,
* throw an IndexOutOfBoundsException.
*/
}