Skip to content
Permalink
Browse files
Implement Interface
  • Loading branch information
ClayDowling committed Apr 15, 2017
1 parent db0a69e commit 4077b27e61ed83b0d571bc2ec19107d308f0b82f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
@@ -8,4 +8,8 @@ In the Initial State I have the implementation of the LifeBoard exposed and refe

## Defined Interface

The first step is defining an interface which encapsulates the actions I am interested in.
The first step is defining an interface which encapsulates the actions I am interested in.

## Implement Interface

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.
@@ -7,6 +7,6 @@ public interface Board {

public int at(int x, int y);

public void set(int x, int y);
public void set(int x, int y, int value);

}
@@ -1,8 +1,32 @@
/**
* Created by clay on 4/15/17.
*/
public class LifeBoard {
public class LifeBoard implements Board {
public static final int X_MAX = 50;
public static final int Y_MAX = 50;
static public boolean[][] value = new boolean[X_MAX][Y_MAX];


public void clear() {
for(int x=0; x < X_MAX; ++x) {
for(int y=0; y < Y_MAX; ++y) {
value[x][y] = false;
}
}
}

public int at(int x, int y) {
if (value[x][y]) {
return 1;
}
return 0;
}

public void set(int x, int y, int value) {
if (0 == value) {
this.value[x][y] = false;
} else {
this.value[x][y] = true;
}
}
}
@@ -0,0 +1,46 @@
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

/**
* Created by clay on 4/15/17.
*/
public class LifeBoardTest {

private LifeBoard board;

@Before
public void setUp() {
board = new LifeBoard();
board.clear();
}

@Test
public void clear_byDefault_setsAllValuesToFalse() {
board.value[10][10] = true;
board.clear();
assertThat(board.value[10][10], is(false));
}

@Test
public void at_forATrueCell_returnsOne() {
board.value[10][10] = true;
assertThat(board.at(10, 10), is(1));
}

@Test
public void at_forFalseCell_returnsZero() {
board.value[10][10] = false;
assertThat(board.at(10, 10), is(0));
}

@Test
public void set_givenZero_willReturnZeroFromAt() {
board.value[10][10] = true;
board.set(10, 10, 0);
assertThat(board.at(10, 10), is(0));
}

}

0 comments on commit 4077b27

Please sign in to comment.