Compare commits

...

2 Commits

Author SHA1 Message Date
julie 5cafa9ad0a final 2024-06-02 12:35:22 +02:00
julie 717eb2f842 last step 2024-06-01 19:56:41 +02:00
5 changed files with 286 additions and 205 deletions

View File

@ -30,10 +30,6 @@ public abstract class Agent {
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 abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);

21
src/backend/Cell.java Normal file
View File

@ -0,0 +1,21 @@
package backend;
// Cell in grid creation
public class Cell {
private int value;
// Constructor
public Cell(int value) {
this.value = value;
}
// obtain value of the cell
public int getValue() {
return value;
}
// Method to set value of the cell
public void setValue(int value) {
this.value = value;
}
}

98
src/backend/Gride.java Normal file
View File

@ -0,0 +1,98 @@
package backend;
import java.util.ArrayList;
// Class representing the grid
public class Gride {
// Attributes
private int height; // Height - grid
private int width; // Width - grid
private ArrayList<ArrayList<Cell>> gride; // 2D array for the grid
private Simulator simulator;
// Constructor
public Gride(int height, int width, Simulator tempSimulator) {
this.height = height; // Set height
this.width = width; // Set width
this.simulator = tempSimulator; // Set simulator object
// Create grid
gride = new ArrayList<>(height);
for (int i = 0; i < height; i++) {
this.gride.add(i, new ArrayList<Cell>());
for (int j = 0; j < width; j++) {
this.gride.get(i).add(new Cell(0)); // Initialize cells with value 0
}
}
}
// Method height of grid
public int getheight() {
return this.height;
}
// Method width of grid
public int getwidth() {
return this.width;
}
// Method lopping border
public boolean isLoopingBorder() {
return simulator.isLoopingBorder();
}
// Method to get cell at specified coordinates
public Cell getCell(int row,int column) {
return gride.get(row).get(column);
}
// Method to set cell at specified coordinates
public void setCell(int row, int column, Cell cell){
this.gride.get(row).set(column, cell);
}
// Count surrounding cells
public int countAround(int row, int column) {
int count = 0;
boolean loopingBorder = isLoopingBorder();
for (int i = row - 1; i <= row + 1; i++) {
for (int j = column - 1; j <= column + 1; j++) {
if (i == row && j == column) {
continue;
}
int x = i;
int y = j;
if (loopingBorder) {
x = (x + width) % width;
y = (y + height) % height;
} else {
if (x < 0 || x >= width || y < 0 || y >= height) {
continue;
}
}
count += this.getCell(x, y).getValue();
}
}
return count; // Return total count
}
// creation of a grid with specified density
public void setRandom(double density) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
double random = Math.random(); // Get random number
if (random < density) {
gride.get(i).get(j).setValue(1); // set cell value to 1 if is less than density
} else {
gride.get(i).get(j).setValue(0); // else set cell value to 0
}
}
}
}
}

View File

