Last active
August 31, 2016 10:15
-
-
Save dpolivaev/7c822ed6c4fcd143ae3d885cc9aca1e2 to your computer and use it in GitHub Desktop.
Outside-in driven by constraints "first class collections members" and "no getters"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package gameOfLife; | |
| import java.util.Set; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| import java.util.function.Consumer; | |
| class Board { | |
| private Set<Coordinate> livingCells; | |
| public Board(Set<Coordinate> livingCells) { | |
| this.livingCells = livingCells; | |
| } | |
| public void forEachLivingCell(Consumer<Coordinate> action) { | |
| livingCells.forEach(action); | |
| } | |
| public int countNeighbours(Coordinate coordinate) { | |
| AtomicInteger neighbourCounter = new AtomicInteger(0); | |
| coordinate.forEachNeighbour(neighbour -> { | |
| if (hasLivingCellAt(neighbour)) | |
| neighbourCounter.incrementAndGet(); | |
| }); | |
| return neighbourCounter.get(); | |
| } | |
| public boolean hasLivingCellAt(Coordinate coordinate) { | |
| return livingCells.contains(coordinate); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package gameOfLife; | |
| import java.util.function.Consumer; | |
| public class Coordinate { | |
| private final int x; | |
| private final int y; | |
| public Coordinate(int x, int y) { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| public void forEachNeighbour(Consumer<Coordinate> action) { | |
| action.accept(new Coordinate(x-1, y-1)); | |
| action.accept(new Coordinate(x-1, y)); | |
| action.accept(new Coordinate(x-1, y+1)); | |
| action.accept(new Coordinate(x, y-1)); | |
| action.accept(new Coordinate(x, y+1)); | |
| action.accept(new Coordinate(x+1, y-1)); | |
| action.accept(new Coordinate(x+1, y)); | |
| action.accept(new Coordinate(x+1, y+1)); | |
| } | |
| @Override | |
| public int hashCode() { | |
| final int prime = 31; | |
| int result = 1; | |
| result = prime * result + x; | |
| result = prime * result + y; | |
| return result; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (this == obj) | |
| return true; | |
| if (obj == null) | |
| return false; | |
| if (getClass() != obj.getClass()) | |
| return false; | |
| Coordinate other = (Coordinate) obj; | |
| if (x != other.x) | |
| return false; | |
| if (y != other.y) | |
| return false; | |
| return true; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package gameOfLife; | |
| import java.util.Arrays; | |
| import java.util.HashSet; | |
| public class Game { | |
| private Board board; | |
| public Game(Coordinate... livingCells) { | |
| this.board = new Board(new HashSet<>(Arrays.asList(livingCells))); | |
| } | |
| public boolean hasLivingCellAt(Coordinate coordinate) { | |
| return board.hasLivingCellAt(coordinate); | |
| } | |
| public void nextIteration() { | |
| HashSet<Coordinate> livingCells = new HashSet<>(); | |
| board.forEachLivingCell(coordinate -> { | |
| if(board.countNeighbours(coordinate) == 2) | |
| livingCells.add(coordinate); | |
| coordinate.forEachNeighbour(neighbour -> { | |
| if(board.countNeighbours(neighbour) == 3) | |
| livingCells.add(neighbour);}); | |
| }); | |
| board = new Board(livingCells); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package gameOfLife; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| import static org.junit.Assert.*; | |
| import org.junit.Test; | |
| import gameOfLife.Coordinate; | |
| import gameOfLife.Game; | |
| public class GameTest { | |
| @Test | |
| public void checksForDeadCell() throws Exception { | |
| Game game = new Game(); | |
| assertThat(game.hasLivingCellAt(new Coordinate(0, 0))).isFalse(); | |
| } | |
| @Test | |
| public void checksForLivingCell() throws Exception { | |
| Game game = new Game(new Coordinate(0, 0)); | |
| assertThat(game.hasLivingCellAt(new Coordinate(0, 0))).isTrue(); | |
| } | |
| @Test | |
| public void singleLivingCellDies() throws Exception { | |
| Game game = new Game(new Coordinate(0, 0)); | |
| game.nextIteration(); | |
| assertThat(game.hasLivingCellAt(new Coordinate(0, 0))).isFalse(); | |
| } | |
| @Test | |
| public void livingCellAtX0Y0WithTwoNeighboursStaysAlive() throws Exception { | |
| Game game = new Game(new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(0, 1)); | |
| game.nextIteration(); | |
| assertThat(game.hasLivingCellAt(new Coordinate(0, 0))).isTrue(); | |
| } | |
| @Test | |
| public void livingCellAtX1Y1WithTwoNeighboursStaysAlive() throws Exception { | |
| Game game = new Game(new Coordinate(1, 1), new Coordinate(1, 0), new Coordinate(0, 1)); | |
| game.nextIteration(); | |
| assertThat(game.hasLivingCellAt(new Coordinate(1, 1))).isTrue(); | |
| } | |
| @Test | |
| public void deadCellAtX1Y1WithThreeNeighboursBecomesAlive() throws Exception { | |
| Game game = new Game(new Coordinate(0, 0), new Coordinate(1, 0), new Coordinate(0, 1)); | |
| game.nextIteration(); | |
| assertThat(game.hasLivingCellAt(new Coordinate(1, 1))).isTrue(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment