Conways Game of Life trouble [java] -
public static boolean[][] calculate(boolean cellstate[][])//imports cell patern(in form of graph) { int x = 0, y = 0; int alive = 0; boolean newcellstate[][] = new boolean[50][50];//what next generation stored in while(x < 50 && y < 50)//calculation loop { try//each adjacent cell { if(cellstate[x-1][y] == true) alive++; //if adjacent cell alive alive variable gets + 1 }catch(indexoutofboundsexception e) {} try { if (cellstate[x-1][y-1] == true) alive++; } catch(indexoutofboundsexception e) { } try { if(cellstate[x-1][y+1] == true) alive++; } catch(indexoutofboundsexception e) { } try { if (cellstate[x][y+1] == true) alive++; } catch(indexoutofboundsexception e) { } try { if (cellstate[x][y-1] == true) alive++; } catch(indexoutofboundsexception e) { } try { if(cellstate[x+1][y] == true) alive++; } catch(indexoutofboundsexception e) { } try { if(cellstate[x+1][y-1] == true) alive++; } catch(indexoutofboundsexception e) { } try { if(cellstate[x+1][y+1] == true) alive++; } catch(indexoutofboundsexception e) { } if(cellstate[x][y] = true) //determines if cell should alive in next generation { if(alive < 2) newcellstate[x][y] = false; else if(alive > 3) newcellstate[x][y] = false; else if(alive == 2) newcellstate[x][y] = true;//stores in new cell state next gen else newcellstate[x][y] = true; } else { newcellstate[x][y] = false; } alive = 0; //resets alive if(x == 49)//counter graph { y++; x=0; }else { x++; } } return newcellstate; }
this method used calculate next generation of conway's game of life. each time run, no matter pattern is, prints out every cell dead except edges of grid.
this main method:
public static void main(string args[]) { int x = 0; int y = 0; boolean cellstate[][] = new boolean[50][50]; string answer; scanner input = new scanner(system.in); while(true) { system.out.print("\n type next generation, 'new' new grid, or 'stop' end>> "); answer = input.nextline(); if(answer.equals("new")) { cellstate = newcells(); }else if(answer.equals("stop")) { break; }else { cellstate = calculate(cellstate); print(cellstate); } } }
i have tested printing method doesn't have problem there. can't figure out why this. appreciated! thanks!
if(cellstate[x][y] = true)
you forgot use ==
one. otherwise, code looks fine.
Comments
Post a Comment