Compare commits
2 Commits
f78952b37d
...
d3e1ce6eb6
| Author | SHA1 | Date |
|---|---|---|
|
|
d3e1ce6eb6 | |
|
|
918d76cec8 |
|
|
@ -0,0 +1,32 @@
|
|||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Cow extends Agent {
|
||||
int timer;
|
||||
Cow(int x,int y){
|
||||
super(x,y,Color.magenta);
|
||||
timer = 0;
|
||||
}
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors,Simulator world) {
|
||||
if(timer>25) {
|
||||
build(x,y,world);
|
||||
timer = 0;
|
||||
}
|
||||
timer ++;
|
||||
return true;
|
||||
}
|
||||
private void build(int x,int y, Simulator world) {
|
||||
world.setCell((x-1),y-1,1);
|
||||
world.setCell((x), y-1,1);
|
||||
world.setCell((x+1), y-1, 1);
|
||||
world.setCell((x+1), y, 1);
|
||||
world.setCell((x), y+1, 1);
|
||||
}
|
||||
public String toString() {
|
||||
return("2");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -36,7 +36,7 @@ public class Sheep extends Agent {
|
|||
hunger++;
|
||||
}
|
||||
this.moveRandom();
|
||||
return hunger>10;
|
||||
return hunger<10;
|
||||
}
|
||||
|
||||
private void moveRandom() {
|
||||
|
|
@ -53,6 +53,7 @@ public class Sheep extends Agent {
|
|||
if(direction == 3) {
|
||||
y-=1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -22,9 +22,11 @@ public class Simulator extends Thread {
|
|||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
private boolean clickActionFlag;
|
||||
private int clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
|
||||
World currentWorld = new World(getWidth(),getHeight());
|
||||
|
||||
//TODO : add missing attribute(s)
|
||||
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
|
|
@ -32,7 +34,7 @@ public class Simulator extends Thread {
|
|||
stopFlag=false;
|
||||
pauseFlag=false;
|
||||
loopingBorder=false;
|
||||
clickActionFlag=false;
|
||||
clickActionFlag=0;
|
||||
|
||||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
|
|
@ -119,7 +121,26 @@ public class Simulator extends Thread {
|
|||
* and the count is in the birth list,
|
||||
* then the cell becomes alive
|
||||
*/
|
||||
ArrayList<String> frame = new ArrayList<String>();
|
||||
String frameLine = "";
|
||||
for (int i = 0; i<getWidth(); i++) {
|
||||
frameLine = "";
|
||||
for(int j = 0; j<getHeight(); j++) {
|
||||
if(getCell(j,i)==0 && fieldBirthValues.contains(currentWorld.getLiving(j,i,LIFE_AREA_RADIUS, loopingBorder))) {
|
||||
frameLine +=("1;");
|
||||
}
|
||||
else if (getCell(j,i)==1 && fieldSurviveValues.contains(currentWorld.getLiving(j,i,LIFE_AREA_RADIUS, loopingBorder))) {
|
||||
frameLine += ("1;");
|
||||
}
|
||||
else {
|
||||
frameLine +=("0;");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
frame.add(frameLine);
|
||||
}
|
||||
loadSaveState(frame);
|
||||
|
||||
|
||||
|
||||
|
|
@ -136,7 +157,13 @@ public class Simulator extends Thread {
|
|||
* method called when clicking pause button
|
||||
*/
|
||||
public void togglePause() {
|
||||
// TODO : actually toggle the corresponding flag
|
||||
// TODO : actually toggle the corresponding flag (done)
|
||||
//pauseFlag = true;
|
||||
if (pauseFlag) {
|
||||
pauseFlag = false;
|
||||
} else {
|
||||
pauseFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -144,6 +171,25 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
//TODO : complete method
|
||||
//Called when clicking on the GUI at the coordinates provided as arguments
|
||||
//Behaviour will vary depending on the clickActionFlagValue flag:
|
||||
// Cell case (default) : toggles the state of the cell at coordinates x,y.
|
||||
//Agent case : create an Agent (Sheep provided for example) at provided
|
||||
//coordinates.
|
||||
// You might want to implement more behaviors for the click depending on
|
||||
//different values of the flag (its type can be changed to tolerate more values)
|
||||
if(clickActionFlag == 0) {
|
||||
if(getCell(x,y)==0) {
|
||||
setCell(x,y,1);
|
||||
}else {
|
||||
setCell(x,y,0);
|
||||
}
|
||||
}else if(clickActionFlag == 1) {
|
||||
agents.add(new Cow(x,y));
|
||||
}
|
||||
else if(clickActionFlag ==2) {
|
||||
agents.add(new Sheep(x,y));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -154,7 +200,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public int getCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
return 0;
|
||||
return currentWorld.getValue(x,y);
|
||||
}
|
||||
/**
|
||||
*
|
||||
|
|
@ -189,6 +235,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
//TODO : complete method
|
||||
currentWorld.setValue(x,y,val);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -198,7 +245,15 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public ArrayList<String> getSaveState() {
|
||||
//TODO : complete method with proper return
|
||||
return null;
|
||||
ArrayList<String> outputSavestate = new ArrayList<String>();
|
||||
for(int i = 0; i <getWidth(); i++) {
|
||||
String lineSavestate = "";
|
||||
for(int j = 0; j<getHeight(); j++) {
|
||||
lineSavestate +=(getCell(i,j)+";");
|
||||
}
|
||||
outputSavestate.add(lineSavestate);
|
||||
}
|
||||
return outputSavestate;
|
||||
}
|
||||
/**
|
||||
*
|
||||
|
|
@ -249,24 +304,35 @@ public class Simulator extends Thread {
|
|||
* maybe just make a constructor in there
|
||||
* and use it here
|
||||
*/
|
||||
currentWorld.randomizer(chanceOfLife);
|
||||
}
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
//TODO : complete method with proper return
|
||||
return false;
|
||||
return loopingBorder;
|
||||
}
|
||||
|
||||
public void toggleLoopingBorder() {
|
||||
//TODO : complete method
|
||||
|
||||
if(loopingBorder) {
|
||||
loopingBorder = false;
|
||||
}else {
|
||||
loopingBorder = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void setLoopDelay(int delay) {
|
||||
//TODO : complete method
|
||||
loopDelay = delay;
|
||||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
//TODO : complete method
|
||||
if(clickActionFlag == 2) {
|
||||
clickActionFlag = 0;
|
||||
}else {
|
||||
clickActionFlag ++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -279,8 +345,18 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public ArrayList<String> getRule() {
|
||||
//TODO : complete method with proper return
|
||||
|
||||
return null;
|
||||
ArrayList<String> outputRule = new ArrayList<String>();
|
||||
String rule = "";
|
||||
for (int i:fieldBirthValues) {
|
||||
rule += (i+";");
|
||||
}
|
||||
outputRule.add(rule);
|
||||
rule = "";
|
||||
for (int i:fieldSurviveValues) {
|
||||
rule += (i+";");
|
||||
}
|
||||
outputRule.add(rule);
|
||||
return outputRule;
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
|
|
@ -298,26 +374,45 @@ public class Simulator extends Thread {
|
|||
String elem = surviveElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
//TODO : add value to possible survive values
|
||||
|
||||
fieldSurviveValues.add(value);
|
||||
}
|
||||
String[] birthElements = birthLine.split(";");
|
||||
for(int x=0; x<birthElements.length;x++) {
|
||||
String elem = birthElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
//TODO : add value to possible birth values
|
||||
|
||||
fieldBirthValues.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
//TODO : Same idea as the other save method, but for agents
|
||||
return null;
|
||||
ArrayList<String> agentSave = new ArrayList<String>();
|
||||
for(Agent i:getAnimals()) {
|
||||
agentSave.add(i.x+";"+i.y+";"+i.toString());
|
||||
|
||||
}
|
||||
return agentSave;
|
||||
}
|
||||
|
||||
public void loadAgents(ArrayList<String> stringArray) {
|
||||
//TODO : Same idea as other load methods, but for agent list
|
||||
for(int i = 0; i<stringArray.size() ;i++) {
|
||||
String agentLine = stringArray.get(i);
|
||||
String[] agentElements = agentLine.split(";");
|
||||
int[] agentValues = new int[3];
|
||||
for(int j = 0; j<agentElements.length;j++) {
|
||||
agentValues[j] = Integer.parseInt(agentElements[j]);
|
||||
}
|
||||
if(agentValues[2] == 1) {
|
||||
agents.add(new Sheep(agentValues[0],agentValues[1]));
|
||||
}
|
||||
if(agentValues[2]==2) {
|
||||
agents.add(new Cow(agentValues[0],agentValues[1]));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* used by label in interface to show the active click action
|
||||
|
|
@ -326,7 +421,15 @@ public class Simulator extends Thread {
|
|||
public String clickActionName() {
|
||||
// TODO : initially return "sheep" or "cell"
|
||||
// depending on clickActionFlag
|
||||
return "";
|
||||
if(clickActionFlag == 1) {
|
||||
return "engineer";
|
||||
}
|
||||
else if (clickActionFlag == 0) {
|
||||
return "cell";
|
||||
}
|
||||
else {
|
||||
return "sheep";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
package backend;
|
||||
|
||||
public class Teleporter {
|
||||
private int size = 0;
|
||||
public Teleporter(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
public int position(int value, boolean loop) {
|
||||
int out = 0;
|
||||
if(loop) {
|
||||
if(value>=0) {
|
||||
out = value;
|
||||
if(out>=size) {
|
||||
out = out - size;
|
||||
}
|
||||
}
|
||||
else {
|
||||
out = size + value;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if( value>=0) {
|
||||
out = value;
|
||||
if(out>=size) {
|
||||
out = -99;
|
||||
}
|
||||
}
|
||||
else {
|
||||
out =-99;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package backend;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class World {
|
||||
Random randGen = new Random();
|
||||
Teleporter teleportX;
|
||||
Teleporter teleportY;
|
||||
private int[][] currentWorld;
|
||||
|
||||
public World(int width, int height) {
|
||||
currentWorld = new int[width][height];
|
||||
this.teleportX = new Teleporter(width);
|
||||
this.teleportY = new Teleporter(height);
|
||||
|
||||
}
|
||||
public void randomizer(double randValue) {
|
||||
for(int i=0; i< currentWorld.length; i++) {
|
||||
for(int j =0; j< currentWorld[i].length; j++) {
|
||||
if(randGen.nextFloat()<randValue) {
|
||||
setValue(i,j,1);
|
||||
}
|
||||
else {
|
||||
setValue(i,j,0);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public int getValue(int x, int y) {
|
||||
if (x == -99 || y == -99) {
|
||||
return 0;
|
||||
}
|
||||
return currentWorld[x][y];
|
||||
}
|
||||
public void setValue(int x, int y, int value) {
|
||||
currentWorld[x][y] = value;
|
||||
}
|
||||
public int getLiving(int x, int y, int radius, boolean loop) {
|
||||
int out = 0;
|
||||
for (int i = x-radius; i<=x+radius; i++) {
|
||||
for(int j = y-radius; j<=y+radius; j++) {
|
||||
if(getValue(teleportX.position(i,loop),teleportY.position(j,loop))==1) {
|
||||
if(j!=y || x!=i) {
|
||||
out ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue