Wireworld/Java: Difference between revisions
Content added Content deleted
(Moving from main task page to shorten it) |
m (Use Java 5 highlighting, whitespace, templates) |
||
Line 1: | Line 1: | ||
{{works with|Java|1.5+}} |
|||
With help from [[Conway's Game of Life#Java]]: |
With help from [[Conway's Game of Life#Java]]: |
||
<lang |
<lang java5>public class Wireworld{ |
||
public static final char empt = ' '; |
public static final char empt = ' '; |
||
public static final char head = 'h'; |
public static final char head = 'h'; |
||
Line 6: | Line 7: | ||
public static final char ctor = '.'; |
public static final char ctor = '.'; |
||
public static char[][] iterate(char[][] world) |
public static char[][] iterate(char[][] world){ |
||
char[][] nextGen = new char[world.length][world[0].length]; |
char[][] nextGen = new char[world.length][world[0].length]; |
||
for |
for(int row = 0; row < world.length; row++){//each row |
||
String thisRow = new String(world[row]); |
String thisRow = new String(world[row]); |
||
for |
for(int col = 0; col < thisRow.length(); col++){//each char in the row |
||
switch (world[row][col]) { |
switch (world[row][col]) { |
||
case head: |
case head: |
||
Line 26: | Line 27: | ||
String same = "";//neighbors in the same row |
String same = "";//neighbors in the same row |
||
String below = "";//neighbors below |
String below = "";//neighbors below |
||
if |
if(col == 0){//all the way on the left |
||
//no one above if on the top row |
//no one above if on the top row |
||
//otherwise grab the neighbors from above |
//otherwise grab the neighbors from above |
||
Line 35: | Line 36: | ||
//otherwise grab the neighbors from below |
//otherwise grab the neighbors from below |
||
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col, col + 2); |
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col, col + 2); |
||
} |
}else if (col == thisRow.length() - 1){//right |
||
//no one above if on the top row |
//no one above if on the top row |
||
//otherwise grab the neighbors from above |
//otherwise grab the neighbors from above |
||
Line 44: | Line 45: | ||
//otherwise grab the neighbors from below |
//otherwise grab the neighbors from below |
||
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col - 1, col + 1); |
below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col - 1, col + 1); |
||
} |
}else{//anywhere else |
||
//no one above if on the top row |
//no one above if on the top row |
||
//otherwise grab the neighbors from above |
//otherwise grab the neighbors from above |
||
Line 55: | Line 56: | ||
} |
} |
||
int heads = headsInNeighborhood(above, same, below); |
int heads = headsInNeighborhood(above, same, below); |
||
switch (heads) |
switch (heads){ |
||
case 1: |
case 1: |
||
case 2: |
case 2: |
||
Line 68: | Line 69: | ||
} |
} |
||
private static int headsInNeighborhood(String above, String same, String below) |
private static int headsInNeighborhood(String above, String same, String below){ |
||
int ans = 0; |
int ans = 0; |
||
if |
if(above != null){//no one above |
||
for |
for(char x : above.toCharArray()){//each neighbor from above |
||
if |
if(x == head){ |
||
ans++;//count it if a head is here |
ans++;//count it if a head is here |
||
} |
} |
||
} |
} |
||
} |
} |
||
for |
for(char x : same.toCharArray()){//two on either side |
||
if |
if(x == head){ |
||
ans++;//count it if a head is here |
ans++;//count it if a head is here |
||
} |
} |
||
} |
} |
||
if |
if(below != null){//no one below |
||
for |
for(char x : below.toCharArray()){//each neighbor below |
||
if |
if(x == head){ |
||
ans++;//count it if a head is here |
ans++;//count it if a head is here |
||
} |
} |
||
Line 92: | Line 93: | ||
} |
} |
||
public static void main(String[] args) |
public static void main(String[] args){ |
||
char[][] world = { |
char[][] world = { |
||
"th.........".toCharArray(), |
"th.........".toCharArray(), |
||
Line 100: | Line 101: | ||
"ht.. ......".toCharArray() |
"ht.. ......".toCharArray() |
||
}; |
}; |
||
for |
for(int i = 0; i <= 20; i++){ |
||
System.out.println("Iteration " + i + ":"); |
System.out.println("Iteration " + i + ":"); |
||
printWorld(world); |
printWorld(world); |
||
Line 108: | Line 109: | ||
} |
} |
||
private static void printWorld(char[][] world) |
private static void printWorld(char[][] world){ |
||
for |
for(char[] row : world){ |
||
System.out.println(row); |
System.out.println(row); |
||
} |
} |
||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre style="height:30ex;overflow:scroll">Iteration 0: |
<pre style="height:30ex;overflow:scroll">Iteration 0: |
||
th......... |
th......... |
Latest revision as of 17:48, 6 August 2014
With help from Conway's Game of Life#Java: <lang java5>public class Wireworld{
public static final char empt = ' '; public static final char head = 'h'; public static final char tail = 't'; public static final char ctor = '.';
public static char[][] iterate(char[][] world){ char[][] nextGen = new char[world.length][world[0].length]; for(int row = 0; row < world.length; row++){//each row String thisRow = new String(world[row]); for(int col = 0; col < thisRow.length(); col++){//each char in the row switch (world[row][col]) { case head: nextGen[row][col] = tail; continue; case tail: nextGen[row][col] = ctor; continue; case empt: nextGen[row][col] = empt; continue; default: } String above = "";//neighbors above String same = "";//neighbors in the same row String below = "";//neighbors below if(col == 0){//all the way on the left //no one above if on the top row //otherwise grab the neighbors from above above = (row == 0) ? null : new String(world[row - 1]).substring(col, col + 2); same = thisRow.substring(col + 1, col + 2); //no one below if on the bottom row //otherwise grab the neighbors from below below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col, col + 2); }else if (col == thisRow.length() - 1){//right //no one above if on the top row //otherwise grab the neighbors from above above = (row == 0) ? null : new String(world[row - 1]).substring(col - 1, col + 1); same = thisRow.substring(col - 1, col); //no one below if on the bottom row //otherwise grab the neighbors from below below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col - 1, col + 1); }else{//anywhere else //no one above if on the top row //otherwise grab the neighbors from above above = (row == 0) ? null : new String(world[row - 1]).substring(col - 1, col + 2); same = thisRow.substring(col - 1, col) + thisRow.substring(col + 1, col + 2); //no one below if on the bottom row //otherwise grab the neighbors from below below = (row == world.length - 1) ? null : new String(world[row + 1]).substring(col - 1, col + 2); } int heads = headsInNeighborhood(above, same, below); switch (heads){ case 1: case 2: nextGen[row][col] = head; break; default: nextGen[row][col] = ctor; } } } return nextGen; }
private static int headsInNeighborhood(String above, String same, String below){ int ans = 0; if(above != null){//no one above for(char x : above.toCharArray()){//each neighbor from above if(x == head){ ans++;//count it if a head is here } } } for(char x : same.toCharArray()){//two on either side if(x == head){ ans++;//count it if a head is here } } if(below != null){//no one below for(char x : below.toCharArray()){//each neighbor below if(x == head){ ans++;//count it if a head is here } } } return ans; }
public static void main(String[] args){ char[][] world = { "th.........".toCharArray(), ". . ".toCharArray(), " ... ".toCharArray(), ". . ".toCharArray(), "ht.. ......".toCharArray() }; for(int i = 0; i <= 20; i++){ System.out.println("Iteration " + i + ":"); printWorld(world); System.out.println(); world = iterate(world); } }
private static void printWorld(char[][] world){ for(char[] row : world){ System.out.println(row); } }
}</lang>
- Output:
Iteration 0: th......... . . ... . . ht.. ...... Iteration 1: .th........ h . ... h . t... ...... Iteration 2: h.th....... t . ... t . .h.. ...... Iteration 3: th.th...... . h ... . . hth. ...... Iteration 4: .th.th..... h t hhh h . t.th ...... Iteration 5: h.th.th.... t . ttt t . .h.t ...... Iteration 6: th.th.th... . h ... . . hth. ...... Iteration 7: .th.th.th.. h t hhh h . t.th ...... Iteration 8: h.th.th.th. t . ttt t . .h.t ...... Iteration 9: th.th.th.th . h ... . . hth. ...... Iteration 10: .th.th.th.t h t hhh h . t.th ......