-
-
Save jscoupreman/664419be947ded867855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.heb.esi.alg3ir.g32666.p1.core; | |
import be.heb.esi.alg3ir.g32666.p1.Config; | |
import be.heb.esi.alg3ir.g32666.p1.core.pattern.MVC.CoreInterface; | |
import be.heb.esi.alg3ir.g32666.p1.core.pattern.MVC.ViewInterface; | |
import java.awt.event.ActionListener; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.LinkedList; | |
import java.util.List; | |
import javax.swing.Timer; | |
/** | |
* | |
* @author Scoupreman Jonathan | |
*/ | |
public class Core implements CoreInterface { | |
private LifeGame lifeGame; | |
private List<ViewInterface> vues; | |
private ActionListener switchRun; | |
private Timer timer; | |
private int delay; | |
private List<File> sauvegardes; | |
private int nbLigne, nbCol; | |
public Core() { | |
nbLigne = Config.boardX; | |
nbCol = Config.boardY; | |
vues = new ArrayList<ViewInterface>(); | |
lifeGame = new LifeGame(nbLigne, nbCol); | |
switchRun = new TimerListener(this); | |
delay = Config.timer; | |
timer = new Timer(delay, switchRun); | |
updateFileList(); | |
} | |
public int getNbLigne() { | |
return nbLigne; | |
} | |
public int getNbCol() { | |
return nbCol; | |
} | |
public void setNbLigne(int n) { | |
this.nbLigne = n; | |
} | |
public void setNbCol(int n) { | |
this.nbCol = n; | |
lifeGame = new LifeGame(nbLigne, nbCol); | |
fire(); | |
} | |
public void unPas() { | |
lifeGame.updateStates(); | |
fire(); | |
} | |
public void safeToFile(String src) { | |
String os = System.getProperty("os.name").toLowerCase(); | |
String slash = ""; | |
if (os.indexOf("win") >= 0) { | |
slash += "/"; | |
} else if (os.indexOf("nux") >= 0) { | |
slash += "\\"; // ? | |
} | |
try { | |
//File file = new File("motifs/"+str); | |
FileOutputStream fos = new FileOutputStream("motifs" + slash + src); | |
DataOutputStream dos = new DataOutputStream(fos); | |
dos.writeInt(lifeGame.getX()); | |
dos.writeInt(lifeGame.getY()); | |
boolean[][] b = lifeGame.getState(); | |
for (int i = 0; i < b.length; i++) { | |
for (int j = 0; j < b[i].length; j++) { | |
dos.writeBoolean(b[i][j]); | |
} | |
} | |
dos.close(); | |
} catch (FileNotFoundException ex) { | |
System.err.println(ex.getMessage()); | |
} catch (IOException ex) { | |
System.err.println(ex.getMessage()); | |
} | |
updateFileList(); | |
} | |
private void updateFileList() { | |
File dir = new File("motifs"); | |
sauvegardes = new LinkedList<File>(Arrays.asList(dir.listFiles())); | |
fire(); | |
} | |
public List<File> getFilesList() { | |
return sauvegardes; | |
} | |
public void loadFile(File file) { | |
// charger le fichier, lire le tableau, le créer, nouveau lifeGame | |
try { | |
//File file = new File("motifs/"+str); | |
FileInputStream fos = new FileInputStream(file); | |
DataInputStream dos = new DataInputStream(fos); | |
int x = dos.readInt(); | |
int y = dos.readInt(); | |
boolean b[][] = new boolean[x][y]; | |
for (int i = 0; i < x; i++) { | |
for (int j = 0; j < y; j++) { | |
b[i][j] = dos.readBoolean(); | |
} | |
} | |
dos.close(); | |
nbLigne = x; | |
nbCol = y; | |
lifeGame = new LifeGame(b, x, y); | |
} catch (FileNotFoundException ex) { | |
System.err.println(ex.getMessage()); | |
} catch (IOException ex) { | |
System.err.println(ex.getMessage()); | |
} | |
fire(); | |
} | |
public LifeGame getGame() { | |
return lifeGame; | |
} | |
@Override | |
public void addCoreListener(ViewInterface vue) { | |
vues.add(vue); | |
} | |
@Override | |
public void removeCoreListener(ViewInterface vue) { | |
vues.remove(vue); | |
} | |
public void refreshListeners() { | |
fire(); | |
} | |
public void fire() { | |
for (ViewInterface vue : vues) { | |
vue.notifierChangement(); | |
} | |
} | |
public void start() { | |
timer.start(); | |
} | |
public void stop() { | |
timer.stop(); | |
} | |
public void setTimer(int t) { | |
delay = t; | |
} | |
public int getTimer() { | |
return delay; | |
} | |
public void updateStates() { | |
lifeGame.updateStates(); | |
fire(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment