?? simulation.java
字號:
import java.util.*;
public class Simulation {
private static final int MAX_SUNLIGHT = 10;
public static final String IMAGE = "image";
public static final String COLOR = "color";
private final int firstRow;
private final int firstColumn;
private final int lastRow;
private final int lastColumn;
private Random rand;
private Vector livingBeings;
private int time;
public Simulation(int initialFirstRow, int initialFirstColumn,
int initialLastRow, int initialLastColumn) {
time = 0;
firstRow = initialFirstRow;
firstColumn = initialFirstColumn;
lastRow = initialLastRow;
lastColumn = initialLastColumn;
livingBeings = new Vector(50);
rand = new Random(7L);
time = 0;
}
public int getFirstRow() {
return firstRow;
}
public int getFirstColumn() {
return firstColumn;
}
public int getLastRow() {
return lastRow;
}
public int getLastColumn() {
return lastColumn;
}
public int getSunlight(int row, int column) {
if(row >= getFirstRow() && row <= getLastRow()
&& column >= getFirstColumn()
&& column <= getLastColumn())
return rand.nextInt(10);
else
return 0;
}
public Random getRand() {
return rand;
}
public int getTime() {
return time;
}
public void addLivingBeing(LivingBeing newLivingBeing) {
if(newLivingBeing == null)
return;
if(livingBeings.contains(newLivingBeing)) {
return;
} else {
livingBeings.add(newLivingBeing);
}
}
public Vector getNeighbors(int row, int column, int distance) {
Vector neighbors = new Vector();
for(int beingIndex = 0; beingIndex < livingBeings.size(); beingIndex++){
LivingBeing being = (LivingBeing)livingBeings.get(beingIndex);
if(being != null && being.getRow() <= row + distance
&& being.getRow() >= row - distance
&& being.getColumn() <= column + distance
&& being.getColumn() >= column - distance) {
neighbors.add(being);
}
}
return neighbors;
}
public void simulateATimeBlock() {
time++;
for(int beingIndex = 0; beingIndex < livingBeings.size(); beingIndex++) {
LivingBeing being = (LivingBeing)livingBeings.get(beingIndex);
if(being != null && !being.isDead())
being.liveALittle();
}
for(int beingIndex = 0; beingIndex < livingBeings.size();) {
LivingBeing being = (LivingBeing)livingBeings.get(beingIndex);
if(being.isDead())
livingBeings.remove(being);
else
beingIndex++;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -