This is lates version of it there is some bıg s to solve it still

This commit is contained in:
Alperen 2024-05-31 22:49:08 +02:00
parent 87fce12387
commit a1bd248d15
3 changed files with 347 additions and 389 deletions

View File

@ -4,37 +4,49 @@ import java.awt.Color;
import java.util.ArrayList;
public abstract class Agent {
protected int x;
protected int y;
protected Color color;
protected Agent(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public Color getDisplayColor() {
return color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isInArea(int x, int y, int radius) {
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 abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
protected int x;
protected int y;
protected Color color;
protected Agent(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public Color getDisplayColor() {
return color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isInArea(int x, int y, int radius) {
int diffX = this.x - x;
int diffY = this.y - y;
int dist = (int) Math.floor(Math.sqrt(diffX * diffX + diffY * diffY));
return dist < radius;
}
// Move the agent to a new position
public void move(int newX, int newY) {
this.x = newX;
this.y = newY;
// Add logic for movement restrictions or checks if necessary
}
// Interact with another agent
public abstract void interact(Agent other);
// Example method to check if the agent is at a specific position
public boolean isAtPosition(int checkX, int checkY) {
return this.x == checkX && this.y == checkY;
}
// Abstract method to be implemented by subclasses
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
// Add any additional methods or functionality below as required for the project.
}

View File

@ -4,56 +4,62 @@ import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
// example of basic animal.
// do not hesitate to make it more complex
// Example of a 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;
Random rand;
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);
// we give our sheep a hunger value of zero at birth
hunger = 0;
//we initialize the random number generator we will use to move randomly
rand = new Random();
}
/**
* action of the animal
* it can interact with the cells or with other animals
* as you wish
*/
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
} else {
hunger++;
}
this.moveRandom();
return hunger>10;
}
private void moveRandom() {
int direction = rand.nextInt(4);
if(direction == 0) {
x+=1;
}
if(direction == 1) {
y+=1;
}
if(direction == 2) {
x-=1;
}
if(direction == 3) {
y-=1;
}
}
int hunger;
Random rand = new Random();
Sheep(int x, int y) {
super(x, y, Color.WHITE); // Sheep are initially white
this.hunger = 5; // Initial hunger level
}
// Method to simulate eating behavior
public void eat() {
if (hunger > 0) {
hunger--;
}
}
// Detailed implementation of interaction with other agents, e.g., wolves
@Override
public void interact(Agent other) {
if (other instanceof Wolf) {
// Logic for being eaten by a wolf
// This could be an indicator to remove this sheep from the world
// This is a placeholder, actual removal should be handled in the simulation logic
}
}
// Retaining original liveTurn and moveRandom methods as per user's code
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(world.getCell(x, y) == 1) {
world.setCell(x, y, 0);
} else {
hunger++;
}
this.moveRandom();
return hunger > 10;
}
private void moveRandom() {
int direction = rand.nextInt(4);
if(direction == 0) {
x += 1;
}
if(direction == 1) {
y += 1;
}
if(direction == 2) {
x -= 1;
}
if(direction == 3) {
y -= 1;
}
}
// Additional methods or functionality can be added below as required for the project.
// For example, reproduction or aging
}

View File

