Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 66 additions & 6 deletions minesweeper/src/main/java/edu/pdx/cs410J/katas/Minesweeper.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
29 changes: 13 additions & 16 deletions minesweeper/src/test/java/edu/pdx/cs410J/katas/MinesweeperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,59 @@
*/
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("*");

String solved = board.solve();
assertThat(solved, equalTo("*"));
}

@Ignore
@Test
public void oneSquareWithNoMine() {
public void oneSquareWithNoMine() throws ZeroSizedBoardException, RaggedBoardException {
Minesweeper board = new Minesweeper(1, 1);
board.addRow(".");

String solved = board.solve();
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("..");
Expand Down