tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,42 @@
|
|||
public class Life{
|
||||
public static void main(String[] args) throws Exception{
|
||||
String start= "_###_##_#_#_#_#__#__";
|
||||
int numGens = 10;
|
||||
for(int i= 0; i < numGens; i++){
|
||||
System.out.println("Generation " + i + ": " + start);
|
||||
start= life(start);
|
||||
}
|
||||
}
|
||||
|
||||
public static String life(String lastGen){
|
||||
String newGen= "";
|
||||
for(int i= 0; i < lastGen.length(); i++){
|
||||
int neighbors= 0;
|
||||
if (i == 0){//left edge
|
||||
neighbors= lastGen.charAt(1) == '#' ? 1 : 0;
|
||||
} else if (i == lastGen.length() - 1){//right edge
|
||||
neighbors= lastGen.charAt(i - 1) == '#' ? 1 : 0;
|
||||
} else{//middle
|
||||
neighbors= getNeighbors(lastGen.substring(i - 1, i + 2));
|
||||
}
|
||||
|
||||
if (neighbors == 0){//dies or stays dead with no neighbors
|
||||
newGen+= "_";
|
||||
}
|
||||
if (neighbors == 1){//stays with one neighbor
|
||||
newGen+= lastGen.charAt(i);
|
||||
}
|
||||
if (neighbors == 2){//flips with two neighbors
|
||||
newGen+= lastGen.charAt(i) == '#' ? "_" : "#";
|
||||
}
|
||||
}
|
||||
return newGen;
|
||||
}
|
||||
|
||||
public static int getNeighbors(String group){
|
||||
int ans= 0;
|
||||
if (group.charAt(0) == '#') ans++;
|
||||
if (group.charAt(2) == '#') ans++;
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue