grid java public public class class grid private private
play

Grid.java public public class class Grid { private private final - PDF document

Grid.java public public class class Grid { private private final final int int width; private private final final int int height; private private final final Status[][] grid; private private boolean boolean empty = true true;


  1. Grid.java public public class class Grid { private private final final int int width; private private final final int int height; private private final final Status[][] grid; private private boolean boolean empty = true true; public public Grid(int int width, int int height) { this this.width = width; this this.height = height; grid = new new Status[width][height]; initialiseGrid(); } public public boolean boolean isEmpty() { return return empty; } public public void void addBomb(int int columnIndex, int int rowIndex) { grid[columnIndex][rowIndex] = Status. BOMB ; empty = false false; for for (int int i = -1; i <= 1; i++) { for for (int int j = -1; j <= 1; j++) { int int col = columnIndex + i; int int row = rowIndex + j; if if (isIndexInsideGrid(col, row)) { grid[col][row] = grid[col][row].increment(); } } } } private private boolean boolean isIndexInsideGrid(int int col, int int row) { return return col >=0 && col < width && row >=0 && row<height; } public public Status getStatus(int int columnIndex, int int rowIndex) { return return grid[columnIndex][rowIndex]; } Page 1

  2. Grid.java private private void void initialiseGrid() { for for (int int i = 0; i < grid.length; i++) { for for (int int j = 0; j < grid[i].length; j++) { grid[i][j] = Status. ZERO ; } } } } Page 2

  3. Status.java public public enum enum Status { ONE (null null), BOMB (), ZERO ( ONE ); private private Status next; private private Status() { next = this this; } private private Status(Status next) { this this.next = next; } public public Status increment() { return return next; } } Page 1

  4. GridTest.java import import junit.framework.Assert; import import org.junit.Test; public public class class GridTest { @Test public public void void shouldGetAnEmptyGridWhenThereIsNoInput() throws throws Exception { Grid grid = new new Grid(0,0); Assert. assertTrue (grid.isEmpty()); } @Test public public void void shouldNotBeEmptyWhenYouAddBombToTheGrid() throws throws Exception { Grid grid = new new Grid(4, 4); grid.addBomb(0, 1); Assert. assertFalse (grid.isEmpty()); } @Test public public void void shouldReturnCorrectNumberOfBombsFromAdjacentCells() throws throws Exception { Grid grid = new new Grid(4, 4); grid.addBomb(0, 1); Status actualStatus = grid.getStatus(0, 0); Assert. assertEquals (Status. ONE , actualStatus); } } Page 1

Recommend


More recommend