CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: help help, ... :) "game of life",

    You don't need the comparison to 3. It is enough to use:
    Code:
    if checkCell(cells,i,j) && (numberOfNeighbors(cells,i,j) == 2)
    Because the next if statement will compare to 3.

  2. #17
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    see i change it to your code
    (which by theway is much easier and more unserstandble)

    but the thing is that the next generation become all "false"

    Code:
    	public static int numberOfNeighbors(boolean[][] cells, int x, int y){
    		int numberOfNeighbors = -1; 
    		//This is because we will count the current cell as a neighbour, so we should start from -1
    
    		//looping over all cells arround current
    		for(int deltaX = -1 ; deltaX <= 1 ; deltaX++){ 
    			//We are starting from one row above, moving to current row and then to the row below
    			for(int deltaY = -1 ; deltaY <= 1 ; deltaY++){ 
    				//We are starting from one column to the left, moving to furren and then to the right 
    				if (checkCell(cells, x + deltaX, y + deltaY)) {
    
    					numberOfNeighbors++; 
    					//Adding one to the number of neighbours if the neighbour is alive. We WILL check the index [0][0] and obviously get true. This is the reason we started the counting from -1.
    				}
    			}
    		}
    
    		return numberOfNeighbors; //returning the result
    	}
    Code:
    	public static boolean[][] nextGeneration(boolean[][] old_cell) {
    
    
    		boolean[][] new_cell = new boolean[old_cell.length][old_cell[0].length];
    
    		for (int i=0;i<old_cell.length;i++)
    			for (int j=0;j<old_cell[0].length;j++)
    				new_cell[i][j] = false;
    
    		for (int i=0;i<old_cell.length;i++)
    			for (int j=0;j<old_cell[0].length;j++)
    			{
    				if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) >= 2)) 
    				{
    					new_cell[i][j] = true;
    				}
    
    				if (numberOfNeighbors(old_cell,i,j) == 3)
    				{
    					new_cell[i][j] = true;
    				}
    
    				else
    				{
    					new_cell[i][j] = false;
    				}
    			}
    
    
    		return new_cell;
    	}

    Code:
    before:
    
    false false false false false 
    false false false false false 
    false false false false false 
    true true true false false 
    false false false false false 
    
    
    after:
    
    false false false false false 
    false false false false false 
    false false false false false 
    false false false false false 
    false false false false false
    but when i switch it back to my neighbornumber function.. i get two trues.

    wierd.
    Last edited by sanfor; December 10th, 2009 at 03:21 AM.

  3. #18
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    see i just checkd to add [0][0] as an true cell

    and i got these "if'z" to be worked.

    Code:
    				if (numberOfNeighbors(old_cell,i,j) == 3)
    				{
    					new_cell[i][j] = true;
    				}
    
    				else
    				{
    					new_cell[i][j] = false;
    				}
    Code:
    false false false false false 
    false false false false false 
    false false false false false 
    true true true false false 
    true false false false false 
    
    
    
    false false false false false 
    false false false false false 
    false false false false false 
    false true false false false 
    false true false false false
    which means the problem is in

    Code:
    				if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) == 2)) 
    				{
    					new_cell[i][j] = true;
    				}

  4. #19
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: help help, ... :) "game of life",

    Put some printouts.
    Inside the loop of nextGeneration put something like:
    Code:
    System.out.println("Number of Neighbours in row " + i + " column " + j + " is: " +  numberOfNeighbours(old_cell, i, j)");
    This will help you debug.
    Post the results here, and it will be easier to find the problem.

  5. #20
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    see on the paper when i start to write it down
    the cell [2][1] has to have 3 true neibghbors.

    while starting with -1
    first numberOfNeighbors++ gives me > 0
    then > 1
    then > 2

    it suppose to be 3

  6. #21
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    Code:
    Number of Neighbours in row 0 column 0 is: 1
    Number of Neighbours in row 0 column 1 is: 2
    Number of Neighbours in row 0 column 2 is: 1
    Number of Neighbours in row 0 column 3 is: 0
    Number of Neighbours in row 0 column 4 is: 0
    Number of Neighbours in row 1 column 0 is: 1
    Number of Neighbours in row 1 column 1 is: 2
    Number of Neighbours in row 1 column 2 is: 1
    Number of Neighbours in row 1 column 3 is: 0
    Number of Neighbours in row 1 column 4 is: 0
    Number of Neighbours in row 2 column 0 is: 1
    Number of Neighbours in row 2 column 1 is: 2
    Number of Neighbours in row 2 column 2 is: 1
    Number of Neighbours in row 2 column 3 is: 0
    Number of Neighbours in row 2 column 4 is: 0
    Number of Neighbours in row 3 column 0 is: 0
    Number of Neighbours in row 3 column 1 is: 0
    Number of Neighbours in row 3 column 2 is: 0
    Number of Neighbours in row 3 column 3 is: 0
    Number of Neighbours in row 3 column 4 is: 0
    Number of Neighbours in row 4 column 0 is: 0
    Number of Neighbours in row 4 column 1 is: 0
    Number of Neighbours in row 4 column 2 is: 0
    Number of Neighbours in row 4 column 3 is: 0
    Number of Neighbours in row 4 column 4 is: 0
    first one
    in the bottom left corner

    [0][0] has two live neighbors

    which are [1][0] and [1][1]

  7. #22
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    i see it's
    when the CELL is LIVE it count the correct neighbors
    but when it's DEAD it counts -1

  8. #23
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: help help, ... :) "game of life",

    Yap, my mistake
    There is no avoidence of starting the counting from 0, and after looping add the following:
    Code:
    if (cells[x][y]){numberOfNeighbours--;}

  9. #24
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    i did it ;P

    Code:
    		int numberOfNeighbors;
    		
    		if (checkCell(cells,x,y))
    			numberOfNeighbors=-1;
    		else
    			numberOfNeighbors=0;
    before:

    Code:
    false false false false false 
    false false false false false 
    false false false false false 
    true true true false false 
    false false false false false
    after:

    Code:
    false false false false false 
    false false false false false 
    false true false false false 
    false true false false false 
    false true false false false

  10. #25
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    DONE

    thanks mate.
    i appreciate it alot

  11. #26
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: help help, ... :) "game of life",

    Great!
    Have fun with next assignments....

  12. #27
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    haha, you might see alot of me soon ;P

  13. #28
    Join Date
    Dec 2009
    Posts
    42

    Re: help help, ... :) "game of life",

    i am having difficults with another assigment

  14. #29
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: help help, ... :) "game of life",

    i am having difficults with another assigment
    OK, well start a new thread and in it explain exactly what the problem is and show what you have done to try to solve it.

  15. #30
    Join Date
    Mar 2010
    Posts
    4

    Re: help help, ... :) "game of life",

    Hi there,

    I need this assignment please because I have to solve it.

    But instead of False True I need it 0 1

    input
    00000
    01110
    00000
    00000
    00000

    output
    00100
    00100
    00100
    00000
    00000

    exactly same this assignment which disccussed here.

    please help cause I have tough courses

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured