ADN Project — DUT Informatique Semestre 1 - Travaux dirigés dalgorithmique.mhtml
ADN Project — DUT Informatique Semestre 1 - Travaux dirigés dalgorithmique
import java.util.Scanner;
import java.util.Arrays;
public class Adn {
final static Scanner input = new Scanner(System.in);
final static int NPOP = 10;
final static int NDISHES = 40;
// déclaration du tableau des symboles T_SYMS
final static char[] T_SYMS = {' ', '.', 'x', 'W'};
public static void afficheState(String s, int[] tabDens, char[] tSyms){
System.out.print(s + "\\t : [ ");
for (int i=0; i<tabDens.length; i++){
System.out.print(tSyms[tabDens[i]] + " ");
}
System.out.println("]");
}
public static int[] getDnaProgram(String s){
int[] dna = new int[s.length()];
for(int i=0; i<s.length(); i++)
dna[i] = Character.getNumericValue(s.charAt(i));
return dna;
}
public static int[] computeNextGen(int[] curGen, int[] dnaProg){
int[] nextGen = new int[curGen.length];
int sumDens;
// calcul général pour les cultures sauf celles des bords
for(int i=1; i<curGen.length-1; i++){
sumDens = curGen[i-1] + curGen[i] + curGen[i+1];
nextGen[i] = dnaProg[sumDens];
}
// culture n°1
sumDens = curGen[0] + curGen[1];
nextGen[0] = dnaProg[sumDens];
//culture n°NDISHES (copilot)
sumDens = curGen[NDISHES-2] + curGen[NDISHES-1];
nextGen[NDISHES-1] = dnaProg[sumDens];
return nextGen;
}
public static int[] initDishes(int n){
int[] tCultures = new int[n];
tCultures[(n-1)/2] = 1;
return tCultures;
}
public static void main(String[] args) {
// current and next states
int[] tCurstates = initDishes(NDISHES);
int[] tNextStates = new int[NDISHES];
System.out.println("Enter the DNA program string below ");
int[] dnaProg = getDnaProgram(input.nextLine());
System.out.println("How many generations to compute ?");
final int NGEN = Integer.parseInt(input.next());
System.out.println("Evolution for DNA programm "+" "+Arrays.toString(dnaProg));
afficheState("0\\t", tCurstates, T_SYMS);
// calcul et affichage des états des générations
for(int gen=1; gen<=NGEN; gen++){
tNextStates = computeNextGen(tCurstates, dnaProg);
afficheState(String.valueOf(gen), tNextStates, T_SYMS);
tCurstates = tNextStates;
}
}
}