Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5 from gnomo567/master

Primera version
  • Loading branch information...
commit 6ab1670b229018d1fb0ed08e93c689a4f9f55b9e 2 parents aa1bdae + fc1ef73
@12meses12katas 12meses12katas authored
View
17 gnomo567/.gitignore
@@ -0,0 +1,17 @@
+## generic files to ignore
+*~
+*.lock
+*.DS_Store
+*.swp
+*.out
+
+#ignore everything under the 'bin' folder
+bin/
+
+#java specific
+*.class
+
+#ignore the eclipse configuration files
+.classpath
+.project
+
View
1  gnomo567/README
@@ -0,0 +1 @@
+Primera version y con muchas dudas. Hay que seguir estudiando la teoría. :-)
View
82 gnomo567/src/com/docekatas/minesweeper/Field.java
@@ -0,0 +1,82 @@
+package com.docekatas.minesweeper;
+
+import java.util.ArrayList;
+
+public class Field {
+
+ private int rows = 0;
+ private int cols = 0;
+ private ArrayList<String> content;
+ private int[][] results;
+
+ public Field(String string) {
+ content = new ArrayList<String>();
+
+ if( string != null && !string.isEmpty() ) {
+ String[] lines = string.split("\n");
+ boolean firstLine = true;
+
+ for (String line : lines) {
+ if( firstLine ) {
+ getFieldSize(line);
+ firstLine = false;
+ }
+ else {
+ content.add(line);
+ }
+ }
+ results = new int[rows][cols];
+ }
+ }
+
+ private void getFieldSize(String firstline) {
+ String[] vars = firstline.split(" ");
+ rows = Integer.parseInt(vars[0]);
+ cols = Integer.parseInt(vars[1]);
+ }
+
+ public String getResult() {
+ if( rows == 0 ) {
+ return "";
+ }
+ else {
+ populateResult();
+ return getResultString();
+ }
+ }
+
+ private String getResultString() {
+ String res = "";
+ for(int i = 0; i < rows; i++) {
+ for(int j = 0; j < cols; j++ ) {
+ if(content.get(i).charAt(j) == '*')
+ res = res.concat("*");
+ else
+ res = res.concat(String.valueOf(results[i][j]));
+ }
+ res = res.concat("\n");
+ }
+ return res;
+ }
+
+ private void populateResult() {
+ for(int i = 0; i < rows; i++) {
+ for(int j = 0; j < cols; j++ ) {
+ if(content.get(i).charAt(j) == '*') {
+ incrementAdjacents(i,j);
+ }
+ }
+ }
+ }
+
+ private void incrementAdjacents(int row, int col) {
+ for(int i = row - 1; i <= row + 1; i++) {
+ for(int j = col - 1; j <= col + 1; j++ ) {
+ if( i >= 0 && i < rows && j >= 0 && j < cols && content.get(i).charAt(j) != '*') {
+ results[i][j]++;
+ }
+ }
+ }
+ }
+
+}
View
36 gnomo567/src/com/docekatas/minesweeper/Minesweeper.java
@@ -0,0 +1,36 @@
+package com.docekatas.minesweeper;
+
+import java.util.ArrayList;
+
+public class Minesweeper {
+
+ private ArrayList<Field> fields = new ArrayList<Field>();
+
+ public Minesweeper(String string) {
+ String field = "";
+ String[] lines = string.split("\n");
+
+ int i = 0;
+ while(!lines[i].equalsIgnoreCase("0 0")) {
+ field = "";
+ do {
+ field = field.concat(lines[i]).concat("\n");
+ i++;
+ } while(lines[i].matches("[\\*.]*"));
+ fields.add(new Field(field));
+ }
+ }
+
+ public String getResult() {
+ String res = "";
+ int i = 1;
+
+ for(Field f : fields) {
+ res = res.concat("Field #").concat(String.valueOf(i)).concat(":\n");
+ res = res.concat(f.getResult()).concat("\n");
+ i++;
+ }
+ return res;
+ }
+
+}
View
11 gnomo567/test/com/docekatas/minesweeper/AllTests.java
@@ -0,0 +1,11 @@
+package com.docekatas.minesweeper;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+@RunWith(Suite.class)
+@SuiteClasses({ FieldTest.class, MinesweeperTest.class })
+public class AllTests {
+
+}
View
33 gnomo567/test/com/docekatas/minesweeper/FieldTest.java
@@ -0,0 +1,33 @@
+package com.docekatas.minesweeper;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class FieldTest {
+
+ @Test
+ public final void testEmptyField() {
+ Field field = new Field("");
+ assertEquals("EmptyField", "", field.getResult());
+ }
+
+ @Test
+ public final void testUnitaryFields() {
+ Field field = new Field("1 1\n.");
+ assertEquals("UnitaryField", "0\n", field.getResult());
+ field = new Field("1 1\n*");
+ assertEquals("UnitaryField", "*\n", field.getResult());
+ }
+
+ @Test
+ public final void testComplexFields() {
+ Field field = new Field("2 2\n..\n..");
+ assertEquals("ComplexField", "00\n00\n", field.getResult());
+ field = new Field("2 2\n*.\n..");
+ assertEquals("ComplexField", "*1\n11\n", field.getResult());
+ field = new Field("3 5\n**...\n.....\n.*...");
+ assertEquals("ComplexField", "**100\n33200\n1*100\n", field.getResult());
+ }
+
+}
View
39 gnomo567/test/com/docekatas/minesweeper/MinesweeperTest.java
@@ -0,0 +1,39 @@
+package com.docekatas.minesweeper;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class MinesweeperTest {
+
+ @Test
+ public final void test() {
+ Minesweeper mines = new Minesweeper(
+ "4 4\n" +
+ "*...\n" +
+ "....\n" +
+ ".*..\n" +
+ "....\n" +
+ "3 5\n" +
+ "**...\n" +
+ ".....\n" +
+ ".*...\n" +
+ "0 0"
+ );
+ assertEquals(
+ "Minesweeper",
+ "Field #1:\n" +
+ "*100\n" +
+ "2210\n" +
+ "1*10\n" +
+ "1110\n" +
+ "\n" +
+ "Field #2:\n" +
+ "**100\n" +
+ "33200\n" +
+ "1*100\n" +
+ "\n",
+ mines.getResult());
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.