diff --git a/minesweeper/src/main/java/edu/pdx/cs410J/katas/Minesweeper.java b/minesweeper/src/main/java/edu/pdx/cs410J/katas/Minesweeper.java index 3fe6bf0..7989bd2 100644 --- a/minesweeper/src/main/java/edu/pdx/cs410J/katas/Minesweeper.java +++ b/minesweeper/src/main/java/edu/pdx/cs410J/katas/Minesweeper.java @@ -1,16 +1,76 @@ package edu.pdx.cs410J.katas; public class Minesweeper { - - public Minesweeper(int row, int column) { - + private char[][] board; + private int row; + private int col; + private int currentEmptyRow; + public Minesweeper(int row, int col) throws ZeroSizedBoardException { + if (row * col == 0) { + throw new ZeroSizedBoardException(); + } + this.row = row; + this.col = col; + board = new char[row][col]; } - public void addRow(String row) { - + public void addRow(String row) throws RaggedBoardException { + if (row.length() != board[0].length) { + throw new RaggedBoardException(); + } + for (int i = 0; i < col; i++) { + board[currentEmptyRow][i] = row.charAt(i); + } + currentEmptyRow++; } public String solve() { - return null; + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if (board[i][j] != '*') { + board[i][j] = (char) (countMines(i, j) + 48); + } + } + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + sb.append(board[i][j]); + } + if (i != row - 1) { + sb.append("\n"); + } + } + return sb.toString(); + } + private int countMines(int row, int col) { + int count = 0; + if (row - 1 >= 0 && col - 1 >= 0 && board[row - 1][col - 1] == '*') { + count++; + } + if (row - 1 >= 0 && board[row - 1][col] == '*') { + count++; + } + if (row - 1 >= 0 && col + 1 < this.col && board[row - 1][col + 1] == '*') { + count++; + } + if (col + 1 < this.col && board[row][col + 1] == '*') { + count++; + } + if (row + 1 < this.row && col + 1 < this.col && board[row + 1][col + 1] == '*') { + count++; + } + if (row + 1 < this.row && board[row + 1][col] == '*') { + count++; + } + if (row + 1 < this.row && col - 1 >= 0 && board[row + 1][col - 1] == '*') { + count++; + } + if (col - 1 >= 0 && board[row][col - 1] == '*') { + count++; + } + return count; } + } diff --git a/minesweeper/src/test/java/edu/pdx/cs410J/katas/MinesweeperTest.java b/minesweeper/src/test/java/edu/pdx/cs410J/katas/MinesweeperTest.java index c209511..24036d2 100644 --- a/minesweeper/src/test/java/edu/pdx/cs410J/katas/MinesweeperTest.java +++ b/minesweeper/src/test/java/edu/pdx/cs410J/katas/MinesweeperTest.java @@ -11,42 +11,39 @@ */ public class MinesweeperTest { - @Ignore @Test(expected = ZeroSizedBoardException.class) - public void zeroSizedBoardThrowException() { + public void zeroSizedBoardThrowException() throws ZeroSizedBoardException { new Minesweeper(0, 0); } - @Ignore @Test(expected = ZeroSizedBoardException.class) - public void boardWithZeroRowsThrowsException() { + public void boardWithZeroRowsThrowsException() throws ZeroSizedBoardException { new Minesweeper(0, 5); } - @Ignore @Test(expected = ZeroSizedBoardException.class) - public void boardWithZeroColumnsThrowsException() { + public void boardWithZeroColumnsThrowsException() throws ZeroSizedBoardException { new Minesweeper(5, 0); } - @Ignore + @Test(expected = RaggedBoardException.class) - public void raggedInputRowThrowsException() { + public void raggedInputRowThrowsException() throws ZeroSizedBoardException, RaggedBoardException { Minesweeper board = new Minesweeper(1, 2); board.addRow("."); } - @Ignore + @Test(expected = RaggedBoardException.class) - public void raggedInputColumnThrowsException() { + public void raggedInputColumnThrowsException() throws ZeroSizedBoardException, RaggedBoardException { Minesweeper board = new Minesweeper(2, 1); board.addRow("."); board.addRow(".."); } - @Ignore + @Test - public void oneSquareWithMine() { + public void oneSquareWithMine() throws ZeroSizedBoardException, RaggedBoardException { Minesweeper board = new Minesweeper(1, 1); board.addRow("*"); @@ -54,9 +51,9 @@ public void oneSquareWithMine() { assertThat(solved, equalTo("*")); } - @Ignore + @Test - public void oneSquareWithNoMine() { + public void oneSquareWithNoMine() throws ZeroSizedBoardException, RaggedBoardException { Minesweeper board = new Minesweeper(1, 1); board.addRow("."); @@ -64,9 +61,9 @@ public void oneSquareWithNoMine() { assertThat(solved, equalTo("0")); } - @Ignore + @Test - public void twoByTwoBoardWithMineAtZeroZero() { + public void twoByTwoBoardWithMineAtZeroZero() throws ZeroSizedBoardException, RaggedBoardException { Minesweeper board = new Minesweeper(2, 2); board.addRow("*."); board.addRow("..");