updated read me, implemented more than 1 type of cell, added rules
This commit is contained in:
parent
99713c7799
commit
f7c1def77c
|
|
@ -1 +1 @@
|
|||
1,10;11,11;9,11;12
|
||||
1,10;11,11;9,11;12
|
||||
|
|
|
|||
|
|
|
@ -0,0 +1,31 @@
|
|||
# Agent Project
|
||||
# Authors: Isabella GOULD-SMITH, Selma LARSSON-RABIAN, Yashveer YADAV
|
||||
|
||||
Introduction:
|
||||
This Agent Project is a Java implementation of Conway's Game of Life, and improves it by introducing agents, rules and other cell types.
|
||||
|
||||
Project Interface Guide:
|
||||
|
||||
- By default, the rule is set to Conway's Game of Life
|
||||
- By default, the border is set to closed
|
||||
- The SAVE WORLD button saves to CSV format the current world, for any cell type
|
||||
- The LOAD WORLD button loades from CSV a saved world, for any cell type
|
||||
- The TOGGLE CLICK button allows the user to cycle through the different cell types AND the different agents
|
||||
- the TOGGLE BORDER button allows the user to close or open the border of the map, which may allow or forbid cells and agents from moving through it. An open border means that each side of the world is connected to the opposite side (i.e. top and bottom sides)
|
||||
- the SPEED SLIDER allows the user to modify the pace of the game.
|
||||
|
||||
Rules:
|
||||
There are different rules that can be loaded into the game change the cell's behaviors, or the types of cells that can be handled, to a maximum of 4 teams. To load them, use the "LOAD RULE" button, and choose a file. If you wish to make or modify a rule, simply create/modify the csv files in the Rules folder. Those rules DO NOT impact the Agents' behaviors. Note that, though initially the rules file apply the same rule to all cells, it is possible to have the colors behave in different ways, demonstrated with the file "3Colors3Rules".
|
||||
|
||||
If you wish to modify/create a rule file, follow these instructions:
|
||||
- each columns describes the rules applied to a color
|
||||
- the first row contains the number of same color neighbors a cell needs to stay alive
|
||||
- the second row contains the number of same color neighbors a non-colored cell needs to come alive
|
||||
|
||||
Agents:
|
||||
Agents are game entities that exist on top of the cells' interactions. Though they may interact with them, they are not directly tied to them. In our version of the project, two agents have been implemented, a sheep and a wolf. Both have to eat to avoid dying, and may reproduce. They work as follows:
|
||||
- Sheep agents can eat cells to replenish their hunger, and reproduce every 5 steps
|
||||
- Wolf agents can eat sheeps, and reproduce every 8 steps
|
||||
|
||||
Though agents may be placed by clicking the board, you may want to load Agnets configuration with the "LOAD AGENTS" button. If you wish to save a configuration, use the SAVR AGENTS button
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
2;3,1;3;5;8,2;3
|
||||
3,3;5;7;,3;6
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
1;3;5;8
|
||||
3;5;7;
|
||||
1;3;5;8
|
||||
3;5;7;
|
||||
|
|
|
|||
|
|
|
@ -1,2 +1,2 @@
|
|||
2;3
|
||||
2;3
|
||||
3
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
2;3,2;3
|
||||
3,3
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
2;3,2;3,2;3
|
||||
3,3,3
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
2;3,2;3,2;3,2;3
|
||||
3,3,3,3
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
2;3
|
||||
3
|
||||
2;3
|
||||
3;6
|
||||
|
|
|
|||
|
|
|
@ -1,12 +1,12 @@
|
|||
import windowInterface.MyInterface;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,161 +1,161 @@
|
|||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class Agent {
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected Color color;
|
||||
protected boolean hasReproducedFlag = false;
|
||||
Random rand;
|
||||
|
||||
protected boolean fertileFlag = false;
|
||||
protected int fertility = 5;
|
||||
protected int maxFertility;
|
||||
|
||||
protected Agent(int x, int y, Color color, int maxFertility) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.color = color;
|
||||
this.maxFertility = maxFertility;
|
||||
rand = new Random();
|
||||
|
||||
if (fertility == maxFertility) {
|
||||
fertileFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
public boolean getHasReproduced() {
|
||||
return hasReproducedFlag;
|
||||
}
|
||||
|
||||
public boolean canReproduce() {
|
||||
return fertileFlag;
|
||||
}
|
||||
|
||||
public void setHasReproduced(boolean flag) {
|
||||
this.hasReproducedFlag = flag;
|
||||
}
|
||||
|
||||
public Color getDisplayColor() {
|
||||
return color;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
public boolean isInArea(int x, int y, int radius, World world, boolean borderLoopFlag) {
|
||||
int width = world.getWidth();
|
||||
int height = world.getHeight();
|
||||
if(borderLoopFlag) {
|
||||
if (x >= width || x < 0 || y >= height || y < 0) {
|
||||
if (x >= width) {
|
||||
x = 0;
|
||||
}
|
||||
else if(x < 0) {
|
||||
x = width-1;
|
||||
}
|
||||
if(y >= height) {
|
||||
y = 0;
|
||||
}
|
||||
else if(y < 0) {
|
||||
y = height-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int diffX = this.x-x;
|
||||
int diffY = this.y-y;
|
||||
int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY));
|
||||
return dist<radius;
|
||||
}
|
||||
|
||||
// Does whatever the agent does during a step
|
||||
// then returns a boolean
|
||||
// if false, agent dies at end of turn
|
||||
// see step function in Simulator
|
||||
|
||||
public void moveRandom(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
int direction = rand.nextInt(4);
|
||||
int width = world.getWidth();
|
||||
int height = world.getHeight();
|
||||
int xNew = x;
|
||||
int yNew = y;
|
||||
if(direction == 0) {
|
||||
xNew = xNew + 1;
|
||||
}
|
||||
if(direction == 1) {
|
||||
yNew = yNew + 1;
|
||||
}
|
||||
if(direction == 2) {
|
||||
xNew = xNew - 1;
|
||||
}
|
||||
if(direction == 3) {
|
||||
yNew = yNew - 1;
|
||||
}
|
||||
boolean agentInNewCell = false;
|
||||
for (Agent agent : neighbors) {
|
||||
if (agent.agentInCell(xNew, yNew)) {
|
||||
agentInNewCell = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!agentInNewCell) {
|
||||
if (xNew < width && xNew >=0 && yNew < height && yNew >= 0) {
|
||||
x = xNew;
|
||||
y = yNew;
|
||||
}
|
||||
else if(borderLoopFlag) {
|
||||
if (xNew >= width) {
|
||||
xNew = 0;
|
||||
}
|
||||
else if(xNew < 0) {
|
||||
xNew = width-1;
|
||||
}
|
||||
if(yNew >= height) {
|
||||
yNew = 0;
|
||||
}
|
||||
else if(yNew < 0) {
|
||||
yNew = height-1;
|
||||
}
|
||||
x = xNew;
|
||||
y = yNew;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean agentInCell(int x, int y) {
|
||||
boolean flag = false;
|
||||
if (this.x == x && this.y == y) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void reproduced() {
|
||||
fertileFlag = false;
|
||||
fertility = 0;
|
||||
this.setHasReproduced(true);
|
||||
}
|
||||
|
||||
public void tickFertility()
|
||||
{
|
||||
if (fertility < maxFertility) {
|
||||
fertility ++;
|
||||
}
|
||||
if (fertility == maxFertility) {
|
||||
fertileFlag = true;
|
||||
}
|
||||
}
|
||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag);
|
||||
|
||||
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class Agent {
|
||||
protected int x;
|
||||
protected int y;
|
||||
protected Color color;
|
||||
protected boolean hasReproducedFlag = false;
|
||||
Random rand;
|
||||
|
||||
protected boolean fertileFlag = false;
|
||||
protected int fertility = 5;
|
||||
protected int maxFertility;
|
||||
|
||||
protected Agent(int x, int y, Color color, int maxFertility) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.color = color;
|
||||
this.maxFertility = maxFertility;
|
||||
rand = new Random();
|
||||
|
||||
if (fertility == maxFertility) {
|
||||
fertileFlag = true;
|
||||
}
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
public boolean getHasReproduced() {
|
||||
return hasReproducedFlag;
|
||||
}
|
||||
|
||||
public boolean canReproduce() {
|
||||
return fertileFlag;
|
||||
}
|
||||
|
||||
public void setHasReproduced(boolean flag) {
|
||||
this.hasReproducedFlag = flag;
|
||||
}
|
||||
|
||||
public Color getDisplayColor() {
|
||||
return color;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
public boolean isInArea(int x, int y, int radius, World world, boolean borderLoopFlag) {
|
||||
int width = world.getWidth();
|
||||
int height = world.getHeight();
|
||||
if(borderLoopFlag) {
|
||||
if (x >= width || x < 0 || y >= height || y < 0) {
|
||||
if (x >= width) {
|
||||
x = 0;
|
||||
}
|
||||
else if(x < 0) {
|
||||
x = width-1;
|
||||
}
|
||||
if(y >= height) {
|
||||
y = 0;
|
||||
}
|
||||
else if(y < 0) {
|
||||
y = height-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int diffX = this.x-x;
|
||||
int diffY = this.y-y;
|
||||
int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY));
|
||||
return dist<radius;
|
||||
}
|
||||
|
||||
// Does whatever the agent does during a step
|
||||
// then returns a boolean
|
||||
// if false, agent dies at end of turn
|
||||
// see step function in Simulator
|
||||
|
||||
public void moveRandom(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
int direction = rand.nextInt(4);
|
||||
int width = world.getWidth();
|
||||
int height = world.getHeight();
|
||||
int xNew = x;
|
||||
int yNew = y;
|
||||
if(direction == 0) {
|
||||
xNew = xNew + 1;
|
||||
}
|
||||
if(direction == 1) {
|
||||
yNew = yNew + 1;
|
||||
}
|
||||
if(direction == 2) {
|
||||
xNew = xNew - 1;
|
||||
}
|
||||
if(direction == 3) {
|
||||
yNew = yNew - 1;
|
||||
}
|
||||
boolean agentInNewCell = false;
|
||||
for (Agent agent : neighbors) {
|
||||
if (agent.agentInCell(xNew, yNew)) {
|
||||
agentInNewCell = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!agentInNewCell) {
|
||||
if (xNew < width && xNew >=0 && yNew < height && yNew >= 0) {
|
||||
x = xNew;
|
||||
y = yNew;
|
||||
}
|
||||
else if(borderLoopFlag) {
|
||||
if (xNew >= width) {
|
||||
xNew = 0;
|
||||
}
|
||||
else if(xNew < 0) {
|
||||
xNew = width-1;
|
||||
}
|
||||
if(yNew >= height) {
|
||||
yNew = 0;
|
||||
}
|
||||
else if(yNew < 0) {
|
||||
yNew = height-1;
|
||||
}
|
||||
x = xNew;
|
||||
y = yNew;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public boolean agentInCell(int x, int y) {
|
||||
boolean flag = false;
|
||||
if (this.x == x && this.y == y) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void reproduced() {
|
||||
fertileFlag = false;
|
||||
fertility = 0;
|
||||
this.setHasReproduced(true);
|
||||
}
|
||||
|
||||
public void tickFertility()
|
||||
{
|
||||
if (fertility < maxFertility) {
|
||||
fertility ++;
|
||||
}
|
||||
if (fertility == maxFertility) {
|
||||
fertileFlag = true;
|
||||
}
|
||||
}
|
||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
package backend;
|
||||
|
||||
public class BasicFunctions {
|
||||
|
||||
public BasicFunctions()
|
||||
{
|
||||
}
|
||||
|
||||
public String intArrayToCSVString(int[] array) {
|
||||
String line = "";
|
||||
for(int x=0; x<array.length;x++) {
|
||||
line = line + Integer.toString(array[x]) + ";";
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
public boolean isIntInArray(int[] array, int numToFind) {
|
||||
boolean flag = false;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (numToFind == array[i]) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
package backend;
|
||||
|
||||
public class BasicFunctions {
|
||||
|
||||
public BasicFunctions()
|
||||
{
|
||||
}
|
||||
|
||||
public String intArrayToCSVString(int[] array) {
|
||||
String line = "";
|
||||
for(int x=0; x<array.length;x++) {
|
||||
line = line + Integer.toString(array[x]) + ";";
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
public boolean isIntInArray(int[] array, int numToFind) {
|
||||
boolean flag = false;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (numToFind == array[i]) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,81 +1,82 @@
|
|||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
public class Cell {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int value;
|
||||
|
||||
|
||||
|
||||
public Cell (int x, int y, int value) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int incrementCount(int count,int[][] world, int xLocal, int yLocal, int iteration) {
|
||||
int borderCell= world[xLocal][yLocal];
|
||||
if(borderCell==1) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
public int countNeighbouringAliveCells(int[][] world,boolean loopingBorderFlag,int width, int height, int iteration) {
|
||||
int count=0;
|
||||
// if (iteration == 1) {
|
||||
// System.out.println(x + " " + y);
|
||||
// }
|
||||
|
||||
for(int n=-1; n<=1;n++) {
|
||||
for (int m=-1; m<=1; m++) {
|
||||
int xLocal=this.x+n;
|
||||
int yLocal=this.y+m;
|
||||
if (this.x != width-1 && this.x != 0 && this.y != height-1 && this.y != 0) {
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
else {
|
||||
if (loopingBorderFlag) {
|
||||
if(xLocal > width-1) {
|
||||
xLocal=0;
|
||||
}
|
||||
if(xLocal < 0) {
|
||||
xLocal=width-1;
|
||||
}
|
||||
if(yLocal > height-1) {
|
||||
yLocal=0;
|
||||
}
|
||||
if(yLocal < 0) {
|
||||
yLocal=height-1;
|
||||
}
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
else {
|
||||
if (xLocal <= width-1 && xLocal >= 0 && yLocal <= height-1 && yLocal >= 0) {
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return count-this.value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package backend;
|
||||
|
||||
public class Cell {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int value;
|
||||
|
||||
|
||||
|
||||
public Cell (int x, int y, int value) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int[] incrementCount(int count[],int[][] world, int xLocal, int yLocal, int iteration) {
|
||||
int borderCell= world[xLocal][yLocal];
|
||||
count[borderCell] ++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
public int[] countNeighbouringAliveCells(int[][] world,boolean loopingBorderFlag,int width, int height, int iteration) {
|
||||
int[] count = new int[5];
|
||||
// if (iteration == 1) {
|
||||
// System.out.println(x + " " + y);
|
||||
// }
|
||||
|
||||
for(int n=-1; n<=1;n++) {
|
||||
for (int m=-1; m<=1; m++) {
|
||||
int xLocal=this.x+n;
|
||||
int yLocal=this.y+m;
|
||||
boolean isCell = false;
|
||||
if (xLocal == this.x && yLocal == this.y) {
|
||||
isCell = true;
|
||||
}
|
||||
if(!isCell) {
|
||||
if (this.x != width-1 && this.x != 0 && this.y != height-1 && this.y != 0) {
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
else {
|
||||
if (loopingBorderFlag) {
|
||||
if(xLocal > width-1) {
|
||||
xLocal=0;
|
||||
}
|
||||
if(xLocal < 0) {
|
||||
xLocal=width-1;
|
||||
}
|
||||
if(yLocal > height-1) {
|
||||
yLocal=0;
|
||||
}
|
||||
if(yLocal < 0) {
|
||||
yLocal=height-1;
|
||||
}
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
else {
|
||||
if (xLocal <= width-1 && xLocal >= 0 && yLocal <= height-1 && yLocal >= 0) {
|
||||
count=this.incrementCount(count,world, xLocal, yLocal, iteration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,80 +1,80 @@
|
|||
package backend;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class File {
|
||||
|
||||
String fileType;
|
||||
int[] size;
|
||||
|
||||
public File(String fileType)
|
||||
{
|
||||
this.fileType = fileType;
|
||||
this.size = new int[2];
|
||||
}
|
||||
|
||||
public String getFileType() {
|
||||
return this.fileType;
|
||||
}
|
||||
|
||||
public int[] getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
public int[][] loadSaveFile(ArrayList<String> lines) {
|
||||
/*
|
||||
* First some checks that the file is usable
|
||||
* We call early returns in conditions like this
|
||||
* "Guard clauses", as they guard the method
|
||||
* against unwanted inputs
|
||||
*/
|
||||
int[][] blank = new int[0][0];
|
||||
|
||||
if(lines.size()<=0) {
|
||||
return blank;
|
||||
}
|
||||
String firstLine = lines.get(0);
|
||||
String[] firstLineElements = firstLine.split(";");
|
||||
if(firstLineElements.length<=0) {
|
||||
return blank;
|
||||
}
|
||||
/*
|
||||
* now we fill in the world
|
||||
* with the content of the file
|
||||
*/
|
||||
int height = lines.size();
|
||||
int width = lines.get(0).split(";").length;
|
||||
this.size[0] = height;
|
||||
this.size[1] = width;
|
||||
|
||||
int[][] fileImage = new int[width][height];
|
||||
for(int y =0; y<lines.size();y++) {
|
||||
String line = lines.get(y);
|
||||
String[] lineElements = line.split(";");
|
||||
for(int x=0; x<lineElements.length;x++) {
|
||||
String elem = lineElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
fileImage[x][y] = value;
|
||||
}
|
||||
}
|
||||
return fileImage;
|
||||
}
|
||||
|
||||
public void writeFile(String fileName, String[] content) {
|
||||
FileWriter csvWriter;
|
||||
try {
|
||||
csvWriter = new FileWriter(fileName);
|
||||
for (String row : content) {
|
||||
csvWriter.append(row);
|
||||
csvWriter.append("\n");
|
||||
}
|
||||
csvWriter.flush();
|
||||
csvWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class File {
|
||||
|
||||
String fileType;
|
||||
int[] size;
|
||||
|
||||
public File(String fileType)
|
||||
{
|
||||
this.fileType = fileType;
|
||||
this.size = new int[2];
|
||||
}
|
||||
|
||||
public String getFileType() {
|
||||
return this.fileType;
|
||||
}
|
||||
|
||||
public int[] getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
public int[][] loadSaveFile(ArrayList<String> lines) {
|
||||
/*
|
||||
* First some checks that the file is usable
|
||||
* We call early returns in conditions like this
|
||||
* "Guard clauses", as they guard the method
|
||||
* against unwanted inputs
|
||||
*/
|
||||
int[][] blank = new int[0][0];
|
||||
|
||||
if(lines.size()<=0) {
|
||||
return blank;
|
||||
}
|
||||
String firstLine = lines.get(0);
|
||||
String[] firstLineElements = firstLine.split(";");
|
||||
if(firstLineElements.length<=0) {
|
||||
return blank;
|
||||
}
|
||||
/*
|
||||
* now we fill in the world
|
||||
* with the content of the file
|
||||
*/
|
||||
int height = lines.size();
|
||||
int width = lines.get(0).split(";").length;
|
||||
this.size[0] = height;
|
||||
this.size[1] = width;
|
||||
|
||||
int[][] fileImage = new int[width][height];
|
||||
for(int y =0; y<lines.size();y++) {
|
||||
String line = lines.get(y);
|
||||
String[] lineElements = line.split(";");
|
||||
for(int x=0; x<lineElements.length;x++) {
|
||||
String elem = lineElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
fileImage[x][y] = value;
|
||||
}
|
||||
}
|
||||
return fileImage;
|
||||
}
|
||||
|
||||
public void writeFile(String fileName, String[] content) {
|
||||
FileWriter csvWriter;
|
||||
try {
|
||||
csvWriter = new FileWriter(fileName);
|
||||
for (String row : content) {
|
||||
csvWriter.append(row);
|
||||
csvWriter.append("\n");
|
||||
}
|
||||
csvWriter.flush();
|
||||
csvWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
package backend;
|
||||
|
||||
public class NeighbourCellRules {
|
||||
|
||||
}
|
||||
|
|
@ -1,60 +1,79 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Rule {
|
||||
|
||||
private int[] surviveArray;
|
||||
private int[] birthArray;
|
||||
private BasicFunctions functions;
|
||||
|
||||
public Rule() {
|
||||
functions = new BasicFunctions();
|
||||
}
|
||||
|
||||
public int[] getSurviveArray()
|
||||
{
|
||||
return this.surviveArray;
|
||||
}
|
||||
|
||||
public int[] getBirthArray()
|
||||
{
|
||||
return this.birthArray;
|
||||
}
|
||||
|
||||
public ArrayList<String> getRuleDataLines() {
|
||||
ArrayList<String> dataLines = new ArrayList<String>();
|
||||
dataLines.add(functions.intArrayToCSVString(this.surviveArray));
|
||||
dataLines.add(functions.intArrayToCSVString(this.birthArray));
|
||||
return dataLines;
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
if(lines.size()<=0) {
|
||||
System.out.println("empty rule file");
|
||||
return;
|
||||
}
|
||||
String surviveLine = lines.get(0);
|
||||
String birthLine = lines.get(1);
|
||||
String[] surviveElements = surviveLine.split(";");
|
||||
this.surviveArray = new int[surviveElements.length];
|
||||
for(int x=0; x<surviveElements.length;x++) {
|
||||
String elem = surviveElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
this.surviveArray[x] = value;
|
||||
//TODO : add value to possible survive values
|
||||
}
|
||||
String[] birthElements = birthLine.split(";");
|
||||
this.birthArray = new int[birthElements.length];
|
||||
for(int x=0; x<birthElements.length;x++) {
|
||||
String elem = birthElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
this.birthArray[x] = value;
|
||||
//TODO : add value to possible birth values
|
||||
|
||||
}
|
||||
if (lines.size() > 2) {
|
||||
String possibleEntites = lines.get(2).split(";")[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Rule {
|
||||
|
||||
private int[][] surviveArrays;
|
||||
private int[][] birthArrays;
|
||||
private BasicFunctions functions;
|
||||
private int entitiesAmount;
|
||||
|
||||
public Rule() {
|
||||
functions = new BasicFunctions();
|
||||
}
|
||||
|
||||
public int[][] getSurviveArrays()
|
||||
{
|
||||
return this.surviveArrays;
|
||||
}
|
||||
|
||||
public int[][] getBirthArrays()
|
||||
{
|
||||
return this.birthArrays;
|
||||
}
|
||||
|
||||
public int getEntitiesAmount() {
|
||||
return entitiesAmount;
|
||||
}
|
||||
|
||||
public ArrayList<String> getRuleDataLines() {
|
||||
ArrayList<String> dataLines = new ArrayList<String>();
|
||||
String surviveLine = "";
|
||||
for(int x = 0; x < entitiesAmount; x ++) {
|
||||
surviveLine = surviveLine + functions.intArrayToCSVString(this.surviveArrays[x]) + ",";
|
||||
}
|
||||
dataLines.add(surviveLine);
|
||||
|
||||
String birthLine = "";
|
||||
for(int x = 0; x < entitiesAmount; x ++) {
|
||||
birthLine = birthLine + functions.intArrayToCSVString(this.birthArrays[x]) + ",";
|
||||
}
|
||||
dataLines.add(birthLine);
|
||||
return dataLines;
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
if(lines.size()<=0) {
|
||||
System.out.println("empty rule file");
|
||||
return;
|
||||
}
|
||||
String surviveLine = lines.get(0);
|
||||
String birthLine = lines.get(1);
|
||||
String[] surviveCells = surviveLine.split(",");
|
||||
this.entitiesAmount = surviveCells.length;
|
||||
this.surviveArrays = new int[surviveCells.length][0];
|
||||
for (int y = 0; y<surviveCells.length; y++) {
|
||||
String[] surviveElements = surviveCells[y].split(";");
|
||||
int[] surviveArray = new int[surviveElements.length];
|
||||
for(int x=0; x<surviveElements.length;x++) {
|
||||
String elem = surviveElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
surviveArray[x] = value;
|
||||
}
|
||||
this.surviveArrays[y] = surviveArray;
|
||||
}
|
||||
String[] birthCells = birthLine.split(",");
|
||||
this.birthArrays = new int[birthCells.length][0];
|
||||
for (int y = 0; y<birthCells.length; y++) {
|
||||
String[] birthElements = birthCells[y].split(";");
|
||||
int[] birthArray = new int[birthElements.length];
|
||||
for(int x=0; x<birthElements.length;x++) {
|
||||
String elem = birthElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
birthArray[x] = value;
|
||||
}
|
||||
this.birthArrays[y] = birthArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,55 +1,55 @@
|
|||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
|
||||
// example of basic animal.
|
||||
// do not hesitate to make it more complex
|
||||
// and DO add at least another species that interact with it
|
||||
// for example wolves that eat Sheep
|
||||
public class Sheep extends Agent {
|
||||
|
||||
private int hunger;
|
||||
private int maxHunger;
|
||||
|
||||
private int maxFertility = 5;
|
||||
|
||||
Sheep(int x,int y){
|
||||
//first we call the constructor of the superClass(Animal)
|
||||
//with the values we want.
|
||||
// here we decide that a Sheep is initially white using this constructor
|
||||
super(x,y,Color.white, 5);
|
||||
// we give our sheep a hunger value of zero at birth
|
||||
hunger = 0;
|
||||
maxHunger = 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* action of the animal
|
||||
* it can interact with the cells or with other animals
|
||||
* as you wish
|
||||
*/
|
||||
public int getMaxHunger()
|
||||
{
|
||||
return maxHunger;
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
if(x >= 0 && y >=0 && x < world.getWidth() && y < world.getHeight()) {
|
||||
if(world.getCell(x, y)==1) {
|
||||
world.setCell(x, y, 0);
|
||||
} else {
|
||||
this.hunger++;
|
||||
}
|
||||
this.moveRandom(neighbors, world, borderLoopFlag);
|
||||
|
||||
}
|
||||
return this.hunger<this.maxHunger;
|
||||
}
|
||||
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
|
||||
// example of basic animal.
|
||||
// do not hesitate to make it more complex
|
||||
// and DO add at least another species that interact with it
|
||||
// for example wolves that eat Sheep
|
||||
public class Sheep extends Agent {
|
||||
|
||||
private int hunger;
|
||||
private int maxHunger;
|
||||
|
||||
private int maxFertility = 5;
|
||||
|
||||
Sheep(int x,int y){
|
||||
//first we call the constructor of the superClass(Animal)
|
||||
//with the values we want.
|
||||
// here we decide that a Sheep is initially white using this constructor
|
||||
super(x,y,Color.white, 5);
|
||||
// we give our sheep a hunger value of zero at birth
|
||||
hunger = 0;
|
||||
maxHunger = 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* action of the animal
|
||||
* it can interact with the cells or with other animals
|
||||
* as you wish
|
||||
*/
|
||||
public int getMaxHunger()
|
||||
{
|
||||
return maxHunger;
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
if(x >= 0 && y >=0 && x < world.getWidth() && y < world.getHeight()) {
|
||||
if(world.getCell(x, y)==1) {
|
||||
world.setCell(x, y, 0);
|
||||
} else {
|
||||
this.hunger++;
|
||||
}
|
||||
this.moveRandom(neighbors, world, borderLoopFlag);
|
||||
|
||||
}
|
||||
return this.hunger<this.maxHunger;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,54 +1,54 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.awt.Color;
|
||||
|
||||
public class Wolf extends Agent {
|
||||
|
||||
int hunger;
|
||||
int maxHunger;
|
||||
int sheepEatenWeight;
|
||||
Sheep sheepEaten;
|
||||
|
||||
int maxFertility = 10;
|
||||
|
||||
Wolf(int x, int y){
|
||||
super(x,y,Color.red, 10);
|
||||
hunger = 0;
|
||||
maxHunger = 20;
|
||||
sheepEatenWeight = 5;
|
||||
}
|
||||
|
||||
public Sheep getSheepEaten() {
|
||||
return sheepEaten;
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
sheepEaten = null;
|
||||
for(Agent agent : neighbors) {
|
||||
if (agent instanceof Sheep)
|
||||
{
|
||||
if (hunger > 0) {
|
||||
this.eatSheep((Sheep) agent);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sheepEaten == null) {
|
||||
hunger ++;
|
||||
}
|
||||
this.moveRandom(neighbors, world, borderLoopFlag);
|
||||
|
||||
return hunger<maxHunger;
|
||||
}
|
||||
|
||||
private void eatSheep(Sheep sheep) {
|
||||
this.hunger = hunger - sheepEatenWeight;
|
||||
sheepEaten = sheep;
|
||||
}
|
||||
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.awt.Color;
|
||||
|
||||
public class Wolf extends Agent {
|
||||
|
||||
int hunger;
|
||||
int maxHunger;
|
||||
int sheepEatenWeight;
|
||||
Sheep sheepEaten;
|
||||
|
||||
int maxFertility = 10;
|
||||
|
||||
Wolf(int x, int y){
|
||||
super(x,y,Color.red, 10);
|
||||
hunger = 0;
|
||||
maxHunger = 20;
|
||||
sheepEatenWeight = 5;
|
||||
}
|
||||
|
||||
public Sheep getSheepEaten() {
|
||||
return sheepEaten;
|
||||
}
|
||||
|
||||
public int getMaxFertility() {
|
||||
return maxFertility;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean liveTurn(ArrayList<Agent> neighbors, World world, boolean borderLoopFlag) {
|
||||
sheepEaten = null;
|
||||
for(Agent agent : neighbors) {
|
||||
if (agent instanceof Sheep)
|
||||
{
|
||||
if (hunger > 0) {
|
||||
this.eatSheep((Sheep) agent);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sheepEaten == null) {
|
||||
hunger ++;
|
||||
}
|
||||
this.moveRandom(neighbors, world, borderLoopFlag);
|
||||
|
||||
return hunger<maxHunger;
|
||||
}
|
||||
|
||||
private void eatSheep(Sheep sheep) {
|
||||
this.hunger = hunger - sheepEatenWeight;
|
||||
sheepEaten = sheep;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,71 +1,71 @@
|
|||
package backend;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class World {
|
||||
|
||||
private int height = 100;
|
||||
private int width = 100;
|
||||
private int[][] world;
|
||||
|
||||
public World(int height, int width)
|
||||
{
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
|
||||
world = new int[width][height];
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public int[][] getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
public void setWorld(int[][] world, int width, int height)
|
||||
{
|
||||
this.world = world;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
/**
|
||||
* get cell value in simulated world
|
||||
* @param x coordinate of cell
|
||||
* @param y coordinate of cell
|
||||
* @return value of cell
|
||||
*/
|
||||
public int getCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
return world[x][y];
|
||||
}
|
||||
|
||||
/**
|
||||
* set value of cell
|
||||
* @param x coord of cell
|
||||
* @param y coord of cell
|
||||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
world[x][y] = val;
|
||||
}
|
||||
|
||||
public void generateRandom(float chanceOfLife) {
|
||||
for(int x=0; x<width;x++) {
|
||||
for (int y=0; y<height; y++) {
|
||||
Random rand = new Random();
|
||||
if (rand.nextDouble() <= chanceOfLife) {
|
||||
world[x][y] = 1;
|
||||
}
|
||||
else {
|
||||
world[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package backend;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class World {
|
||||
|
||||
private int height = 100;
|
||||
private int width = 100;
|
||||
private int[][] world;
|
||||
|
||||
public World(int height, int width)
|
||||
{
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
|
||||
world = new int[width][height];
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return this.width;
|
||||
}
|
||||
|
||||
public int[][] getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
public void setWorld(int[][] world, int width, int height)
|
||||
{
|
||||
this.world = world;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
/**
|
||||
* get cell value in simulated world
|
||||
* @param x coordinate of cell
|
||||
* @param y coordinate of cell
|
||||
* @return value of cell
|
||||
*/
|
||||
public int getCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
return world[x][y];
|
||||
}
|
||||
|
||||
/**
|
||||
* set value of cell
|
||||
* @param x coord of cell
|
||||
* @param y coord of cell
|
||||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
world[x][y] = val;
|
||||
}
|
||||
|
||||
public void generateRandom(float chanceOfLife) {
|
||||
for(int x=0; x<width;x++) {
|
||||
for (int y=0; y<height; y++) {
|
||||
Random rand = new Random();
|
||||
if (rand.nextDouble() <= chanceOfLife) {
|
||||
world[x][y] = 1;
|
||||
}
|
||||
else {
|
||||
world[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,98 +1,98 @@
|
|||
package windowInterface;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import backend.Agent;
|
||||
import backend.Simulator;
|
||||
import backend.World;
|
||||
|
||||
public class JPanelDraw extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
private World world;
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
world = mySimu.getSimWorld();
|
||||
int x = (me.getX()*world.getWidth())/getWidth();
|
||||
int y = (me.getY()*world.getHeight())/getHeight();
|
||||
mySimu.clickCell(x,y);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
world = mySimu.getSimWorld();
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float)this.getWidth()/(float)world.getWidth();
|
||||
float cellHeight = (float)this.getHeight()/(float)world.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<world.getWidth();x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<world.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for(int x=0; x<world.getWidth();x++) {
|
||||
for (int y=0; y<world.getHeight(); y++) {
|
||||
int cellContent = world.getCell(x,y);
|
||||
if(cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if(cellContent == 1) {
|
||||
g.setColor(Color.green);
|
||||
}
|
||||
if(cellContent == 2) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
if(cellContent == 3) {
|
||||
g.setColor(Color.cyan);
|
||||
}
|
||||
g.fillRect(
|
||||
(int) Math.round(x*cellWidth),
|
||||
(int) Math.round(y*cellHeight),
|
||||
(int) Math.round(cellWidth),
|
||||
(int) Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (Agent animal:mySimu.getAnimals()){
|
||||
g.setColor(animal.getDisplayColor());
|
||||
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
||||
(int)Math.round(animal.getY()*cellHeight),
|
||||
(int)Math.round(cellWidth/2),
|
||||
(int)Math.round(cellHeight/2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package windowInterface;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import backend.Agent;
|
||||
import backend.Simulator;
|
||||
import backend.World;
|
||||
|
||||
public class JPanelDraw extends JPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Simulator mySimu;
|
||||
private MyInterface interfaceGlobal;
|
||||
private World world;
|
||||
|
||||
public JPanelDraw(MyInterface itf) {
|
||||
super();
|
||||
mySimu = null;
|
||||
interfaceGlobal = itf;
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent me) {
|
||||
// System.out.println(me);
|
||||
if(mySimu == null) {
|
||||
interfaceGlobal.instantiateSimu();
|
||||
}
|
||||
world = mySimu.getSimWorld();
|
||||
int x = (me.getX()*world.getWidth())/getWidth();
|
||||
int y = (me.getY()*world.getHeight())/getHeight();
|
||||
mySimu.clickCell(x,y);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void setSimu(Simulator simu) {
|
||||
mySimu = simu;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
this.setBackground(Color.black);
|
||||
if (mySimu != null) {
|
||||
world = mySimu.getSimWorld();
|
||||
// Draw Interface from state of simulator
|
||||
float cellWidth = (float)this.getWidth()/(float)world.getWidth();
|
||||
float cellHeight = (float)this.getHeight()/(float)world.getHeight();
|
||||
g.setColor(Color.gray);
|
||||
for(int x=0; x<world.getWidth();x++) {
|
||||
int graphX = Math.round(x*cellWidth);
|
||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||
}
|
||||
for (int y=0; y<world.getHeight(); y++) {
|
||||
int graphY = Math.round(y*cellHeight);
|
||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||
}
|
||||
for(int x=0; x<world.getWidth();x++) {
|
||||
for (int y=0; y<world.getHeight(); y++) {
|
||||
int cellContent = world.getCell(x,y);
|
||||
if(cellContent == 0) {
|
||||
continue;
|
||||
}
|
||||
if(cellContent == 1) {
|
||||
g.setColor(Color.green);
|
||||
}
|
||||
if(cellContent == 2) {
|
||||
g.setColor(Color.yellow);
|
||||
}
|
||||
if(cellContent == 3) {
|
||||
g.setColor(Color.cyan);
|
||||
}
|
||||
g.fillRect(
|
||||
(int) Math.round(x*cellWidth),
|
||||
(int) Math.round(y*cellHeight),
|
||||
(int) Math.round(cellWidth),
|
||||
(int) Math.round(cellHeight)
|
||||
);
|
||||
}
|
||||
}
|
||||
for (Agent animal:mySimu.getAnimals()){
|
||||
g.setColor(animal.getDisplayColor());
|
||||
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
||||
(int)Math.round(animal.getY()*cellHeight),
|
||||
(int)Math.round(cellWidth/2),
|
||||
(int)Math.round(cellHeight/2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,412 +1,412 @@
|
|||
package windowInterface;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import backend.Simulator;
|
||||
import backend.File;
|
||||
import backend.World;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class MyInterface extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = -6840815447618468846L;
|
||||
private JPanel contentPane;
|
||||
private JLabel stepLabel;
|
||||
private JLabel borderLabel;
|
||||
private JLabel speedLabel;
|
||||
private JPanelDraw panelDraw;
|
||||
private Simulator mySimu=null;
|
||||
private JSlider randSlider;
|
||||
private JSlider speedSlider;
|
||||
private JLabel clickLabel;
|
||||
private File worldFile;
|
||||
private File ruleFile;
|
||||
private File agentFile;
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public MyInterface() {
|
||||
|
||||
agentFile = new File("agent");
|
||||
ruleFile = new File("rule");
|
||||
worldFile = new File("world");
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(10, 10, 700, 600);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
setContentPane(contentPane);
|
||||
|
||||
JPanel panelTop = new JPanel();
|
||||
contentPane.add(panelTop, BorderLayout.NORTH);
|
||||
|
||||
JPanel panelRight = new JPanel();
|
||||
panelRight.setLayout(new GridLayout(12,1));
|
||||
contentPane.add(panelRight, BorderLayout.EAST);
|
||||
|
||||
JButton btnGo = new JButton("Start/Pause");
|
||||
btnGo.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonGo();
|
||||
}
|
||||
});
|
||||
panelTop.add(btnGo);
|
||||
|
||||
JButton btnClickAct = new JButton("Toggle Click");
|
||||
btnClickAct.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonToggleClickAction();
|
||||
}
|
||||
});
|
||||
panelTop.add(btnClickAct);
|
||||
|
||||
stepLabel = new JLabel("Step : X");
|
||||
panelTop.add(stepLabel);
|
||||
|
||||
speedLabel = new JLabel("speed slider : ");
|
||||
panelTop.add(speedLabel);
|
||||
|
||||
speedSlider = new JSlider();
|
||||
speedSlider.setValue(3);
|
||||
speedSlider.setMinimum(0);
|
||||
speedSlider.setMaximum(10);
|
||||
speedSlider.setOrientation(SwingConstants.HORIZONTAL);
|
||||
speedSlider.setPreferredSize(new Dimension(100,30));
|
||||
speedSlider.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent arg0) {
|
||||
changeSpeed();
|
||||
}
|
||||
});
|
||||
panelTop.add(speedSlider);
|
||||
|
||||
// JButton btnSpeed = new JButton("Set Speed");
|
||||
// btnSpeed.addActionListener(new ActionListener() {
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// clicButtonSpeed();
|
||||
// }
|
||||
// });
|
||||
// panelTop.add(btnSpeed);
|
||||
|
||||
JButton btnLoad = new JButton("Load World");
|
||||
btnLoad.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoad);
|
||||
|
||||
JButton btnSave = new JButton("Save World");
|
||||
btnSave.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSave);
|
||||
|
||||
|
||||
JButton btnLoadRule = new JButton("Load Rule");
|
||||
btnLoadRule.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadRuleFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoadRule);
|
||||
|
||||
JButton btnSaveRule = new JButton("Save Rule");
|
||||
btnSaveRule.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveRuleToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSaveRule);
|
||||
|
||||
JButton btnLoadAgents = new JButton("Load Agents");
|
||||
btnLoadAgents.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadAgentsFileButton();;
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoadAgents);
|
||||
|
||||
JButton btnSaveAgents = new JButton("Save Agents");
|
||||
btnSaveAgents.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveAgentsToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSaveAgents);
|
||||
|
||||
JButton btnRandGen = new JButton("Random Field");
|
||||
btnRandGen.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
generateRandomBoard();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnRandGen);
|
||||
|
||||
|
||||
JLabel randLabel = new JLabel("random density slider :");
|
||||
panelRight.add(randLabel);
|
||||
|
||||
randSlider = new JSlider();
|
||||
randSlider.setValue(50);
|
||||
randSlider.setMinimum(0);
|
||||
randSlider.setMaximum(100);
|
||||
randSlider.setPreferredSize(new Dimension(30,200));
|
||||
panelRight.add(randSlider);
|
||||
|
||||
|
||||
JButton btnBorder = new JButton("Toggle Border");
|
||||
btnBorder.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonBorder();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnBorder);
|
||||
|
||||
panelDraw = new JPanelDraw(this);
|
||||
contentPane.add(panelDraw, BorderLayout.CENTER);
|
||||
|
||||
instantiateSimu();
|
||||
|
||||
borderLabel = new JLabel("border : X");
|
||||
panelRight.add(borderLabel);
|
||||
borderLabel.setText("border : " +
|
||||
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||
|
||||
clickLabel = new JLabel("click : X");
|
||||
panelRight.add(clickLabel);
|
||||
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||
}
|
||||
|
||||
public void setStepBanner(String s) {
|
||||
stepLabel.setText(s);
|
||||
}
|
||||
|
||||
public void setBorderBanner(String s) {
|
||||
borderLabel.setText(s);
|
||||
}
|
||||
|
||||
public void setClickBanner(String s) {
|
||||
clickLabel.setText(s);
|
||||
}
|
||||
|
||||
public JPanelDraw getPanelDessin() {
|
||||
return panelDraw;
|
||||
}
|
||||
|
||||
public void instantiateSimu() {
|
||||
if(mySimu==null) {
|
||||
mySimu = new Simulator(this);
|
||||
panelDraw.setSimu(mySimu);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonGo() {
|
||||
this.instantiateSimu();
|
||||
if(!mySimu.isAlive()) {
|
||||
mySimu.start();
|
||||
} else {
|
||||
mySimu.togglePause();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonToggleClickAction() {
|
||||
if(mySimu != null) {
|
||||
mySimu.toggleClickAction();
|
||||
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonBorder() {
|
||||
if(mySimu != null) {
|
||||
mySimu.toggleLoopingBorder();
|
||||
borderLabel.setText("border : " +
|
||||
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||
}
|
||||
}
|
||||
|
||||
public void generateRandomBoard() {
|
||||
this.instantiateSimu();
|
||||
float chanceOfLife = ((float)randSlider.getValue())/((float)randSlider.getMaximum());
|
||||
mySimu.generateRandom(chanceOfLife);
|
||||
panelDraw.repaint();
|
||||
}
|
||||
|
||||
public void changeSpeed() {
|
||||
if(mySimu != null) {
|
||||
int delay = (int)Math.pow(2, 10-speedSlider.getValue());
|
||||
mySimu.setLoopDelay(delay);
|
||||
} else {
|
||||
speedSlider.setValue(3);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadFileButton() {
|
||||
Simulator loadedSim = new Simulator(this);
|
||||
String fileName=SelectFile("World");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(mySimu != null) {
|
||||
mySimu.stopSimu();
|
||||
this.eraseLabels();
|
||||
}
|
||||
int[][] fileImage = this.worldFile.loadSaveFile(stringArray);
|
||||
World world = loadedSim.getSimWorld();
|
||||
int[] size = this.worldFile.getSize();
|
||||
world.setWorld(fileImage, size[0], size[1]);
|
||||
loadedSim.setSimWorld(world);
|
||||
mySimu = loadedSim;
|
||||
panelDraw.setSimu(mySimu);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadRuleFileButton() {
|
||||
String fileName=SelectFile("Rule");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mySimu.setRule(stringArray);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadAgentsFileButton() {
|
||||
String fileName=SelectFile("Agents");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mySimu.loadAgents(stringArray);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void clicSaveToFileButton() {
|
||||
String fileName=SelectFile("World");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getSaveState();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
worldFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicSaveRuleToFileButton() {
|
||||
String fileName=SelectFile("Rule");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getRule().getRuleDataLines();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
ruleFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicSaveAgentsToFileButton() {
|
||||
String fileName=SelectFile("Agents");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getAgentsSave();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
agentFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String SelectFile(String fileType) {
|
||||
String s;
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setCurrentDirectory(new java.io.File(".\\" + fileType));
|
||||
chooser.setDialogTitle("Choose a file");
|
||||
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
||||
chooser.setAcceptAllFileFilterUsed(true);
|
||||
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
s=chooser.getSelectedFile().toString();
|
||||
} else {
|
||||
System.out.println("No Selection ");
|
||||
s="";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void writeFile(String fileName, String[] content) {
|
||||
FileWriter csvWriter;
|
||||
try {
|
||||
csvWriter = new FileWriter(fileName);
|
||||
for (String row : content) {
|
||||
csvWriter.append(row);
|
||||
csvWriter.append("\n");
|
||||
}
|
||||
csvWriter.flush();
|
||||
csvWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void update (int stepCount) {
|
||||
this.setStepBanner("Step : "+ stepCount);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void eraseLabels() {
|
||||
this.setStepBanner("Step : X");
|
||||
this.setBorderBanner("border : X");
|
||||
this.setClickBanner("click : X");
|
||||
speedSlider.setValue(3);
|
||||
}
|
||||
|
||||
}
|
||||
package windowInterface;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import backend.Simulator;
|
||||
import backend.File;
|
||||
import backend.World;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class MyInterface extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = -6840815447618468846L;
|
||||
private JPanel contentPane;
|
||||
private JLabel stepLabel;
|
||||
private JLabel borderLabel;
|
||||
private JLabel speedLabel;
|
||||
private JPanelDraw panelDraw;
|
||||
private Simulator mySimu=null;
|
||||
private JSlider randSlider;
|
||||
private JSlider speedSlider;
|
||||
private JLabel clickLabel;
|
||||
private File worldFile;
|
||||
private File ruleFile;
|
||||
private File agentFile;
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public MyInterface() {
|
||||
|
||||
agentFile = new File("agent");
|
||||
ruleFile = new File("rule");
|
||||
worldFile = new File("world");
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(10, 10, 700, 600);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
setContentPane(contentPane);
|
||||
|
||||
JPanel panelTop = new JPanel();
|
||||
contentPane.add(panelTop, BorderLayout.NORTH);
|
||||
|
||||
JPanel panelRight = new JPanel();
|
||||
panelRight.setLayout(new GridLayout(12,1));
|
||||
contentPane.add(panelRight, BorderLayout.EAST);
|
||||
|
||||
JButton btnGo = new JButton("Start/Pause");
|
||||
btnGo.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonGo();
|
||||
}
|
||||
});
|
||||
panelTop.add(btnGo);
|
||||
|
||||
JButton btnClickAct = new JButton("Toggle Click");
|
||||
btnClickAct.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonToggleClickAction();
|
||||
}
|
||||
});
|
||||
panelTop.add(btnClickAct);
|
||||
|
||||
stepLabel = new JLabel("Step : X");
|
||||
panelTop.add(stepLabel);
|
||||
|
||||
speedLabel = new JLabel("speed slider : ");
|
||||
panelTop.add(speedLabel);
|
||||
|
||||
speedSlider = new JSlider();
|
||||
speedSlider.setValue(3);
|
||||
speedSlider.setMinimum(0);
|
||||
speedSlider.setMaximum(10);
|
||||
speedSlider.setOrientation(SwingConstants.HORIZONTAL);
|
||||
speedSlider.setPreferredSize(new Dimension(100,30));
|
||||
speedSlider.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent arg0) {
|
||||
changeSpeed();
|
||||
}
|
||||
});
|
||||
panelTop.add(speedSlider);
|
||||
|
||||
// JButton btnSpeed = new JButton("Set Speed");
|
||||
// btnSpeed.addActionListener(new ActionListener() {
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// clicButtonSpeed();
|
||||
// }
|
||||
// });
|
||||
// panelTop.add(btnSpeed);
|
||||
|
||||
JButton btnLoad = new JButton("Load World");
|
||||
btnLoad.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoad);
|
||||
|
||||
JButton btnSave = new JButton("Save World");
|
||||
btnSave.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSave);
|
||||
|
||||
|
||||
JButton btnLoadRule = new JButton("Load Rule");
|
||||
btnLoadRule.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadRuleFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoadRule);
|
||||
|
||||
JButton btnSaveRule = new JButton("Save Rule");
|
||||
btnSaveRule.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveRuleToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSaveRule);
|
||||
|
||||
JButton btnLoadAgents = new JButton("Load Agents");
|
||||
btnLoadAgents.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadAgentsFileButton();;
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoadAgents);
|
||||
|
||||
JButton btnSaveAgents = new JButton("Save Agents");
|
||||
btnSaveAgents.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveAgentsToFileButton();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSaveAgents);
|
||||
|
||||
JButton btnRandGen = new JButton("Random Field");
|
||||
btnRandGen.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
generateRandomBoard();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnRandGen);
|
||||
|
||||
|
||||
JLabel randLabel = new JLabel("random density slider :");
|
||||
panelRight.add(randLabel);
|
||||
|
||||
randSlider = new JSlider();
|
||||
randSlider.setValue(50);
|
||||
randSlider.setMinimum(0);
|
||||
randSlider.setMaximum(100);
|
||||
randSlider.setPreferredSize(new Dimension(30,200));
|
||||
panelRight.add(randSlider);
|
||||
|
||||
|
||||
JButton btnBorder = new JButton("Toggle Border");
|
||||
btnBorder.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonBorder();
|
||||
}
|
||||
});
|
||||
panelRight.add(btnBorder);
|
||||
|
||||
panelDraw = new JPanelDraw(this);
|
||||
contentPane.add(panelDraw, BorderLayout.CENTER);
|
||||
|
||||
instantiateSimu();
|
||||
|
||||
borderLabel = new JLabel("border : X");
|
||||
panelRight.add(borderLabel);
|
||||
borderLabel.setText("border : " +
|
||||
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||
|
||||
clickLabel = new JLabel("click : X");
|
||||
panelRight.add(clickLabel);
|
||||
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||
}
|
||||
|
||||
public void setStepBanner(String s) {
|
||||
stepLabel.setText(s);
|
||||
}
|
||||
|
||||
public void setBorderBanner(String s) {
|
||||
borderLabel.setText(s);
|
||||
}
|
||||
|
||||
public void setClickBanner(String s) {
|
||||
clickLabel.setText(s);
|
||||
}
|
||||
|
||||
public JPanelDraw getPanelDessin() {
|
||||
return panelDraw;
|
||||
}
|
||||
|
||||
public void instantiateSimu() {
|
||||
if(mySimu==null) {
|
||||
mySimu = new Simulator(this);
|
||||
panelDraw.setSimu(mySimu);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonGo() {
|
||||
this.instantiateSimu();
|
||||
if(!mySimu.isAlive()) {
|
||||
mySimu.start();
|
||||
} else {
|
||||
mySimu.togglePause();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonToggleClickAction() {
|
||||
if(mySimu != null) {
|
||||
mySimu.toggleClickAction();
|
||||
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||
}
|
||||
}
|
||||
|
||||
public void clicButtonBorder() {
|
||||
if(mySimu != null) {
|
||||
mySimu.toggleLoopingBorder();
|
||||
borderLabel.setText("border : " +
|
||||
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||
}
|
||||
}
|
||||
|
||||
public void generateRandomBoard() {
|
||||
this.instantiateSimu();
|
||||
float chanceOfLife = ((float)randSlider.getValue())/((float)randSlider.getMaximum());
|
||||
mySimu.generateRandom(chanceOfLife);
|
||||
panelDraw.repaint();
|
||||
}
|
||||
|
||||
public void changeSpeed() {
|
||||
if(mySimu != null) {
|
||||
int delay = (int)Math.pow(2, 10-speedSlider.getValue());
|
||||
mySimu.setLoopDelay(delay);
|
||||
} else {
|
||||
speedSlider.setValue(3);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadFileButton() {
|
||||
Simulator loadedSim = new Simulator(this);
|
||||
String fileName=SelectFile("World");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(mySimu != null) {
|
||||
mySimu.stopSimu();
|
||||
this.eraseLabels();
|
||||
}
|
||||
int[][] fileImage = this.worldFile.loadSaveFile(stringArray);
|
||||
World world = loadedSim.getSimWorld();
|
||||
int[] size = this.worldFile.getSize();
|
||||
world.setWorld(fileImage, size[0], size[1]);
|
||||
loadedSim.setSimWorld(world);
|
||||
mySimu = loadedSim;
|
||||
panelDraw.setSimu(mySimu);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadRuleFileButton() {
|
||||
String fileName=SelectFile("Rule");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mySimu.setRule(stringArray);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public void clicLoadAgentsFileButton() {
|
||||
String fileName=SelectFile("Agents");
|
||||
ArrayList<String> stringArray = new ArrayList<String>();
|
||||
if (fileName.length()>0) {
|
||||
try {
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
while (line != null) {
|
||||
stringArray.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mySimu.loadAgents(stringArray);
|
||||
this.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void clicSaveToFileButton() {
|
||||
String fileName=SelectFile("World");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getSaveState();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
worldFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicSaveRuleToFileButton() {
|
||||
String fileName=SelectFile("Rule");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getRule().getRuleDataLines();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
ruleFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
public void clicSaveAgentsToFileButton() {
|
||||
String fileName=SelectFile("Agents");
|
||||
if (fileName.length()>0) {
|
||||
ArrayList<String> content = mySimu.getAgentsSave();
|
||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||
agentFile.writeFile(fileName, strArr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String SelectFile(String fileType) {
|
||||
String s;
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
chooser.setCurrentDirectory(new java.io.File(".\\" + fileType));
|
||||
chooser.setDialogTitle("Choose a file");
|
||||
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
||||
chooser.setAcceptAllFileFilterUsed(true);
|
||||
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
s=chooser.getSelectedFile().toString();
|
||||
} else {
|
||||
System.out.println("No Selection ");
|
||||
s="";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void writeFile(String fileName, String[] content) {
|
||||
FileWriter csvWriter;
|
||||
try {
|
||||
csvWriter = new FileWriter(fileName);
|
||||
for (String row : content) {
|
||||
csvWriter.append(row);
|
||||
csvWriter.append("\n");
|
||||
}
|
||||
csvWriter.flush();
|
||||
csvWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void update (int stepCount) {
|
||||
this.setStepBanner("Step : "+ stepCount);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void eraseLabels() {
|
||||
this.setStepBanner("Step : X");
|
||||
this.setBorderBanner("border : X");
|
||||
this.setClickBanner("click : X");
|
||||
speedSlider.setValue(3);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue