refining files

This commit is contained in:
maxim 2024-05-24 13:58:46 +02:00
parent 0b39cc2410
commit bde23cb19f
3 changed files with 57 additions and 27 deletions

View File

@ -1,3 +1,5 @@
//Importing the graphical interface
import windowInterface.MyInterface;

View File

@ -4,10 +4,6 @@ import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
// 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 {
int hunger;
@ -30,11 +26,14 @@ public class Sheep extends Agent {
* as you wish
*/
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
//if the sheep is on a cell with value 1 (food) it eats and cell becomes empty of food
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
} else {
//if there is no food, increase the hunger
hunger++;
}
//moves the sheep randomly
this.moveRandom();
return hunger>10;
}

View File

@ -1,19 +1,23 @@
package backend;
import java.util.ArrayList;
import windowInterface.MyInterface;
public class Simulator extends Thread {
private MyInterface mjf;
//Defining the grid size and life parameters
private final int COL_NUM = 50;
private final int LINE_NUM = 50;
private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
//Conway Radius
private final int LIFE_AREA_RADIUS = 1;
//Animal Neighborhood Radius : 5
//Animal Neighborhood Radius
private final int ANIMAL_AREA_RADIUS = 2;
//List to hold the rules
private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues;
@ -25,13 +29,16 @@ public class Simulator extends Thread {
private boolean clickActionFlag;
private int loopDelay = 150;
//TODO : add missing attribute(s)
//TODO: Add missing attributes :
//Creating the grid
private double randomDensitySlider = 0.5;
private int width;
private int height;
private Grid grid;
//Initializing the Simulation
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
@ -43,7 +50,8 @@ public class Simulator extends Thread {
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
//TODO : add missing attribute initialization
//TODO : Add missing attribute initialization
this.width=COL_NUM;
this.height=LINE_NUM;
@ -56,11 +64,13 @@ public class Simulator extends Thread {
}
//Getting the width of grid
public int getWidth() {
//TODO : replace with proper return
return this.width;
}
//Getting the height of grid
public int getHeight() {
//TODO : replace with proper return
return this.height;
@ -94,6 +104,8 @@ public class Simulator extends Thread {
* makes all the actions to go from one step to the other
*/
public void makeStep() {
//Updating the agent:
// agent behaviors first
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
@ -112,7 +124,9 @@ public class Simulator extends Thread {
}
this.applyStep();
//then evolution of the field
// TODO : apply game rule to all cells 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 !
@ -128,14 +142,13 @@ public class Simulator extends Thread {
* then the cell becomes alive
*/
}
/*
* leave this as is
*/
//Stopping the Simulation
public void stopSimu() {
stopFlag=true;
}
@ -143,16 +156,23 @@ public class Simulator extends Thread {
/*
* method called when clicking pause button
*/
//Pausing the Simulation
public void togglePause() {
// TODO : actually toggle the corresponding flag
// TODO : actually toggle the corresponding flag :
pauseFlag = !pauseFlag;
}
/**
* method called when clicking on a cell in the interface
*/
//Handling of the cell clicks in the interface
public void clickCell(int x, int y) {
//TODO : complete method
//TODO : complete method :
if (clickActionFlag == true) {
int currentCellValue = getCell(x, y);
int newCellValue = 0;
@ -174,8 +194,12 @@ public class Simulator extends Thread {
* @param y coordinate of cell
* @return value of cell
*/
//Getting the cell value
public int getCell(int x, int y) {
//TODO : complete method with proper return
//TODO : complete method with proper return:
return this.grid.getCell(x,y).getValue();
}
/**
@ -192,6 +216,8 @@ public class Simulator extends Thread {
* @param radius
* @return list of agents in area
*/
//Getting the agent in the circular area
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
ArrayList<Agent> inArea = new ArrayList<Agent>();
for(int i=0;i<agents.size();i++) {
@ -209,6 +235,8 @@ public class Simulator extends Thread {
* @param y coord of cell
* @param val to set in cell
*/
//Setting the value of a cell in the grid
public void setCell(int x, int y, int val) {
this.grid.getCell(x, y).setValue(val);
}
@ -218,6 +246,8 @@ public class Simulator extends Thread {
* @return lines of file representing
* the simulated world in its present state
*/
//Counting the surrounding cells
public void countAround(int x, int y) {
this.grid.countAround(x, y);
}
@ -266,6 +296,8 @@ public class Simulator extends Thread {
* @param chanceOfLife the chance for each cell
* to be alive in new state
*/
//Generating a random world state
public void generateRandom(float chanceOfLife) {
//TODO : complete method
/*
@ -278,12 +310,12 @@ public class Simulator extends Thread {
this.grid.setRandom(chanceOfLife);
}
//Returns if borders loop
public boolean isLoopingBorder() {
//TODO : complete method with proper return
return loopingBorder;
}
public void toggleLoopingBorder() {
//TODO : complete method
loopingBorder = !loopingBorder;
@ -310,7 +342,8 @@ public class Simulator extends Thread {
* @see loadRule for inverse process
*/
public ArrayList<String> getRule() {
//TODO : complete method with proper return
//TODO : complete method with proper return :
return null;
}
@ -320,7 +353,7 @@ public class Simulator extends Thread {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
String surviveLine = lines.get(0);
@ -329,19 +362,19 @@ 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
}
}
//Applying the simulation step to the grid
public void applyStep() {
Grid newGridUpdated = new Grid(this.height, this.width, this);
System.out.println("New Iteration :");
@ -372,12 +405,10 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() {
//TODO : Same idea as the other save method, but for agents
return null;
}
public void loadAgents(ArrayList<String> stringArray) {
//TODO : Same idea as other load methods, but for agent list
}
@ -386,8 +417,6 @@ public class Simulator extends Thread {
* @return String representation of click action
*/
public String clickActionName() {
// TODO : initially return "sheep" or "cell"
// depending on clickActionFlag
return "";
}