Skip to content

Instantly share code, notes, and snippets.

@m-242
Last active February 25, 2019 13:48
Show Gist options
  • Save m-242/0b6b93f7a3ad2ddbada598667b4ad7d3 to your computer and use it in GitHub Desktop.
Save m-242/0b6b93f7a3ad2ddbada598667b4ad7d3 to your computer and use it in GitHub Desktop.
/*
* @Author: m242
* @Date: 2019-02-25 09:41:21
* @Last Modified by: m242
* @Last Modified time: 2019-02-25 14:43:32
*/
class Automate{
// Attributs et accesseurs
private Cellule start;
private Cellule stop;
public Cellule getStart(){
return start;
}
// Constructeurs
public Automate(){
start = null;
}
public Automate(String str){
for(int i = 0; i < str.length(); i++){
Character s = str.charAt(i);
if(s.equals('#')){
this.addEnd(true);
} else if (s.equals('-')){
this.addEnd(false);
} else {
System.out.println("INCORRECT VALUE ENTERED");
System.exit(666); // For cheese
}
}
}
// Méthodes
public void initialisation(){
// state : ---###-####
this.start = null; // on réinitialise l'automate
this.addStart(true);
this.addEnd(true);
this.addEnd(true);
this.addEnd(false);
this.addEnd(false);
this.addEnd(false);
this.addEnd(true);
this.addEnd(false);
this.addEnd(false);
this.addEnd(false);
this.addEnd(false);
}
public void addStart(boolean v){
Cellule c = new Cellule(v);
if(start != null){
start.setPrecedente(c);
start = c;
} else {
start = c;
}
}
public void addEnd(boolean v){
Cellule c = new Cellule(v);
if(stop != null){
stop.setSuivante(c);
c.setPrecedente(stop);
stop = c;
} else if(start != null){
start.setSuivante(c);
c.setPrecedente(start);
stop = c;
} else {
start = c;
}
}
public void afficher(){
Cellule c = start;
while(c != stop){
c.afficher();
c = c.getSuivante();
}
c.afficher();
System.out.println();
}
public void uneEtape(){
Cellule c = start;
while(c != stop){
c.prochaineEtape();
c = c.getSuivante();
}
c.prochaineEtape();
c = start;
while(c != stop){
c.miseAJour();
c = c.getSuivante();
}
c.miseAJour();
}
public void nEtapes(int n){
this.afficher();
for(int i = 0; i < n; i++){
this.uneEtape();
this.afficher();
}
}
}
/*
* @Author: m242
* @Date: 2019-02-25 09:23:38
* @Last Modified by: m242
* @Last Modified time: 2019-02-25 14:21:35
*/
class Cellule{
// Attributs et accesseurs
private Cellule precedente;
private Cellule suivante;
private boolean noire;
private boolean prochainEtat;
public Cellule getPrecedente(){
return precedente;
}
public void setPrecedente(Cellule c){
precedente = c;
}
public Cellule getSuivante(){
return suivante;
}
public void setSuivante(Cellule c){
suivante = c;
}
public boolean getNoire(){
return noire;
}
public void setNoire(boolean b){
noire = b;
}
public boolean getProchainEtat(){
return prochainEtat;
}
public void setProchainEtat(boolean b){
prochainEtat = b;
}
// Constructeurs
public Cellule(boolean noire){
this.noire = noire;
suivante = null;
precedente = null;
prochainEtat = false;
}
// Méthodes
public void afficher(){
String c = "-";
if(noire){
c = "#";
}
System.out.print(c);
}
public void prochaineEtape(){
// on récupère les valeurs de i-1 et i+1
boolean h, j;
h = j = false; // blanc par défaut.
if(precedente != null){
h = precedente.getNoire();
}
if(suivante != null){
j = suivante.getNoire();
}
prochainEtat = !(h == j && h == noire);
}
public void miseAJour(){
noire = prochainEtat;
}
}
/*
* @Author: m242
* @Date: 2019-02-25 09:33:40
* @Last Modified by: m242
* @Last Modified time: 2019-02-25 14:45:35
*/
public class Test{
/*
public static void addEndBatch(int n, Automate a, boolean v){
// Cette fonction ajoute n cellule de valeur v à a
for(int i = 0; i<n; i++){
a.addEnd(v);
}
}
*/
public static void main(String[] args){
/*
System.out.println("***Exercice 1 :");
System.out.println("Question 1 :");
Cellule vraiePuisFausse = new Cellule(true);
Cellule vraie = new Cellule(true);
vraiePuisFausse.setNoire(false);
vraiePuisFausse.afficher();
vraie.afficher();
*/
/*
Cellule c1 = new Cellule(false);
Automate a1 = new Automate(c1);
addEndBatch(2, a1, false);
addEndBatch(3, a1, true);
a1.addEnd(false);
addEndBatch(4, a1, true);
a1.afficher();
*/
/*
Automate a2 = new Automate();
a2.initialisation();
a2.afficher();
a2.uneEtape();
a2.afficher();
*/
Automate a3 = new Automate("-##-----##-");
a3.nEtapes(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment