Переглянути джерело

Better patch.toString(), no threading, test

master
Josh Hofing 12 роки тому
джерело
коміт
6010ad0fbe
3 змінених файлів з 86 додано та 68 видалено
  1. +12
    -7
      src/Goldfish.java
  2. +1
    -1
      src/Patch.java
  3. +73
    -60
      src/rules/Conway.java

+ 12
- 7
src/Goldfish.java Переглянути файл

@@ -15,15 +15,20 @@ public class Goldfish {

public void run () {
//TODO: make it run.
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 3; j++) {
_grid.getPatch(i,j).setState(1);
}
}
_grid.getPatch(0,1).setState(1);
_grid.getPatch(1,2).setState(1);
_grid.getPatch(2,2).setState(1);
_grid.getPatch(2,1).setState(1);
_grid.getPatch(2,0).setState(1);
System.out.println(_grid);

_grid = Conway.run(_grid);

_grid = Conway.run(_grid);
_grid = Conway.run(_grid);

_grid = Conway.run(_grid);
System.out.println("------------");
System.out.println(_grid);
}

public static void main (String[] args) {


+ 1
- 1
src/Patch.java Переглянути файл

@@ -46,7 +46,7 @@ public class Patch {
}

public String toString() {
return _label + ", " + _state;
return _label + " " + ((_state == 1) ? "1" : ".");
}

public Patch[] get4Neighbors() {


+ 73
- 60
src/rules/Conway.java Переглянути файл

@@ -7,78 +7,91 @@ public class Conway extends RuleSet {
public static int states = 2;

public static Grid run (Grid g) {
Thread[] threads = new Thread[g.getWidth()];
//Thread[] threads = new Thread[g.getWidth()];
Grid newGrid = new Grid(g.getWidth(), g.getHeight());
for (int i = 0; i < g.getWidth(); i++) {
Conway.rowThread rt = new Conway.rowThread(g, newGrid, i);
Thread t = new Thread(rt);
rt.setThread(t);
threads[i] = t;
t.run();
//for (int j = 0; j < g.getHeight(); j++) {
//Patch[] neighbors = g.getPatch(i, j).get8Neighbors();
//int numAlive = 0;
//for (Patch p : neighbors)
//if (p.getState() == 1) numAlive++;
//Patch p = g.getPatch(i,j).clone();
//if (numAlive < 2) {
//p.setState(0); //Dies by underpopulation
//}
//if (numAlive > 3) {
//p.setState(0); //Dies by overpopulation
//}
//if (numAlive == 3)
//p.setState(1); //Born with 3 neighbors.
//newGrid.setPatch(i,j,p);
//}
}
boolean running = true;
while (running) {
running = false;
for (Thread t : threads) {
if (t.isAlive()) running = true;
}
}
return newGrid;
}


public static class rowThread implements Runnable {
private Grid _original, _newGrid;
private int _rowNum;
private Thread _thread;

public rowThread (Grid orig, Grid newGrid, int rowNum) {
_original = orig;
_newGrid = newGrid;
_rowNum = rowNum;
}
public void setThread (Thread t) {
_thread = t;
}

public void run () {
for (int j = 0; j < _original.getHeight(); j++) {
Patch[] neighbors = _original.getPatch(_rowNum, j).get8Neighbors();
//Conway.rowThread rt = new Conway.rowThread(g, newGrid, i);
//Thread t = new Thread(rt);
//rt.setThread(t);
//threads[i] = t;
//t.run();
for (int j = 0; j < g.getHeight(); j++) {
Patch[] neighbors = g.getPatch(i, j).get8Neighbors();
int numAlive = 0;
for (Patch p : neighbors)
if (p.getState() == 1) numAlive++;
Patch p = _original.getPatch(_rowNum,j).clone();
for (Patch neighbor : neighbors)
if (neighbor.getState() == 1) numAlive++;
//System.out.println(numAlive);
Patch p = g.getPatch(i,j).clone(newGrid);
if (numAlive < 2) {
p.setState(0); //Dies by underpopulation
//System.out.println("ded by under");
}
if (numAlive > 3) {
//System.out.println("ded by over");
p.setState(0); //Dies by overpopulation
}
if (numAlive == 3)
p.setState(1); //Born with 3 neighbors.
_newGrid.setPatch(_rowNum,j,p);
}
try {
_thread.join();
} catch (Exception e) {
e.printStackTrace();
newGrid.setPatch(i,j,p);
}
}

System.out.println(g);
System.out.println("-----");
System.out.println(newGrid);
//boolean running = true;
//while (running) {
//running = false;
//for (Thread t : threads) {
//if (t.isAlive()) running = true;
//}
//}
return newGrid;
}


//public static class rowThread implements Runnable {
//private Grid _original, _newGrid;
//private int _rowNum;
//private Thread _thread;

//public rowThread (Grid orig, Grid newGrid, int rowNum) {
//_original = orig;
//_newGrid = newGrid;
//_rowNum = rowNum;
//}
//public void setThread (Thread t) {
//_thread = t;
//}

//public void run () {
//for (int j = 0; j < _original.getHeight(); j++) {
//Patch[] neighbors = _original.getPatch(_rowNum, j).get8Neighbors();
//int numAlive = 0;
//System.out.println(_original.getPatch(_rowNum, j).getX() + "," + _original.getPatch(_rowNum, j).getY());
//for (Patch p : neighbors) {
//System.out.print(p.getState());
//if (p.getState() == 1) {
//numAlive++;
//}
//}
//System.out.println();
//Patch p = _original.getPatch(_rowNum,j).clone();
//if (numAlive < 2) {
//p.setState(0); //Dies by underpopulation
//}
//if (numAlive > 3) {
//p.setState(0); //Dies by overpopulation
//}
//if (numAlive == 3)
//p.setState(1); //Born with 3 neighbors.
//_newGrid.setPatch(_rowNum,j,p);
//}
//try {
//_thread.join();
//} catch (Exception e) {
//e.printStackTrace();
//}
//}
//}
}

Завантаження…
Відмінити
Зберегти