package edu.stuy.goldfish.rules;

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

public class Conway extends RuleSet {
    public static int states = 2;

    public static Grid run (Grid g) {
        //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 neighbor : neighbors)
                    if (neighbor.getState() == 1) numAlive++;
                Patch p = g.getPatch(i,j).clone(newGrid);
                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();
                //int numAlive = 0;
                //for (Patch p : neighbors)
                    //if (p.getState() == 1) numAlive++;
                //Patch p = _original.getPatch(_rowNum,j).clone(_newGrid);
                //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();
            //}
        //}
    //}
}

@@ -206,63 +206,8 @@
 ) %7B%0A
-        //Thread%5B%5D threads = new Thread%5Bg.getWidth()%5D;%0A
     
@@ -317,206 +317,8 @@
 ) %7B%0A
-            //Conway.rowThread rt = new Conway.rowThread(g, newGrid, i);%0A            //Thread t = new Thread(rt);%0A            //rt.setThread(t);%0A            //threads%5Bi%5D = t;%0A            //t.run();%0A
     
@@ -621,16 +621,17 @@
 Patch(i,
+ 
 j).clone
@@ -955,33 +955,32 @@
 with 3 neighbors
-.
 %0A               
@@ -1003,1679 +1003,68 @@
 h(i,
-j,p);%0A            %7D%0A        %7D%0A%0A        //boolean running = true;%0A        //while (running) %7B%0A            //running = false;%0A            //for (Thread t : threads) %7B%0A                //if (t.isAlive()) running = true;%0A            //%7D%0A        //%7D%0A        return newGrid;%0A    %7D%0A%0A%0A    //public static class rowThread implements Runnable %7B %0A        //private Grid _original, _newGrid;%0A        //private int _rowNum;%0A        //private Thread _thread;%0A%0A        //public rowThread (Grid orig, Grid newGrid, int rowNum) %7B%0A            //_original = orig;%0A            //_newGrid = newGrid;%0A            //_rowNum = rowNum;%0A        //%7D%0A        //public void setThread (Thread t) %7B%0A            //_thread = t;%0A        //%7D%0A%0A        //public void run () %7B%0A            //for (int j = 0; j %3C _original.getHeight(); j++) %7B%0A                //Patch%5B%5D neighbors = _original.getPatch(_rowNum, j).get8Neighbors();%0A                //int numAlive = 0;%0A                //for (Patch p : neighbors)%0A                    //if (p.getState() == 1) numAlive++;%0A                //Patch p = _original.getPatch(_rowNum,j).clone(_
+ j, p);%0A            %7D%0A        %7D%0A        return 
 newGrid
-)
 ;%0A    
-            //if (numAlive %3C 2) %7B%0A                    //p.setState(0); //Dies by underpopulation%0A                //%7D%0A                //if (numAlive %3E 3) %7B%0A                    //p.setState(0); //Dies by overpopulation%0A                //%7D%0A                //if (numAlive == 3)%0A                    //p.setState(1); //Born with 3 neighbors.%0A                //_newGrid.setPatch(_rowNum,j,p);%0A            //%7D%0A            //try %7B%0A                //_thread.join();%0A            //%7D catch (Exception e) %7B%0A                //e.printStackTrace();%0A            //%7D%0A        //%7D%0A    //
 %7D%0A%7D%0A

Remove old threading code