This commit is contained in:
Timéo 2024-05-29 19:54:41 +02:00
commit 65d2baabfd
3 changed files with 65 additions and 18 deletions

2
Agents/test1 Normal file
View File

@ -0,0 +1,2 @@
1,46;17,61;17,57;33,
2,

View File

@ -1,2 +1,2 @@
1;3;5;8
3;5;7;
3;5;7

1 1 1;3;5;8 3 5 8
2 3 3;5;7 5 7

View File

@ -178,11 +178,11 @@ public class Simulator extends Thread {
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);}
//if(!agent.liveTurn(
// neighbors,
//this)) {
//agents.remove(agent);
//{
/*if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent);
{*/
// Apply Game of Life rules
@ -267,7 +267,29 @@ public class Simulator extends Thread {
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
if (clickActionFlag==0) { // cell
setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
} else if (clickActionFlag==1) { // sheep
ArrayList<Agent> nearby=getNeighboringAnimals(x,y,1); //look if the cell has an agent
if (nearby.isEmpty()) {
Sheep sheep = new Sheep(x,y);
agents.add(sheep);
}else {
for (Agent animal:nearby) {
agents.remove(animal);
}
}
} else if (clickActionFlag==2) { // wolf
ArrayList<Agent> nearby=getNeighboringAnimals(x,y,1);
if (nearby.isEmpty()) {
Sheep sheep = new Sheep(x,y);
agents.add(sheep);
}else {
for (Agent animal:nearby) {
agents.remove(animal);
}
}
}
}
@ -443,23 +465,46 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() {
ArrayList<String> agentsSave = new ArrayList<>();
for (int j = 0; j < getHeight(); j++) {
StringBuilder lineState = new StringBuilder();
for (int i = 0 ; i < getWidth() ; i++) {
lineState.append(getCell(i, j));
if (j < getHeight() -1) {
lineState.append(",");
ArrayList<String> agentsList = new ArrayList<String>();
String sheepLine = "1,";
String wolfLine = "2,";
for (Agent agent : agents) {
int x = agent.getX();
int y = agent.getY();
if (agent instanceof Sheep)
{
sheepLine = sheepLine + x + ";" + y + ",";
}
else if (agent instanceof Wolf)
{
wolfLine = wolfLine + x + ";" + y + ",";
}
}
agentsSave.add(lineState.toString());
}
return agentsSave;
agentsList.add(sheepLine);
agentsList.add(wolfLine);
return agentsList;
}
public void loadAgents(ArrayList<String> stringArray) {
//TODO : Same idea as other load methods, but for agent list
for(int y =0; y<stringArray.size();y++) {
String line = stringArray.get(y);
String[] lineElements = line.split(",");
int agentType = Integer.parseInt(lineElements[0]);
for(int x=1; x<lineElements.length;x++) {
String[] coordinates = lineElements[x].split(";");
int i = Integer.parseInt(coordinates[0]);
int j = Integer.parseInt(coordinates[1]);
if (agentType == 1) {
Sheep newSheep = new Sheep(i, j);
agents.add(newSheep);
}
if (agentType == 2) {
Wolf newWolf = new Wolf(i, j);
agents.add(newWolf);
}
}
}
}
/**