Anonymous user
Wireworld/Java: Difference between revisions
m
Use Java 5 highlighting, whitespace, templates
(Moving from main task page to shorten it) |
m (Use Java 5 highlighting, whitespace, templates) |
||
Line 1:
{{works with|Java|1.5+}}
With help from [[Conway's Game of Life#Java]]:
<lang
public static final char empt = ' ';
public static final char head = 'h';
Line 6 ⟶ 7:
public static final char ctor = '.';
public static char[][] iterate(char[][] world)
char[][] nextGen = new char[world.length][world[0].length];
for
String thisRow = new String(world[row]);
for
switch (world[row][col]) {
case head:
Line 26 ⟶ 27:
String same = "";//neighbors in the same row
String below = "";//neighbors below
if
//no one above if on the top row
//otherwise grab the neighbors from above
Line 35 ⟶ 36:
//otherwise grab the neighbors from below
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col, col + 2);
}
//no one above if on the top row
//otherwise grab the neighbors from above
Line 44 ⟶ 45:
//otherwise grab the neighbors from below
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col - 1, col + 1);
}
//no one above if on the top row
//otherwise grab the neighbors from above
Line 55 ⟶ 56:
}
int heads = headsInNeighborhood(above, same, below);
switch (heads)
case 1:
case 2:
Line 68 ⟶ 69:
}
private static int headsInNeighborhood(String above, String same, String below)
int ans = 0;
if
for
if
ans++;//count it if a head is here
}
}
}
for
if
ans++;//count it if a head is here
}
}
if
for
if
ans++;//count it if a head is here
}
Line 92 ⟶ 93:
}
public static void main(String[] args)
char[][] world = {
"th.........".toCharArray(),
Line 100 ⟶ 101:
"ht.. ......".toCharArray()
};
for
System.out.println("Iteration " + i + ":");
printWorld(world);
Line 108 ⟶ 109:
}
private static void printWorld(char[][] world)
for
System.out.println(row);
}
}
}</lang>
{{out}}
<pre style="height:30ex;overflow:scroll">Iteration 0:
th.........
|