Skip to content

Commit 4077b27

Browse files
committed
Implement Interface
1 parent db0a69e commit 4077b27

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ In the Initial State I have the implementation of the LifeBoard exposed and refe
88

99
## Defined Interface
1010

11-
The first step is defining an interface which encapsulates the actions I am interested in.
11+
The first step is defining an interface which encapsulates the actions I am interested in.
12+
13+
## Implement Interface
14+
15+
Next I implement the interface, without changing the classes which access the board object. All my tests are still green and I could push to master without breaking things.

src/main/java/Board.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public interface Board {
77

88
public int at(int x, int y);
99

10-
public void set(int x, int y);
10+
public void set(int x, int y, int value);
1111

1212
}

src/main/java/LifeBoard.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
/**
22
* Created by clay on 4/15/17.
33
*/
4-
public class LifeBoard {
4+
public class LifeBoard implements Board {
55
public static final int X_MAX = 50;
66
public static final int Y_MAX = 50;
77
static public boolean[][] value = new boolean[X_MAX][Y_MAX];
8+
9+
10+
public void clear() {
11+
for(int x=0; x < X_MAX; ++x) {
12+
for(int y=0; y < Y_MAX; ++y) {
13+
value[x][y] = false;
14+
}
15+
}
16+
}
17+
18+
public int at(int x, int y) {
19+
if (value[x][y]) {
20+
return 1;
21+
}
22+
return 0;
23+
}
24+
25+
public void set(int x, int y, int value) {
26+
if (0 == value) {
27+
this.value[x][y] = false;
28+
} else {
29+
this.value[x][y] = true;
30+
}
31+
}
832
}

src/test/java/LifeBoardTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.junit.Before;
2+
import org.junit.Test;
3+
4+
import static org.hamcrest.Matchers.is;
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Created by clay on 4/15/17.
9+
*/
10+
public class LifeBoardTest {
11+
12+
private LifeBoard board;
13+
14+
@Before
15+
public void setUp() {
16+
board = new LifeBoard();
17+
board.clear();
18+
}
19+
20+
@Test
21+
public void clear_byDefault_setsAllValuesToFalse() {
22+
board.value[10][10] = true;
23+
board.clear();
24+
assertThat(board.value[10][10], is(false));
25+
}
26+
27+
@Test
28+
public void at_forATrueCell_returnsOne() {
29+
board.value[10][10] = true;
30+
assertThat(board.at(10, 10), is(1));
31+
}
32+
33+
@Test
34+
public void at_forFalseCell_returnsZero() {
35+
board.value[10][10] = false;
36+
assertThat(board.at(10, 10), is(0));
37+
}
38+
39+
@Test
40+
public void set_givenZero_willReturnZeroFromAt() {
41+
board.value[10][10] = true;
42+
board.set(10, 10, 0);
43+
assertThat(board.at(10, 10), is(0));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)