This commit is contained in:
Astatin3
2024-03-26 14:07:09 -06:00
parent f8d9da826c
commit f8d5729e52
24 changed files with 4318 additions and 0 deletions
+85
View File
@@ -0,0 +1,85 @@
import org.code.neighborhood.*;
public class ConwayRunner {
public int mod(int n) {
int o = n % 8;
if(o < 0){
return 8+o;
}
return o;
}
public int isAlive(String color) {
boolean isAlive = (color == "White" || color == "1");
if(isAlive){
return 1;
}else{
return 0;
}
}
public void run(Renderer r, String[][] state) {
for(String[] line : state){
System.out.println(String.join("",line));
}
r.render(state);
this.iterate(r, state);
}
public void iterate(Renderer r, String[][] initState) {
int index1 = 0;
String[][] newState = new String[8][8];
for(String[] line : initState) {
int index2 = 0;
for(String cell : line) {
String cell1 = initState[this.mod(index1-1)][this.mod(index2-1)];
String cell2 = initState[this.mod(index1-1)][this.mod(index2)];
String cell3 = initState[this.mod(index1-1)][this.mod(index2+1)];
String cell4 = initState[this.mod(index1)][this.mod(index2-1)];
String cell5 = initState[this.mod(index1)][this.mod(index2+1)];
String cell6 = initState[this.mod(index1+1)][this.mod(index2-1)];
String cell7 = initState[this.mod(index1+1)][this.mod(index2)];
String cell8 = initState[this.mod(index1+1)][this.mod(index2+1)];
boolean isAlive = (cell == "White");
int count = this.isAlive(cell1)+
this.isAlive(cell2)+
this.isAlive(cell3)+
this.isAlive(cell4)+
this.isAlive(cell5)+
this.isAlive(cell6)+
this.isAlive(cell7)+
this.isAlive(cell8);
String newColor = new String();
if(isAlive && count < 2){
newColor = "Black";
}else if(isAlive && count == 2 || count == 3){
newColor = "White";
}else if(isAlive && count > 3){
newColor = "Black";
}else if(!isAlive && count == 3){
newColor = "White";
}else if(!isAlive){
newColor = "Black";
}
// System.out.println(count);
// newLine.set(index2, newColor);
newState[index1][index2] = (newColor);
index2++;
}
index1++;
// newState.set(index1, newLine);
}
this.run(r, newState);
}
}
+25
View File
@@ -0,0 +1,25 @@
import org.code.neighborhood.*;
public class NeighborhoodRunner {
public static void main(String[] args) {
String I = "White";
String O = "Black";
String[][] initState = new String[][] {
new String[] {O,O,O,O,O,O,O,O},
new String[] {O,I,I,O,O,O,O,O},
new String[] {O,I,O,I,O,O,O,O},
new String[] {O,I,O,O,O,O,O,O},
new String[] {O,O,O,O,O,O,O,O},
new String[] {O,O,O,O,O,I,I,O},
new String[] {O,O,O,O,I,O,I,O},
new String[] {O,O,O,O,O,O,I,O}};
ConwayRunner cw = new ConwayRunner();
Renderer r = new Renderer();
cw.run(r, initState);
cw.run(r, initState);
}
}
+68
View File
@@ -0,0 +1,68 @@
import org.code.neighborhood.*;
/*
* MuralPainter is a PainterPlus that paints
* murals in The Neighborhood
*/
public class Renderer extends Painter {
public void render(String[][] mural) {
this.setPaint((8*8));
int index1 = 0;
for(String[] line : mural){
// System.out.println(line[0].toString());
int index2 = 0;
for(String color : line){
// System.out.println(color);
if(!this.isOnPaint()){
this.paint(color);
}else if(this.isOnPaint() && this.getColor() != color){
this.scrapePaint();
this.paint(color);
}
if(index2 != line.length-1){
this.move();
}
index2++;
}
if(index1 != mural.length-1){
this.turnLeft();
this.turnLeft();
this.turnLeft();
this.move();
this.turnLeft();
this.turnLeft();
this.turnLeft();
for(int i=0;i<line.length-1;i++){
this.move();
}
this.turnLeft();
this.turnLeft();
}else{
this.turnLeft();
for(int i=0;i<mural.length-1;i++){
this.move();
}
this.turnLeft();
for(int i=0;i<line.length-1;i++){
this.move();
}
this.turnLeft();
this.turnLeft();
}
index1++;
}
}
}