@ -1,76 +1,87 @@
package backend;
import java.util.ArrayList;
// Importing windowInterface for graphical user interface
import windowInterface.MyInterface;
// Class for simulation
public class Simulator extends Thread {
// Interface object
private MyInterface mjf;
private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1;
//Animal Neighborhood Radius : 5
private final int ANIMAL_AREA_RADIUS = 2;
private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues;
// Constants for grid dimensions and other parameters
private final int COL_NUM = 100; // Number of columns in the grid
private final int LINE_NUM = 100; // Number of lines in the grid
private final int LIFE_TYPE_NUM = 4; // Number of life types
private final int LIFE_AREA_RADIUS = 1; // Radius for life area
private final int ANIMAL_AREA_RADIUS = 2; // Radius for animal area
private ArrayList<Integer> fieldSurviveValues; // Array for survival values
private ArrayList<Integer> fieldBirthValues; // Array for birth values
private ArrayList<Agent> agents;
private ArrayList<Agent> agents; // List of agents
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
// Flags for simulation control
private boolean stopFlag; // Flag to stop simulation
private boolean pauseFlag; // Flag to pause simulation
private boolean loopingBorder; // Flag for looping border behavior
private boolean clickActionFlag; // Flag for click action
private int loopDelay = 150; // Delay between simulation steps
//TODO : add missing attribute(s)
// Additional attribute(s)
private double randomDansitySlider = 0.5; // Random density slider value
private int width; // Width of the grid
private int height; // Height of the grid
private Gride gride; // Grid object
// Constructor
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
pauseFlag=false;
loopingBorder=false;
clickActionFlag=false;
// Initializing interface object and flags
mjf = mjfParam; // Interface object
stopFlag=false; // Initializing stop flag
pauseFlag=false; // Initializing pause flag
loopingBorder=false; // Initializing looping border flag
clickActionFlag=false; // Initializing click action flag
agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
agents = new ArrayList<Agent>(); // Initializing agents list
fieldBirthValues = new ArrayList<Integer>(); // Initializing birth values array
fieldSurviveValues = new ArrayList<Integer>(); // Initializing survival values array
//TODO : add missing attribute initialization
this.width=COL_NUM; // Setting width of the grid
this.height=LINE_NUM; // Setting height of the grid
gride = new Gride(height, width, this); // Creating grid object
//Default rule : Survive always, birth never
// Initializing survival values array
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
}
// Method to get width of the grid
public int getWidth() {
//TODO : replace with proper return
return 0;
return this.width;
}
// Method to get height of the grid
public int getHeight() {
//TODO : replace with proper return
return 0;
return this.height;
}
//Should probably stay as is
// Method to run the simulation
public void run() {
// Initializing step count
int stepCount=0;
// Running simulation until stop flag is set
while(!stopFlag) {
stepCount++;
makeStep();
mjf.update(stepCount);
mjf.update(stepCount); // Updating interface
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Pausing simulation if pause flag is set
while(pauseFlag && !stopFlag) {
try {
Thread.sleep(loopDelay);
@ -79,18 +90,11 @@ public class Simulator extends Thread {
}
}
}
}
/**
* method called at each step of the simulation
* makes all the actions to go from one step to the other
*/
// Method to perform one step of simulation
public void makeStep() {
// agent behaviors first
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
// Iterating through agents
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
@ -103,73 +107,49 @@ public class Simulator extends Thread {
agents.remove(agent);
}
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
/* you should distribute this action in methods/classes
* don't write everything here !
*
* the idea is first to get the surrounding values
* then count how many are alive
* then check if that number is in the lists of rules
* if the cell is alive
* and the count is in the survive list,
* then the cell stays alive
* if the cell is not alive
* and the count is in the birth list,
* then the cell becomes alive
*/
this.applyStep(); // Applying simulation step
}
/*
* leave this as is
*/
// Method to stop simulation
public void stopSimu() {
stopFlag=true;
}
/*
* method called when clicking pause button
*/
// Method to toggle pause
public void togglePause() {
// TODO : actually toggle the corresponding flag
pauseFlag = !pauseFlag;
}
/**
* method called when clicking on a cell in the interface
*/
// Method to handle click on cell
public void clickCell(int x, int y) {
//TODO : complete method
// Checking if click action flag is true
if (clickActionFlag == true) {
int currentCellValue = getCell(x, y);
int newCellValue = 0;
// Toggling cell value
if (currentCellValue == 0) {
newCellValue = 1;
} else {
newCellValue = 0;
}
this.setCell(x, y, newCellValue);
} else {
return;
}
}
/**
* get cell value in simulated world
* @param x coordinate of cell
* @param y coordinate of cell
* @return value of cell
*/
// Method to get cell value
public int getCell(int x, int y) {
//TODO : complete method with proper return
return 0;
return this.gride.getCell(x,y).getValue();
}
/**
*
* @return list of Animals in simulated world
*/
// Method to get list of animals
public ArrayList<Agent> getAnimals(){
return agents;
}
/**
* selects Animals in a circular area of simulated world
* @param x center
* @param y center
* @param radius
* @return list of agents in area
*/
// Method to get neighboring animals
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
ArrayList<Agent> inArea = new ArrayList<Agent>();
for(int i=0;i<agents.size();i++) {
@ -181,36 +161,23 @@ public class Simulator extends Thread {
return inArea;
}
/**
* set value of cell
* @param x coord of cell
* @param y coord of cell
* @param val to set in cell
*/
// Method to set cell value
public void setCell(int x, int y, int val) {
//TODO : complete method
this.gride.getCell(x, y).setValue(val);
}
/**
*
* @return lines of file representing
* the simulated world in its present state
*/
// Method to count surrounding cells
public void countAround(int x, int y) {
this.gride.countAround(x, y);
}
// Method to get save state
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
return null;
}
/**
*
* @param lines of file representing saved world state
*/
// Method to load save state
public void loadSaveState(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
*/
if(lines.size()<=0) {
return;
}
@ -219,10 +186,6 @@ public class Simulator extends Thread {
if(firstLineElements.length<=0) {
return;
}
/*
* now we fill in the world
* with the content of the file
*/
for(int y =0; y<lines.size();y++) {
String line = lines.get(y);
String[] lineElements = line.split(";");
@ -234,62 +197,41 @@ public class Simulator extends Thread {
}
}
/**
* called by button, with slider providing the argument
* makes a new world state with random cell states
* @param chanceOfLife the chance for each cell
* to be alive in new state
*/
// Method to generate random grid
public void generateRandom(float chanceOfLife) {
//TODO : complete method
/*
* Advice :
* as you should probably have a separate class
* representing the field of cells...
* maybe just make a constructor in there
* and use it here
*/
this.gride.setRandom(chanceOfLife);
}
// Method to check looping border
public boolean isLoopingBorder() {
//TODO : complete method with proper return
return false;
return loopingBorder;
}
// Method to toggle looping border
public void toggleLoopingBorder() {
//TODO : complete method
loopingBorder = !loopingBorder;
}
// Method to set loop delay
public void setLoopDelay(int delay) {
//TODO : complete method
loopDelay = delay;
}
public void toggleClickAction() {
//TODO : complete method
}
/**
* prepare the content of a file saving present ruleSet
* as you might want to save a state,
* initialy written in this class constructor
* as a file for future use
* @return File content as an ArrayList of Lines (String)
* @see loadRule for inverse process
*/
public ArrayList<String> getRule() {
//TODO : complete method with proper return
// Method to toggle click action
public void toggleClickAction() {
clickActionFlag = !clickActionFlag;
}
// Method to get rule
public ArrayList<String> getRule() {
return null;
}
}
public void loadRule(ArrayList<String> lines) {
// Method to load rule
public void loadRule(ArrayList<String> lines) {
if(lines.size()<=0) {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
String surviveLine = lines.get(0);
String birthLine = lines.get(1);
@ -297,36 +239,60 @@ public class Simulator extends Thread {
for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x];
int value = Integer.parseInt(elem);
//TODO : add value to possible survive values
}
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
}
}
}
public ArrayList<String> getAgentsSave() {
//TODO : Same idea as the other save method, but for agents
// Method to apply simulation step
public void applyStep() {
Gride newGrideUpdated = new Gride(this.height, this.width, this);
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
int cellValue = this.gride.getCell(i, j).getValue();
int newCellValue = cellValue;
int count = this.gride.countAround(i, j);
if (cellValue == 1) {
if (count == 2 || count == 3) {
newCellValue = 1;
} else {
newCellValue = 0;
}
} else {
if (count == 3) {
newCellValue = 1;
}
}
newGrideUpdated.setCell(i, j, new Cell(newCellValue));
}
}
gride = newGrideUpdated;
}
// Method to get agents save
public ArrayList<String> getAgentsSave() {
return null;
}
}
public void loadAgents(ArrayList<String> stringArray) {
//TODO : Same idea as other load methods, but for agent list
}
/**
* used by label in interface to show the active click action
* @return String representation of click action
*/
public String clickActionName() {
// TODO : initially return "sheep" or "cell"
// depending on clickActionFlag
return "";
}
// Method to load agents
public void loadAgents(ArrayList<String> stringArray) {
}
/**
* Used by label in interface to show the active click action
* @return String representation of click action
*/
public String clickActionName() {
return "";
}
}

View File

@ -45,7 +45,7 @@ public class JPanelDraw extends JPanel {
super.paintComponent(g);
this.setBackground(Color.black);
if (mySimu != null) {
// Draw Interface from state of simulator
// Create the Interface from state of simulator
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
g.setColor(Color.gray);