@ -1,332 +1,272 @@
package backend;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
import windowInterface.MyInterface;
public class Simulator extends Thread {
private MyInterface mjf;
private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4;
private final int LIFE_AREA_RADIUS = 1;
private final int ANIMAL_AREA_RADIUS = 2;
private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues;
private ArrayList<Agent> agents;
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
private int[][] world; // Added missing attribute for the world grid
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;
private ArrayList<Agent> agents;
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag = false;
pauseFlag = false;
loopingBorder = false;
clickActionFlag = false;
agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
world = new int[COL_NUM][LINE_NUM]; // Initializing the world grid
//TODO : add missing attribute(s)
// Default rule: Survive always, birth never
for (int i = 0; i < 9; i++) {
fieldSurviveValues.add(i);
}
}
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
pauseFlag=false;
loopingBorder=false;
clickActionFlag=false;
public int getWidth() {
return COL_NUM;
}
agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
public int getHeight() {
return LINE_NUM;
}
//TODO : add missing attribute initialization
//Default rule : Survive always, birth never
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
}
public void run() {
int stepCount = 0;
while (!stopFlag) {
stepCount++;
makeStep();
mjf.update(stepCount);
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (pauseFlag && !stopFlag) {
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public int getWidth() {
//TODO : replace with proper return
return 0;
}
public void makeStep() {
Iterator<Agent> agentIterator = agents.iterator();
while (agentIterator.hasNext()) {
Agent agent = agentIterator.next();
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if (!agent.liveTurn(neighbors, this)) {
agentIterator.remove();
}
}
int[][] newWorld = new int[COL_NUM][LINE_NUM];
for (int i = 0; i < COL_NUM; i++) {
for (int j = 0; j < LINE_NUM; j++) {
int liveNeighbors = countLiveNeighbors(i, j);
if (world[i][j] == 1 && fieldSurviveValues.contains(liveNeighbors)) {
newWorld[i][j] = 1;
} else if (world[i][j] == 0 && fieldBirthValues.contains(liveNeighbors)) {
newWorld[i][j] = 1;
} else {
newWorld[i][j] = 0;
}
}
}
world = newWorld;
}
public int getHeight() {
//TODO : replace with proper return
return 0;
}
private int countLiveNeighbors(int x, int y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
int nx = x + i;
int ny = y + j;
if (loopingBorder) {
nx = (nx + COL_NUM) % COL_NUM;
ny = (ny + LINE_NUM) % LINE_NUM;
}
if (nx >= 0 && nx < COL_NUM && ny >= 0 && ny < LINE_NUM) {
count += world[nx][ny];
}
}
}
return count;
}
//Should probably stay as is
public void run() {
int stepCount=0;
while(!stopFlag) {
stepCount++;
makeStep();
mjf.update(stepCount);
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(pauseFlag && !stopFlag) {
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stopSimu() {
stopFlag = true;
}
}
public void togglePause() {
pauseFlag = !pauseFlag;
}
/**
* method called at each step of the simulation
* makes all the actions to go from one step to the other
*/
public void makeStep() {
// agent behaviors first
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
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
*/
}
/*
* leave this as is
*/
public void stopSimu() {
stopFlag=true;
}
/*
* method called when clicking pause button
*/
public void togglePause() {
// TODO : actually toggle the corresponding flag
}
/**
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
//TODO : complete method
}
/**
* 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 0;
}
/**
*
* @return list of Animals in simulated world
*/
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
*/
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
ArrayList<Agent> inArea = new ArrayList<Agent>();
for(int i=0;i<agents.size();i++) {
Agent agent = agents.get(i);
if(agent.isInArea(x,y,radius)) {
inArea.add(agent);
}
}
return inArea;
}
public void clickCell(int x, int y) {
if (clickActionFlag) {
// Example agent creation (Sheep) at clicked coordinates
agents.add(new Sheep(x, y));
} else {
world[x][y] = world[x][y] == 1 ? 0 : 1;
}
}
/**
* 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) {
//TODO : complete method
}
/**
*
* @return lines of file representing
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
return null;
}
/**
*
* @param lines of file representing saved world 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;
}
String firstLine = lines.get(0);
String[] firstLineElements = firstLine.split(";");
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(";");
for(int x=0; x<lineElements.length;x++) {
String elem = lineElements[x];
int value = Integer.parseInt(elem);
setCell(x, y, value);
}
}
}
public int getCell(int x, int y) {
return world[x][y];
}
/**
* 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
*/
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
*/
}
public boolean isLoopingBorder() {
//TODO : complete method with proper return
return false;
}
public void toggleLoopingBorder() {
//TODO : complete method
}
public void setLoopDelay(int delay) {
//TODO : complete method
}
public void toggleClickAction() {
//TODO : complete method
}
public ArrayList<Agent> getAnimals() {
return agents;
}
/**
* 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
return null;
}
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius) {
ArrayList<Agent> inArea = new ArrayList<Agent>();
for (Agent agent : agents) {
if (agent.isInArea(x, y, radius)) {
inArea.add(agent);
}
}
return inArea;
}
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);
String[] surviveElements = surviveLine.split(";");
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
return null;
}
public void setCell(int x, int y, int val) {
world[x][y] = val;
}
public void loadAgents(ArrayList<String> stringArray) {
//TODO : Same idea as other load methods, but for agent list
}
public ArrayList<String> getSaveState() {
ArrayList<String> lines = new ArrayList<String>();
for (int y = 0; y < LINE_NUM; y++) {
StringBuilder line = new StringBuilder();
for (int x = 0; x < COL_NUM; x++) {
line.append(world[x][y]);
if (x < COL_NUM - 1) {
line.append(";");
}
}
lines.add(line.toString());
}
return lines;
}
/**
* 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 "";
}
public void loadSaveState(ArrayList<String> lines) {
if (lines.size() <= 0) return;
String firstLine = lines.get(0);
String[] firstLineElements = firstLine.split(";");
if (firstLineElements.length <= 0) return;
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);
setCell(x, y, value);
}
}
}
public void generateRandom(float chanceOfLife) {
for (int x = 0; x < COL_NUM; x++) {
for (int y = 0; y < LINE_NUM; y++) {
world[x][y] = Math.random() < chanceOfLife ? 1 : 0;
}
}
}
public boolean isLoopingBorder() {
return loopingBorder;
}
public void toggleLoopingBorder() {
loopingBorder = !loopingBorder;
}
public void setLoopDelay(int delay) {
loopDelay = delay;
}
public void toggleClickAction() {
clickActionFlag = !clickActionFlag;
}
public ArrayList<String> getRule() {
ArrayList<String> lines = new ArrayList<String>();
StringBuilder surviveLine = new StringBuilder();
StringBuilder birthLine = new StringBuilder();
for (int i = 0; i < fieldSurviveValues.size(); i++) {
surviveLine.append(fieldSurviveValues.get(i));
if (i < fieldSurviveValues.size() - 1) {
surviveLine.append(";");
}
}
for (int i = 0; i < fieldBirthValues.size(); i++) {
birthLine.append(fieldBirthValues.get(i));
if (i < fieldBirthValues.size() - 1) {
birthLine.append(";");
}
}
lines.add(surviveLine.toString());
lines.add(birthLine.toString());
return lines;
}
public void loadRule(ArrayList<String> lines) {
if (lines.size() <= 0) {
System.out.println("empty rule file");
return;
}
fieldSurviveValues.clear();
fieldBirthValues.clear();
String surviveLine = lines.get(0);
String birthLine = lines.get(1);
String[] surviveElements = surviveLine.split(";");
for (String elem : surviveElements) {
int value = Integer.parseInt(elem);
fieldSurviveValues.add(value);
}
String[] birthElements = birthLine.split(";");
for (String elem : birthElements) {
int value = Integer.parseInt(elem);
fieldBirthValues.add(value);
}
}
public ArrayList<String> getAgentsSave() {
ArrayList<String> lines = new ArrayList<String>();
for (Agent agent : agents) {
lines.add(agent.toString());
}
return lines;
}
public void loadAgents(ArrayList<String> stringArray) {
agents.clear();
for (String line : stringArray) {
// Example agent loading (assuming the toString format contains necessary info)
String[] parts = line.split(",");
if (parts.length == 3 && parts[0].equals("Sheep")) {
agents.add(new Sheep(Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
}
}
}
public String clickActionName() {
return clickActionFlag ? "Sheep" : "Cell";
}
}