Annonce

>>> Bienvenue sur codelab! >>> Première visite ? >>> quelques mots sur codelab //// une carte des membres//// (apéros) codelab


#1 2018-02-21 14:00:38 Problème cellular automata

Cocoht
nouveau membre
Date d'inscription: 2017-11-26
Messages: 2

Problème cellular automata



Salut tout le monde,

j'aimerais bien, pour les cours générer différents automatismes cellulaires un peu comme "the game of life" (mais celui ci existe déjà et je trouve qu'il est compliqué de modifier le code)
J'ai tenté un nouveau truc par rapport à ce que j'ai vu sur internet, j'ai eu beau chercher une erreur, je ne trouve pas; pourtant il est écrit "expecting EOF, found 'for'"

je vous mets le code (il y a deux onglets) si vous pouviez m'aider à trouver la solution ça serait top! Merci

//premier onglet

class Cell {
}
int [] cells = {1,0,1,0,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,0};

// draw the array
for (int i = 0; i < cells.length; i++) {
  if (cells[i] == 0) fill (255);
  else fill(0);
  stroke(0);
  rect(i*50,0,50,50);
}


// for every cell in the array //1
for (int i = 0; i < cells.length; i++) { //for every cell in the array
  //the neighborhood
  int left = cell[i-1];
  int middle = cell[i];
  int right = cell[i+1];
  int newstate = rules(left,middle,right); //look up the new value according to the rules
  cell[i] = newstate; //set the cell's state to the new value
}

for (int i = 1; i <cells.length-1; i++) { //2 a loop that ignores the first and the last cell
  int left = cell[i-1];
  int middle = cell[i];
  int right = cell[i+1];
  int newstate = rules(left, middle, right);
  newcells[i] = newstate; // saving the new wtate in the new array
}
  cells [i] = newstate;
  cells [i] = newstate;
 
  int[] newcells = int new int[cells.length]; //another array to store the states for the next generation
  for (int i = 1; i < cells.length-1; i++) { //3
  int left = cell[i-1]; //look at the states from the current array
  int middle = cell[i];
  int right = cell[i+1];
  int newstate = rules(left, middle, right);
  newcells[i] = newstate; //saving the new state in the new array
  }
 
  cells = newcells; //the new generation becomes the current generation
 
int rules (int a, int b, int c) { //function receives 3 ints and returns 1
int [] ruleset = {0,1,0,1,1,0,1,0};
if (a == 1 && b == 1 && c == 1) return ruleset [0];
int rules (int a, int b, int c) {
  if      (a == 1 && b == 1 && c == 1) return ruleset[0];
  else if (a == 1 && b == 1 && c == 0) return ruleset[1];
  else if (a == 1 && b == 0 && c == 1) return ruleset[2];
  else if (a == 1 && b == 0 && c == 0) return ruleset[3];
  else if (a == 0 && b == 1 && c == 1) return ruleset[4];
  else if (a == 0 && b == 1 && c == 0) return ruleset[5];
  else if (a == 0 && b == 0 && c == 1) return ruleset[6];
  else if (a == 0 && b == 0 && c == 0) return ruleset[7];
 
  return 0;
}

int rules (int a, int b, int c) {
  String s = "" + a + b + c; //a quick way to join three bits into a String
  int index = Integer.parseInt(s,2); // the second argument "2" indicates that we intend to parse a binary number
  return ruleset[index];
}

int[] ruleset = {1,1,0,1,1,1,1,0}; //rule 222
if (a == 1 && b==1 && c==1) return ruleset[];
int[] ruleset = {0,1,1,1,1,0,1,1}; //rule 222 in reverse order



//deuxième onglet
class CA {
  int[] cells; //we need an array for the cells and one for the rules
  int ruleset;
 
  CA() {
    cells = new int[width];
    ruleset = {0,1,0,1,1,0,1,0};  //arbitrarily starting with the rule 90
   
    for (int i = 0; i < cells.length; i++) {
      cells[i] = 0;
    }
    cells[cells.length/2] = 1; //all cells start with state 0, except the center cell state 1
    }
   
    void generate() {
      //compute the next generation
      int[] nextgen = new int[cells.length];
      for (int i = 1; i < cells.length-1; i++) {
        int left = cells[i-1];
        int me = cells [i]
        int right = cells [i+1];
        nextgen[i] = rules(left, me, right);
      }
      cells = nextgen;
    }
   
    //look up a new state from the ruleset
    int rules (int a, int b, int c) {
      String s = "" + a + b + c;
      int index = Integer.parseInt(s,2);
      return ruleset[index];
    }
  }
 
  int w = 10;
  int[] cells = new int [width/w]; //how many cells fit across given a certain width
  for(int i = 0; i<cells.length; i++) {
    //black or white fill?
    if (cells[i] == 1) fill(0);
    else               fill (255);
    rect(i*w,0,w,w); //notice how the x-location is the cell index times the cell width. In the above scenario, this would give us cells located at x equals 0,10,20,39, aoo the way up to 600
  }
   
}

Hors ligne

 

#2 2018-02-21 16:28:33 Re : Problème cellular automata

Mushussu
membre
Lieu: Orléans
Date d'inscription: 2012-05-24
Messages: 802

Re: Problème cellular automata



Bonjour,

Le code que tu publies est bourré d'erreurs.
Il est difficile de comprendre ce que tu souhaites faire. Il y a un grand danger de rendre le code incompréhensible en réalisant des copier/coller trouvés sur internet.

Le principal soucis est qu'il n'y a pas de fonction setup() ni draw().

Je ne peux malheureusement pas débloquer la situation.

Dis-nous où tu as trouvé le code pour que nous puissions t'aider ou alors simplifier le tout.

Bon courage.

Hors ligne

 

fil rss de cette discussion : rss

Pied de page des forums

Powered by FluxBB

codelab, graphisme & code : emoc / 2008-2024