Skip to content

Commit e2136dc

Browse files
authored
Merge pull request #61 from
Fix birth/survival rule bug in Game of Life
2 parents 4c53bcd + 3032b3b commit e2136dc

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

GameOfLife/src/main/java/com/example/deneme/Game.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
public class Game {
44

5-
public void applyBirthRule (Cell cell){
6-
if(!cell.isAlive() && cell.getNeighbours()==3){
5+
public void applyBirthRule(Cell cell) {
6+
if (!cell.isAlive() && cell.getNeighbours() == 3) {
77
cell.setAlive(true);
8-
}cell.setAlive(false);
8+
} else {
9+
cell.setAlive(false);
10+
}
911
}
1012

11-
public void applySurvivalRule(Cell cell){
12-
if (cell.getNeighbours()==2||cell.getNeighbours()==3){
13-
cell.setAlive(true);
14-
}cell.setAlive(false);
15-
}
13+
public void applySurvivalRule(Cell cell) {
14+
if (cell.getNeighbours() == 2 || cell.getNeighbours() == 3) {
15+
cell.setAlive(true);
16+
} else {
17+
cell.setAlive(false);
18+
}
19+
}
1620

1721
private Grid grid;
1822

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.deneme;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class GameRulesTest {
7+
8+
@Test
9+
void birthRuleSetsAliveWhenExactlyThreeNeighbors() {
10+
Cell cell = new Cell();
11+
cell.setAlive(false);
12+
cell.setNeighbours(3);
13+
Game game = new Game(1, 1);
14+
game.applyBirthRule(cell);
15+
assertTrue(cell.isAlive(), "Cell should become alive when it has exactly three neighbours");
16+
}
17+
18+
@Test
19+
void birthRuleKeepsDeadWhenNotThreeNeighbors() {
20+
Cell cell = new Cell();
21+
cell.setAlive(false);
22+
cell.setNeighbours(2);
23+
Game game = new Game(1, 1);
24+
game.applyBirthRule(cell);
25+
assertFalse(cell.isAlive(), "Cell should remain dead when it does not have three neighbours");
26+
}
27+
28+
@Test
29+
void survivalRuleKeepsAliveWithTwoOrThreeNeighbors() {
30+
Cell cell = new Cell();
31+
cell.setNeighbours(2);
32+
Game game = new Game(1, 1);
33+
game.applySurvivalRule(cell);
34+
assertTrue(cell.isAlive(), "Cell should stay alive with two neighbours");
35+
36+
cell.setNeighbours(3);
37+
game.applySurvivalRule(cell);
38+
assertTrue(cell.isAlive(), "Cell should stay alive with three neighbours");
39+
}
40+
41+
@Test
42+
void survivalRuleKillsWithOtherNeighborCounts() {
43+
Cell cell = new Cell();
44+
cell.setNeighbours(1);
45+
Game game = new Game(1, 1);
46+
game.applySurvivalRule(cell);
47+
assertFalse(cell.isAlive(), "Cell should die with neighbour count other than two or three");
48+
}
49+
}

0 commit comments

Comments
 (0)