Code Kata Report

diff -r 1bcc39844e20 -r a4610cc0ae73 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:31:31 2012 -0600 @@ -0,0 +1,22 @@ +package com.nicholas.tuck.katas.minesweeper; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class MinesweeperTest { + + @Test + public void shouldLoadTrivialGrid() { + char[][] input = new char[2][2]; + input[0] = new char[] {'.', '.'}; + input[1] = new char[] {'.', '.'}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, notNullValue()); + assertThat(output[0], is(new char[] {'0', '0'})); + assertThat(output[1], is(new char[] {'0', '0'})); + } +} diff -r 1bcc39844e20 -r a4610cc0ae73 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:31:31 2012 -0600 @@ -0,0 +1,11 @@ +package com.nicholas.tuck.katas.minesweeper; + +public class Minesweeper { + public Minesweeper(char[][] input) { + + } + + public char[][] sweep() { + return new char[][] {{'0', '0'}, {'0', '0'}}; + } +} ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r a4610cc0ae73 -r 8bc92a255b87 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:31:31 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:44:08 2012 -0600 @@ -6,17 +6,30 @@ import static org.hamcrest.Matchers.*; public class MinesweeperTest { @Test - public void shouldLoadTrivialGrid() { + public void shouldLoadNoMines() { char[][] input = new char[2][2]; input[0] = new char[] {'.', '.'}; input[1] = new char[] {'.', '.'}; Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output, notNullValue()); assertThat(output[0], is(new char[] {'0', '0'})); assertThat(output[1], is(new char[] {'0', '0'})); } + + @Test + public void shouldLoadAllMines() { + char[][] input = new char[2][2]; + input[0] = new char[] {'*', '*'}; + input[1] = new char[] {'*', '*'}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, notNullValue()); + assertThat(output[0], is(new char[] {'*', '*'})); + assertThat(output[1], is(new char[] {'*', '*'})); + } } diff -r a4610cc0ae73 -r 8bc92a255b87 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:31:31 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:44:08 2012 -0600 @@ -1,11 +1,24 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { + + private final char[][] minefield; + public Minesweeper(char[][] input) { + minefield = input; } public char[][] sweep() { - return new char[][] {{'0', '0'}, {'0', '0'}}; + for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { + char[] row = minefield[rowIndex]; + for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { + char cell = row[colIndex]; + if (cell != '*') { + minefield[rowIndex][colIndex] = '0'; + } + } + } + return minefield; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 8bc92a255b87 -r a0c289ebb976 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:44:08 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:53:27 2012 -0600 @@ -30,6 +30,15 @@ char[][] output = minesweeper.sweep(); assertThat(output, notNullValue()); assertThat(output[0], is(new char[] {'*', '*'})); assertThat(output[1], is(new char[] {'*', '*'})); } + + @Test + public void cellNextToSingleMineShouldBeOne() { + char[][] input = {{'*', '.'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output[0], is(new char[] {'*', '1'})); + } } diff -r 8bc92a255b87 -r a0c289ebb976 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:44:08 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:53:27 2012 -0600 @@ -1,24 +1,35 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { - char cell = row[colIndex]; + + char cell = minefield[rowIndex][colIndex]; + if (cell != '*') { - minefield[rowIndex][colIndex] = '0'; + char previousCell = 0; + if ((colIndex - 1) >= 0){ + previousCell = row[colIndex -1]; + } + + if (previousCell == '*') { + minefield[rowIndex][colIndex] = '1'; + } else { + minefield[rowIndex][colIndex] = '0'; + } } } } return minefield; } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r a0c289ebb976 -r 77995a151543 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:53:27 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:55:51 2012 -0600 @@ -1,35 +1,39 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { - - char cell = minefield[rowIndex][colIndex]; - - if (cell != '*') { - char previousCell = 0; - if ((colIndex - 1) >= 0){ - previousCell = row[colIndex -1]; - } - - if (previousCell == '*') { - minefield[rowIndex][colIndex] = '1'; - } else { - minefield[rowIndex][colIndex] = '0'; - } - } + updateCell(rowIndex, colIndex); } } return minefield; } + + private void updateCell(int rowIndex, int colIndex) { + char[] row = minefield[rowIndex]; + char cell = row[colIndex]; + + if (cell != '*') { + char previousCell = 0; + if ((colIndex - 1) >= 0){ + previousCell = row[colIndex -1]; + } + + if (previousCell == '*') { + minefield[rowIndex][colIndex] = '1'; + } else { + minefield[rowIndex][colIndex] = '0'; + } + } + } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 77995a151543 -r 92ea9e11ff66 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 12:55:51 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:11:52 2012 -0600 @@ -39,6 +39,15 @@ Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output[0], is(new char[] {'*', '1'})); } + + @Test + public void cellWithMinesOnLeftAndRightShouldBeTwo() { + char[][] input = {{'*', '.', '*'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output[0], is(new char[] {'*', '2', '*'})); + } } diff -r 77995a151543 -r 92ea9e11ff66 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 12:55:51 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:11:52 2012 -0600 @@ -1,39 +1,47 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; - + int numMines = 0; if (cell != '*') { char previousCell = 0; + char nextCell = 0; if ((colIndex - 1) >= 0){ previousCell = row[colIndex -1]; } if (previousCell == '*') { - minefield[rowIndex][colIndex] = '1'; - } else { - minefield[rowIndex][colIndex] = '0'; + ++numMines; } + if ((colIndex + 1) < row.length){ + nextCell = row[colIndex +1]; + } + + if (nextCell == '*') { + ++numMines; + } + minefield[rowIndex][colIndex] = (char)((int)'0'+ numMines); } + } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 92ea9e11ff66 -r 6dfb161c93cb src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:11:52 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:17:15 2012 -0600 @@ -1,47 +1,47 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { char previousCell = 0; char nextCell = 0; if ((colIndex - 1) >= 0){ previousCell = row[colIndex -1]; } if (previousCell == '*') { ++numMines; } if ((colIndex + 1) < row.length){ nextCell = row[colIndex +1]; } if (nextCell == '*') { ++numMines; } - minefield[rowIndex][colIndex] = (char)((int)'0'+ numMines); + minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 6dfb161c93cb -r fc2d79672709 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:17:15 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:29:17 2012 -0600 @@ -48,6 +48,18 @@ Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output[0], is(new char[] {'*', '2', '*'})); } + @Test + public void cellsWithMinesAboveShouldBeOne() { + char[][] input = {{'*'}, + {'.'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, notNullValue()); + assertThat(output[0], is(new char[] {'*'})); + assertThat(output[1], is(new char[] {'1'})); + } + } diff -r 6dfb161c93cb -r fc2d79672709 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:17:15 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:29:17 2012 -0600 @@ -1,47 +1,58 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { char previousCell = 0; char nextCell = 0; + char aboveCell = 0; + if ((colIndex - 1) >= 0){ previousCell = row[colIndex -1]; } if (previousCell == '*') { ++numMines; } if ((colIndex + 1) < row.length){ nextCell = row[colIndex +1]; } if (nextCell == '*') { ++numMines; } + + if (rowIndex > 0) { + aboveCell = minefield[rowIndex -1][colIndex]; + } + if (aboveCell == '*') { + ++numMines; + } + + minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r fc2d79672709 -r 4d8e2bb35431 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:29:17 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:36:21 2012 -0600 @@ -1,58 +1,47 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { - char previousCell = 0; - char nextCell = 0; - char aboveCell = 0; - - if ((colIndex - 1) >= 0){ - previousCell = row[colIndex -1]; - } + char previousCell = ((colIndex - 1) >= 0) ? row[colIndex -1] : 0; + char nextCell = ((colIndex + 1) < row.length) ? row[colIndex +1] : 0; + char aboveCell = (rowIndex >0 ) ? minefield[rowIndex -1][colIndex] : 0; if (previousCell == '*') { ++numMines; - } - if ((colIndex + 1) < row.length){ - nextCell = row[colIndex +1]; } if (nextCell == '*') { ++numMines; } - if (rowIndex > 0) { - aboveCell = minefield[rowIndex -1][colIndex]; - } if (aboveCell == '*') { ++numMines; } - minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 4d8e2bb35431 -r 4d4873882f4e src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:36:21 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:46:32 2012 -0600 @@ -10,11 +10,11 @@ @Test public void shouldLoadNoMines() { char[][] input = new char[2][2]; input[0] = new char[] {'.', '.'}; input[1] = new char[] {'.', '.'}; - + Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output, notNullValue()); assertThat(output[0], is(new char[] {'0', '0'})); assertThat(output[1], is(new char[] {'0', '0'})); @@ -48,18 +48,28 @@ Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output[0], is(new char[] {'*', '2', '*'})); } + @Test - public void cellsWithMinesAboveShouldBeOne() { + public void cellsWithMineAboveShouldBeOne() { char[][] input = {{'*'}, {'.'}}; Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); - assertThat(output, notNullValue()); - assertThat(output[0], is(new char[] {'*'})); - assertThat(output[1], is(new char[] {'1'})); + assertThat(output, is(new char[][] {{'*'}, {'1'}})); } + @Test + public void cellsWithMineBelowShouldBeOne() { + char[][] input = {{'.'}, + {'*'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, is(new char[][] {{'1'}, {'*'}})); + } + + } diff -r 4d8e2bb35431 -r 4d4873882f4e src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:36:21 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:46:32 2012 -0600 @@ -1,47 +1,52 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { - char previousCell = ((colIndex - 1) >= 0) ? row[colIndex -1] : 0; - char nextCell = ((colIndex + 1) < row.length) ? row[colIndex +1] : 0; - char aboveCell = (rowIndex >0 ) ? minefield[rowIndex -1][colIndex] : 0; + char previousCell = (colIndex - 1 >= 0) ? row[colIndex -1] : 0; + char nextCell = (colIndex + 1 < row.length) ? row[colIndex +1] : 0; + char aboveCell = (rowIndex > 0) ? minefield[rowIndex -1][colIndex] : 0; + char belowCell = (rowIndex + 1 < minefield.length) ? minefield[rowIndex +1][colIndex] : 0; if (previousCell == '*') { ++numMines; } if (nextCell == '*') { ++numMines; } if (aboveCell == '*') { ++numMines; } + if (belowCell == '*') { + ++numMines; + } + minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 4d4873882f4e -r 43d6d5f34d2d src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 13:46:32 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 14:01:14 2012 -0600 @@ -69,7 +69,28 @@ Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output, is(new char[][] {{'1'}, {'*'}})); } + @Test + public void cellsWithMinesAboveAndBelowShouldBeTwo() { + char[][] input = {{'*'}, + {'.'}, + {'*'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, is(new char[][] {{'*'}, {'2'}, {'*'}})); + } + + @Test + public void cellsWithMineAboveAndLeftShouldBeOne() { + char[][] input = {{'*', '.'}, + {'.', '.'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, is(new char[][] {{'*', '1'}, + {'1', '1'}})); + } } diff -r 4d4873882f4e -r 43d6d5f34d2d src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 13:46:32 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 14:01:14 2012 -0600 @@ -1,52 +1,45 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { char previousCell = (colIndex - 1 >= 0) ? row[colIndex -1] : 0; char nextCell = (colIndex + 1 < row.length) ? row[colIndex +1] : 0; char aboveCell = (rowIndex > 0) ? minefield[rowIndex -1][colIndex] : 0; char belowCell = (rowIndex + 1 < minefield.length) ? minefield[rowIndex +1][colIndex] : 0; - - if (previousCell == '*') { - ++numMines; - } - - if (nextCell == '*') { - ++numMines; - } - - if (aboveCell == '*') { - ++numMines; - } - - if (belowCell == '*') { - ++numMines; + char upperLeftCell = (rowIndex > 0 && colIndex -1 >= 0) + ? minefield[rowIndex -1][colIndex-1] : 0; + + char[] neighbors = new char[] {previousCell, nextCell, aboveCell, belowCell, upperLeftCell}; + for (char currentNeighbor : neighbors) { + if (currentNeighbor == '*') { + ++numMines; + } } minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 43d6d5f34d2d -r 9fbefc8cf4d6 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 14:01:14 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 14:18:55 2012 -0600 @@ -91,6 +91,19 @@ char[][] output = minesweeper.sweep(); assertThat(output, is(new char[][] {{'*', '1'}, {'1', '1'}})); } + @Test + public void cellSurroundedByMinesShouldBeEight() { + char[][] input = {{'*', '*', '*'}, + {'*', '.', '*'}, + {'*', '*', '*'}}; + char[][] expectedOutput = {{'*', '*', '*'}, + {'*', '8', '*'}, + {'*', '*', '*'}}; + + Minesweeper minesweeper = new Minesweeper(input); + char[][] output = minesweeper.sweep(); + assertThat(output, is(expectedOutput)); + } } diff -r 43d6d5f34d2d -r 9fbefc8cf4d6 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 14:01:14 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 14:18:55 2012 -0600 @@ -1,45 +1,45 @@ package com.nicholas.tuck.katas.minesweeper; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { - char previousCell = (colIndex - 1 >= 0) ? row[colIndex -1] : 0; - char nextCell = (colIndex + 1 < row.length) ? row[colIndex +1] : 0; - char aboveCell = (rowIndex > 0) ? minefield[rowIndex -1][colIndex] : 0; - char belowCell = (rowIndex + 1 < minefield.length) ? minefield[rowIndex +1][colIndex] : 0; - char upperLeftCell = (rowIndex > 0 && colIndex -1 >= 0) - ? minefield[rowIndex -1][colIndex-1] : 0; + + int topRow = (rowIndex > 0) ? rowIndex -1 : 0; + int bottomRow = (rowIndex + 1 < minefield.length) ? rowIndex + 1 : rowIndex; + int leftCol = (colIndex - 1 >= 0) ? colIndex -1 : 0; + int rightCol = (colIndex + 1 < row.length) ? colIndex +1 : colIndex; - char[] neighbors = new char[] {previousCell, nextCell, aboveCell, belowCell, upperLeftCell}; - for (char currentNeighbor : neighbors) { - if (currentNeighbor == '*') { - ++numMines; + for (int neighborRow = topRow; neighborRow <= bottomRow; neighborRow++){ + for (int neighborCol = leftCol; neighborCol <= rightCol; neighborCol++){ + if (minefield[neighborRow][neighborCol] == '*'){ + numMines++; + } } } minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } } ****************************************** ****************************************** ************ Next Diff ******************* ****************************************** ****************************************** diff -r 9fbefc8cf4d6 -r 238b0ae2d2f5 src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java --- a/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 14:18:55 2012 -0600 +++ b/src/test/java/com/nicholas/tuck/katas/minesweeper/MinesweeperTest.java Fri Nov 30 14:44:09 2012 -0600 @@ -1,13 +1,28 @@ package com.nicholas.tuck.katas.minesweeper; import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; public class MinesweeperTest { + + public static final char[][] TWO_BY_TWO_ABOVE_LEFT_INPUT = {{'*', '.'}, + {'.', '.'}}; + public static final char[][] TWO_BY_TWO_ABOVE_LEFT_OUTPUT = {{'*', '1'}, + {'1', '1'}}; + public static final char[][] THREE_BY_THREE_SURRENDER_INPUT = {{'*', '*', '*'}, + {'*', '.', '*'}, + {'*', '*', '*'}}; + public static final char[][] THREE_BY_THREE_SURRENDER_OUTPUT = {{'*', '*', '*'}, + {'*', '8', '*'}, + {'*', '*', '*'}}; @Test public void shouldLoadNoMines() { char[][] input = new char[2][2]; input[0] = new char[] {'.', '.'}; @@ -82,28 +97,43 @@ assertThat(output, is(new char[][] {{'*'}, {'2'}, {'*'}})); } @Test public void cellsWithMineAboveAndLeftShouldBeOne() { - char[][] input = {{'*', '.'}, - {'.', '.'}}; - - Minesweeper minesweeper = new Minesweeper(input); + Minesweeper minesweeper = new Minesweeper(TWO_BY_TWO_ABOVE_LEFT_INPUT); char[][] output = minesweeper.sweep(); - assertThat(output, is(new char[][] {{'*', '1'}, - {'1', '1'}})); + assertThat(output, is(TWO_BY_TWO_ABOVE_LEFT_OUTPUT)); } @Test public void cellSurroundedByMinesShouldBeEight() { - char[][] input = {{'*', '*', '*'}, - {'*', '.', '*'}, - {'*', '*', '*'}}; - char[][] expectedOutput = {{'*', '*', '*'}, - {'*', '8', '*'}, - {'*', '*', '*'}}; + Minesweeper minesweeper = new Minesweeper(THREE_BY_THREE_SURRENDER_INPUT); + char[][] output = minesweeper.sweep(); + assertThat(output, is(THREE_BY_THREE_SURRENDER_OUTPUT)); + } + + @Test + public void complexFieldShouldBeSweptCorrectly() { + char[][] input ={{'*', '*', '.'}, + {'.', '.', '*'}, + {'.', '.', '*'}, + {'*', '.', '*'}}; + char[][] expectedOutput = {{'*', '*', '2'}, + {'2', '4', '*'}, + {'1', '4', '*'}, + {'*', '3', '*'}}; Minesweeper minesweeper = new Minesweeper(input); char[][] output = minesweeper.sweep(); assertThat(output, is(expectedOutput)); } + + @Test + public void sweepFieldsShouldSweepMultipleFields() { + List<char[][]> inputFields = Arrays.asList(TWO_BY_TWO_ABOVE_LEFT_INPUT, THREE_BY_THREE_SURRENDER_INPUT); + List<char[][]> outputFields = Minesweeper.sweepFields(inputFields); + + assertThat(outputFields.size(), is(equalTo(inputFields.size()))); + assertThat(outputFields.get(0), is(TWO_BY_TWO_ABOVE_LEFT_OUTPUT)); + assertThat(outputFields.get(1), is(THREE_BY_THREE_SURRENDER_OUTPUT)); + } } diff -r 9fbefc8cf4d6 -r 238b0ae2d2f5 src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java --- a/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 14:18:55 2012 -0600 +++ b/src/main/java/com/nicholas/tuck/katas/minesweeper/Minesweeper.java Fri Nov 30 14:44:09 2012 -0600 @@ -1,45 +1,58 @@ package com.nicholas.tuck.katas.minesweeper; + +import java.util.ArrayList; +import java.util.List; public class Minesweeper { private final char[][] minefield; public Minesweeper(char[][] input) { minefield = input; } public char[][] sweep() { for (int rowIndex = 0, minefieldLength = minefield.length; rowIndex < minefieldLength; rowIndex++) { char[] row = minefield[rowIndex]; for (int colIndex = 0, rowLength = row.length; colIndex < rowLength; colIndex++) { updateCell(rowIndex, colIndex); } } return minefield; } private void updateCell(int rowIndex, int colIndex) { char[] row = minefield[rowIndex]; char cell = row[colIndex]; int numMines = 0; if (cell != '*') { int topRow = (rowIndex > 0) ? rowIndex -1 : 0; int bottomRow = (rowIndex + 1 < minefield.length) ? rowIndex + 1 : rowIndex; int leftCol = (colIndex - 1 >= 0) ? colIndex -1 : 0; int rightCol = (colIndex + 1 < row.length) ? colIndex +1 : colIndex; for (int neighborRow = topRow; neighborRow <= bottomRow; neighborRow++){ for (int neighborCol = leftCol; neighborCol <= rightCol; neighborCol++){ if (minefield[neighborRow][neighborCol] == '*'){ numMines++; } } } minefield[rowIndex][colIndex] = Character.forDigit(numMines, 10); } } + + public static List<char[][]> sweepFields(List<char[][]> inputFields) { + List<char[][]> output = new ArrayList<char[][]>(); + + for (char[][] field : inputFields) { + Minesweeper minesweeper = new Minesweeper(field); + output.add(minesweeper.sweep()); + } + return output; + } }