From 7c2279216e2267e2c2479cf57649dc32a6e8bb8e Mon Sep 17 00:00:00 2001 From: Raphaelsav <94864277+Raphaelsav@users.noreply.github.com> Date: Wed, 10 Apr 2024 14:36:58 +0200 Subject: [PATCH] deleted 2 packages --- src/src/backend/Agent.java | 40 --- src/src/backend/Sheep.java | 59 ---- src/src/backend/Simulator.java | 332 ------------------- src/src/windowInterface/JPanelDraw.java | 94 ------ src/src/windowInterface/MyInterface.java | 393 ----------------------- 5 files changed, 918 deletions(-) delete mode 100644 src/src/backend/Agent.java delete mode 100644 src/src/backend/Sheep.java delete mode 100644 src/src/backend/Simulator.java delete mode 100644 src/src/windowInterface/JPanelDraw.java delete mode 100644 src/src/windowInterface/MyInterface.java diff --git a/src/src/backend/Agent.java b/src/src/backend/Agent.java deleted file mode 100644 index 9eeef1e..0000000 --- a/src/src/backend/Agent.java +++ /dev/null @@ -1,40 +0,0 @@ -package backend; - -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 neighbors, Simulator world); - - -} diff --git a/src/src/backend/Sheep.java b/src/src/backend/Sheep.java deleted file mode 100644 index b05d141..0000000 --- a/src/src/backend/Sheep.java +++ /dev/null @@ -1,59 +0,0 @@ -package backend; - -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; - 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 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; - } - } - - -} diff --git a/src/src/backend/Simulator.java b/src/src/backend/Simulator.java deleted file mode 100644 index 52b5f9f..0000000 --- a/src/src/backend/Simulator.java +++ /dev/null @@ -1,332 +0,0 @@ -package backend; -import java.util.ArrayList; - -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; - //Conway Radius : 1 - private final int LIFE_AREA_RADIUS = 1; - //Animal Neighborhood Radius : 5 - private final int ANIMAL_AREA_RADIUS = 2; - private ArrayList fieldSurviveValues; - private ArrayList fieldBirthValues; - - private ArrayList agents; - - private boolean stopFlag; - private boolean pauseFlag; - private boolean loopingBorder; - private boolean clickActionFlag; - private int loopDelay = 150; - - //TODO : add missing attribute(s) - - public Simulator(MyInterface mjfParam) { - mjf = mjfParam; - stopFlag=false; - pauseFlag=false; - loopingBorder=false; - clickActionFlag=false; - - agents = new ArrayList(); - fieldBirthValues = new ArrayList(); - fieldSurviveValues = new ArrayList(); - - //TODO : add missing attribute initialization - - - - //Default rule : Survive always, birth never - for(int i =0; i<9; i++) { - fieldSurviveValues.add(i); - } - - } - - public int getWidth() { - //TODO : replace with proper return - return 0; - } - - public int getHeight() { - //TODO : replace with proper return - return 0; - } - - //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(); - } - } - } - - } - - /** - * 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 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 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 getNeighboringAnimals(int x, int y, int radius){ - ArrayList inArea = new ArrayList(); - for(int i=0;i getSaveState() { - //TODO : complete method with proper return - return null; - } - /** - * - * @param lines of file representing saved world state - */ - public void loadSaveState(ArrayList 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 getRule() { - //TODO : complete method with proper return - - return null; - } - - public void loadRule(ArrayList 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 getAgentsSave() { - //TODO : Same idea as the other save method, but for agents - return null; - } - - public void loadAgents(ArrayList 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 ""; - } - -} diff --git a/src/src/windowInterface/JPanelDraw.java b/src/src/windowInterface/JPanelDraw.java deleted file mode 100644 index de9fede..0000000 --- a/src/src/windowInterface/JPanelDraw.java +++ /dev/null @@ -1,94 +0,0 @@ -package windowInterface; - -import java.awt.Color; -import java.awt.Graphics; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.ArrayList; - -import javax.swing.JPanel; - -import backend.Agent; -import backend.Simulator; - -public class JPanelDraw extends JPanel { - - private static final long serialVersionUID = 1L; - private Simulator mySimu; - private MyInterface interfaceGlobal; - - public JPanelDraw(MyInterface itf) { - super(); - mySimu = null; - interfaceGlobal = itf; - addMouseListener(new MouseAdapter() { - public void mousePressed(MouseEvent me) { - // System.out.println(me); - if(mySimu == null) { - interfaceGlobal.instantiateSimu(); - } - int x = (me.getX()*mySimu.getWidth())/getWidth(); - int y = (me.getY()*mySimu.getHeight())/getHeight(); - mySimu.clickCell(x,y); - repaint(); - } - }); - } - - - public void setSimu(Simulator simu) { - mySimu = simu; - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - this.setBackground(Color.black); - if (mySimu != null) { - // Draw 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); - for(int x=0; x stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - if(mySimu != null) { - mySimu.stopSimu(); - this.eraseLabels(); - } - loadedSim.loadSaveState(stringArray); - mySimu = loadedSim; - panelDraw.setSimu(mySimu); - this.repaint(); - } - } - - public void clicLoadRuleFileButton() { - String fileName=SelectFile(); - ArrayList stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - mySimu.loadRule(stringArray); - this.repaint(); - } - } - - public void clicLoadAgentsFileButton() { - String fileName=SelectFile(); - ArrayList stringArray = new ArrayList(); - if (fileName.length()>0) { - try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); - } - fileContent.close(); - } catch (Exception e) { - e.printStackTrace(); - } - mySimu.loadAgents(stringArray); - this.repaint(); - } - } - - - public void clicSaveToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getSaveState(); - writeFile(fileName, (String[]) content.toArray()); - } - } - - public void clicSaveRuleToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getRule(); - writeFile(fileName, (String[]) content.toArray()); - } - } - - public void clicSaveAgentsToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { - ArrayList content = mySimu.getAgentsSave(); - writeFile(fileName, (String[]) content.toArray()); - } - } - - - public String SelectFile() { - String s; - JFileChooser chooser = new JFileChooser(); - chooser.setCurrentDirectory(new java.io.File(".")); - chooser.setDialogTitle("Choose a file"); - chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - chooser.setAcceptAllFileFilterUsed(true); - if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { - s=chooser.getSelectedFile().toString(); - } else { - System.out.println("No Selection "); - s=""; - } - return s; - } - - public void writeFile(String fileName, String[] content) { - FileWriter csvWriter; - try { - csvWriter = new FileWriter(fileName); - for (String row : content) { - csvWriter.append(row); - csvWriter.append("\n"); - } - csvWriter.flush(); - csvWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void update (int stepCount) { - this.setStepBanner("Step : "+ stepCount); - this.repaint(); - } - - public void eraseLabels() { - this.setStepBanner("Step : X"); - this.setBorderBanner("border : X"); - this.setClickBanner("click : X"); - speedSlider.setValue(3); - } - -}