소스 검색

Added Brian's Brain implementation, and run it in Goldfish

master
Josh Hofing 12 년 전
부모
커밋
8311ba5ffb
2개의 변경된 파일38개의 추가작업 그리고 21개의 파일을 삭제
  1. +11
    -21
      src/Goldfish.java
  2. +27
    -0
      src/rules/BriansBrain.java

+ 11
- 21
src/Goldfish.java 파일 보기

@@ -15,29 +15,19 @@ public class Goldfish {

public void run () {
//TODO: make it run.
_grid.getPatch(1,0).setState(1);
_grid.getPatch(2,1).setState(1);
_grid.getPatch(2,2).setState(1);
_grid.getPatch(1,2).setState(1);
_grid.getPatch(0,2).setState(1);
_grid.getPatch(1,0).setState(2);
_grid.getPatch(2,1).setState(2);
_grid.getPatch(2,2).setState(2);
_grid.getPatch(1,2).setState(2);
_grid.getPatch(0,2).setState(2);
System.out.println(_grid);

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

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

_grid = Conway.run(_grid);
System.out.println("------------");
System.out.println(_grid);
}
for (int i = 0; i < 6; i++) {
_grid = BriansBrain.run(_grid);
System.out.println("------------");
System.out.println(_grid);
}
}

public static void main (String[] args) {
Goldfish g = new Goldfish();


+ 27
- 0
src/rules/BriansBrain.java 파일 보기

@@ -0,0 +1,27 @@
package edu.stuy.goldfish.rules;

import edu.stuy.goldfish.Grid;
import edu.stuy.goldfish.Patch;

public class BriansBrain extends RuleSet {
public static int states = 3;

public static Grid run (Grid g) {
Grid newGrid = new Grid(g.getWidth(), g.getHeight());
for (int i = 0; i < g.getWidth(); i++) {
for (int j = 0; j < g.getHeight(); j++) {
Patch[] neighbors = g.getPatch(i, j).get8Neighbors();
int numAlive = 0;
for (Patch neighbor : neighbors)
if (neighbor.getState() == 2) numAlive++;
Patch orig = g.getPatch(i,j);
Patch p = g.getPatch(i,j).clone(newGrid);
if (orig.getState() == 0 && numAlive == 2) p.setState(2);
if (orig.getState() == 1) p.setState(0); //Dying cells die.
if (orig.getState() == 2) p.setState(1); //Make living cells dying.
newGrid.setPatch(i,j,p);
}
}
return newGrid;
}
}

불러오는 중...
취소
저장