> For the complete documentation index, see [llms.txt](https://nissan-goldberg.gitbook.io/java101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nissan-goldberg.gitbook.io/java101/exercises/exercise-2.md).

# Exercise 2

### &#x20;משחק החיים

{% tabs %}
{% tab title="GameOfLife.java" %}

```java
public class GameOfLife {

    public static void main(String[] args) {
        gameOfLife(240,10);
    }

    //YOU CAN ADD MORE FUNCTIONS IF YOU WANT

    public static void gameOfLife(int n, int cellSize){
        //n = 240; cellSize = 10;
        //TODO
    }
    public static void clearCells(boolean[][]cells) {
        //TODO
    }


    public static void drawCells(boolean[][]cells, int cellSize, Color color) {
        StdDraw.setPenColor(color);
        for (int i=0; i<cells.length; i++) {
            for(int j=0; j<cells[0].length; j++) {
                if (cells[i][j]) {
                    double p = (i*cellSize*2 + cellSize)/2;
                    double q = (j*cellSize*2 + cellSize)/2;
                    StdDraw.filledSquare(p, q, cellSize/2);
                }
            }
        }
    }
}
```

{% endtab %}

{% tab title="NextGeneration.java" %}

```java
public class NextGeneration {
    /* חובה */    
    public static boolean[][] nextGeneration(boolean[][] cells){
        //TODO
    }
    
    
    
    /* לא חובה */    
    public static boolean isInside(boolean[][] cells, int x, int y){ }

    /* לא חובה */   
    public static boolean checkCell(boolean[][] cells, int x, int y){ }

    /* לא חובה */    
    public static int numberOfNeighbors(boolean[][] cells,int x,int y){ }
}
```

{% endtab %}
{% endtabs %}

### Functions that can help

```java
StdDraw.setXscale(minX, maxX);
StdDraw.setYscale(minY, maxY);
StdDraw.setPenColor(Color.LIGHT_GRAY);

for (int i = 0; i <= n; i+=cellSize) {
		//line(x0, y0, x1, y2)
		StdDraw.line(i, y0, i, y2);
}

for (int i = 0; i <= n; i+=cellSize) {
		//line(x0, y0, x1, y2)
		StdDraw.line(x0, i, x1, i);
}
```

####

#### Running: The Game of Life

![](https://2498830366-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVGLlEB8nZRYjtdyfF8%2F-M_0Zek5ZHQhM_DokeFR%2F-M_0_jukFmBVZ6UypMXf%2FGameOfLife.gif?alt=media\&token=27eb0a1e-5d4f-418a-ada8-853d7dac5aae)
