V1.1-working board (make step broken)

This commit is contained in:
lucas 2024-05-19 14:29:23 +02:00
parent f491395ade
commit 4ae11bba06
4 changed files with 175 additions and 36 deletions

View File

@ -36,7 +36,7 @@ public class Sheep extends Agent {
hunger++;
}
this.moveRandom();
return hunger>10;
return hunger<10;
}
private void moveRandom() {

View File

@ -24,6 +24,8 @@ public class Simulator extends Thread {
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
World currentWorld = new World(getWidth(), getHeight());
//TODO : add missing attribute(s)
@ -36,7 +38,7 @@ public class Simulator extends Thread {
agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
//TODO : add missing attribute initialization
@ -50,13 +52,11 @@ public class Simulator extends Thread {
}
public int getWidth() {
//TODO : replace with proper return
return 0;
return COL_NUM;
}
public int getHeight() {
//TODO : replace with proper return
return 0;
return LINE_NUM;
}
//Should probably stay as is
@ -91,7 +91,8 @@ public class Simulator extends Thread {
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
for(Agent agent : agents) {
for(int i=agents.size()-1;i>=0;i--) {
Agent agent = agents.get(i);
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
@ -103,6 +104,25 @@ public class Simulator extends Thread {
agents.remove(agent);
}
}
ArrayList<String>frame= new ArrayList<String>();
String frameLine = "";
for (int i=0;i<getWidth();i++) {
frameLine = "";
for (int j=0;j<getHeight();j++) {
if (getCell(i,j) == 0 && fieldBirthValues.contains(currentWorld.getLiving(i, j, LIFE_AREA_RADIUS, loopingBorder))) {
frameLine += ("1;");
}
if (getCell(i,j) == 1 && fieldSurviveValues.contains(currentWorld.getLiving(i, j, LIFE_AREA_RADIUS, loopingBorder))) {
frameLine += ("1;");
}
else {
frameLine += ("0;");
}
}
frame.add(frameLine);
}
loadSaveState(frame);
//then evolution of the field
// TODO : apply game rule to all cells of the field
@ -136,14 +156,29 @@ public class Simulator extends Thread {
* method called when clicking pause button
*/
public void togglePause() {
// TODO : actually toggle the corresponding flag
if (pauseFlag) {
pauseFlag = false;
} else {
pauseFlag = true;
}
}
/**
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
//TODO : complete method
if (!clickActionFlag) {
if (getCell(x,y)==0) {
setCell(x,y,1);
}
else {
setCell(x,y,0);
}
}
else {
agents.add(new Sheep(x,y));
}
}
/**
@ -152,9 +187,8 @@ public class Simulator extends Thread {
* @param y coordinate of cell
* @return value of cell
*/
public int getCell(int x, int y) {
//TODO : complete method with proper return
return 0;
public int getCell(int x, int y) {
return currentWorld.getValue(x,y);
}
/**
*
@ -188,7 +222,8 @@ public class Simulator extends Thread {
* @param val to set in cell
*/
public void setCell(int x, int y, int val) {
//TODO : complete method
currentWorld.setValue(x,y,val);
}
/**
@ -197,14 +232,22 @@ public class Simulator extends Thread {
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
return null;
ArrayList<String> outputSaveState = new ArrayList<String>();
for (int i=0; i<getWidth(); i++) {
String lineSaveState = "";
for(int j=0; j<getHeight(); j++) {
lineSaveState += (getCell(i,j)+";");
}
outputSaveState.add(lineSaveState);
}
return outputSaveState;
}
/**
*
* @param lines of file representing saved world state
*/
public void loadSaveState(ArrayList<String> lines) {
public void loadSaveState(ArrayList<String> lines) {
/*
* First some checks that the file is usable
* We call early returns in conditions like this
@ -241,14 +284,7 @@ public class Simulator extends Thread {
* 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
*/
currentWorld.randomizer(chanceOfLife);
}
public boolean isLoopingBorder() {
@ -266,11 +302,15 @@ public class Simulator extends Thread {
}
public void setLoopDelay(int delay) {
//TODO : complete method
loopDelay = delay;
}
public void toggleClickAction() {
//TODO : complete method
if (clickActionFlag) {
clickActionFlag = false;
} else {
clickActionFlag = true;
}
}
/**
@ -282,9 +322,18 @@ public class Simulator extends Thread {
* @see loadRule for inverse process
*/
public ArrayList<String> getRule() {
//TODO : complete method with proper return
return null;
ArrayList<String> outputRule = new ArrayList<String>();
String rule = "";
for (int i:fieldBirthValues) {
rule += (i+";");
}
outputRule.add(rule);
rule = "";
for (int i:fieldSurviveValues) {
rule += (i+";");
}
outputRule.add(rule);
return outputRule;
}
public void loadRule(ArrayList<String> lines) {
@ -292,7 +341,8 @@ public class Simulator extends Thread {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
fieldSurviveValues.clear();
fieldBirthValues.clear();
String surviveLine = lines.get(0);
@ -300,16 +350,15 @@ public class Simulator extends Thread {
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
int value = Integer.parseInt(elem);
fieldSurviveValues.add(value);
}
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
fieldBirthValues.add(value);
}
}
@ -328,9 +377,13 @@ public class Simulator extends Thread {
* @return String representation of click action
*/
public String clickActionName() {
if (clickActionFlag) {
return "sheep";
}
else {
return "cell";
}
// TODO : initially return "sheep" or "cell"
// depending on clickActionFlag
return "";
}
}

View File

@ -0,0 +1,35 @@
package backend;
public class Teleporter {
private int size = 0;
public Teleporter(int size) {
this.size = size;
}
public int position(int value, boolean loop) {
int out = 0;
if (loop) {
if (value>=0) {
out = value;
if (out>=size) {
out = out - size;
}
}
else {
out = size + value;
}
}
else {
if (value>=0) {
out = value;
if (out>=size) {
out = -99;
}
}
else {
out = -99;
}
}
return out;
}
}

View File

@ -0,0 +1,51 @@
package backend;
import java.util.Random;
public class World {
Random randGen = new Random();
Teleporter teleportX ;
Teleporter teleportY ;
private int[][] currentWorld;
public World(int width, int height) {
currentWorld = new int[width][height];
this.teleportX = new Teleporter(width);
this.teleportY = new Teleporter(height);
}
public void randomizer(double randValue) {
for (int i =0; i < currentWorld.length; i++) {
for (int j =0; j < currentWorld[i].length; j++) {
if (randGen.nextFloat()<randValue) {
setValue(i,j,1);
}
else {
setValue(i,j,0);
}
}
}
}
public int getValue(int x, int y) {
if (x == -99 || y == -99 ) {
return 0;
}
return currentWorld [x][y];
}
public void setValue(int x, int y, int value) {
currentWorld [x][y] = value;
}
public int getLiving(int x,int y,int radius,boolean loop) {
int out = 0;
for (int i=x-radius;i<=x+radius;i++) {
for (int j=y-radius;j<=y+radius;j++) {
if (getValue(teleportX.position(i, loop),teleportY.position(j, loop))==1) {
out ++;
}
}
}
return out;
}
}