# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342211803 -14400 # Node ID 0dbbc8983926cf8b984a4d3f006e8f31bf14d683 # Parent 162341642dd78ef4632a98bd6b8df81f2d1f4601 Parsing Mine from string diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Fri Jul 13 21:55:14 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Sat Jul 14 00:36:43 2012 +0400 @@ -7,20 +7,30 @@ * 13.07.2012 21:25:49 * */ -public abstract class CellContent implements TextRepresentable { +public enum CellContent implements TextRepresentable { + + ClosedLambdaLift("L"), + Earth("."), + Empty(" "), + Lambda("\\"), + MiningRobot("R"), + OpenLambdaLift("O"), + Rock("*"), + Wall("#"); + + private String text; + + private CellContent(String text) { + this.text = text; + } @Override - public CellContent fromText(String text) { - for (CellContent content : new CellContent[] { - new ClosedLambdaLift(), - new Earth(), - new Empty(), - new Lambda(), - new MiningRobot(), - new OpenLambdaLift(), - new Rock(), - new Wall() - }) { + public String toText() { + return text; + } + + public static CellContent fromText(String text) { + for (CellContent content : values()) { if (text != null && text.equals(content.toText())) return content; } diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/ClosedLambdaLift.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/ClosedLambdaLift.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:34 - * - */ -public class ClosedLambdaLift extends CellContent { - - @Override - public String toText() { - return "L"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/Earth.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/Earth.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:39 - * - */ -public class Earth extends CellContent { - - @Override - public String toText() { - return "."; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/Empty.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/Empty.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:42 - * - */ -public class Empty extends CellContent { - - @Override - public String toText() { - return " "; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/Lambda.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/Lambda.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -/** - * - */ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:37:31 - * - */ -public class Lambda extends CellContent { - - @Override - public String toText() { - return "\\"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/MiningRobot.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/MiningRobot.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:47 - * - */ -public class MiningRobot extends CellContent { - - @Override - public String toText() { - return "R"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/OpenLambdaLift.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/OpenLambdaLift.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:50 - * - */ -public class OpenLambdaLift extends CellContent { - - @Override - public String toText() { - return "O"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/Rock.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/Rock.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:52 - * - */ -public class Rock extends CellContent { - - @Override - public String toText() { - return "*"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/cellscontents/Wall.java --- a/icfp-model/src/main/java/ru/bosony/model/cellscontents/Wall.java Fri Jul 13 21:55:14 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package ru.bosony.model.cellscontents; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 13.07.2012 21:41:55 - * - */ -public class Wall extends CellContent { - - @Override - public String toText() { - return "#"; - } - -} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/io/TextRepresentable.java --- a/icfp-model/src/main/java/ru/bosony/model/io/TextRepresentable.java Fri Jul 13 21:55:14 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/io/TextRepresentable.java Sat Jul 14 00:36:43 2012 +0400 @@ -8,6 +8,5 @@ public interface TextRepresentable { public String toText(); - public TextRepresentable fromText(String text); } diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/mine/Cell.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sat Jul 14 00:36:43 2012 +0400 @@ -0,0 +1,38 @@ +package ru.bosony.model.mine; + +import ru.bosony.model.cellscontents.CellContent; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 13.07.2012 22:10:18 + * + */ +public class Cell { + + protected int x; + protected int y; + protected CellContent content; + + public Cell(int x, int y, CellContent content) { + this.x = x; + this.y = y; + this.content = content; + } + + public CellContent getContent() { + return content; + } + + public void setContent(CellContent content) { + this.content = content; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + +} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 00:36:43 2012 +0400 @@ -0,0 +1,58 @@ +package ru.bosony.model.mine; + +import ru.bosony.model.cellscontents.CellContent; +import ru.bosony.model.io.TextRepresentable; + +public class Mine implements TextRepresentable { + + public Cell[][] cells; + protected int sizeX; + protected int sizeY; + + public Mine(String text) { + fromText(text); + } + + @Override + public String toText() { + String text = ""; + for (int y = 0; y < sizeY; y++) { + for (int x = 0; x < sizeX; x++) { + text += cells[x][y].getContent().toText(); + } + text += "\n"; + } + return text.replaceAll("\n*$", ""); + } + + private void fromText(String text) { + String[] rows = text.split("\n+"); + int y = rows.length; + int x = 0; + for (String row : rows) { + if (x < row.length()) + x = row.length(); + } + sizeX = x; + sizeY = y; + cells = new Cell[x][y]; + for (int curY = 0; curY < rows.length; curY++) { + String row = rows[curY]; + for (int curX = 0; curX < row.length() || curX < x; curX++) { + CellContent content = null; + if (curX < row.length()) + content = CellContent.fromText("" + row.charAt(curX)); + else + content = CellContent.Empty; + Cell cell = new Cell(curX + 1, rows.length - curY, content); + cells[curX][curY] = cell; + } + } + } + + @Override + public String toString() { + return toText(); + } + +} diff -r 162341642dd7 -r 0dbbc8983926 icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java Sat Jul 14 00:36:43 2012 +0400 @@ -0,0 +1,241 @@ +package ru.bosony.tests; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import ru.bosony.model.cellscontents.CellContent; +import ru.bosony.model.mine.Cell; +import ru.bosony.model.mine.Mine; + +public class TestMineParsing { + + protected static String alignMap(String map) { + String alignedMap = ""; + String[] rows = map.split("\n"); + int y = rows.length; + int x = 0; + for (String row : rows) { + if (x < row.length()) + x = row.length(); + } + for (int curY = 0; curY < rows.length; curY++) { + String row = rows[curY]; + for (int curX = 0; curX < row.length() || curX < x; curX++) { + if (curX < row.length()) + alignedMap += row.charAt(curX); + else + alignedMap += " "; + } + alignedMap += "\n"; + } + return alignedMap.replaceAll("\n$", ""); + } + + @Test + public void testMap01() { + String map = + "######\n" + + "#. *R#\n" + + "# \\.#\n" + + "#\\ * #\n" + + "L .\\#\n" + + "######"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap02() { + String map = + "#######\n" + + "#..***#\n" + + "#..\\\\\\#\n" + + "#...**#\n" + + "#.*.*\\#\n" + + "LR....#\n" + + "#######"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap03() { + String map = + "########\n" + + "#..R...#\n" + + "#..*...#\n" + + "#..#...#\n" + + "#.\\.\\..L\n" + + "####**.#\n" + + "#\\.....#\n" + + "#\\..* .#\n" + + "########"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap04() { + String map = + "#########\n" + + "#.*..#\\.#\n" + + "#.\\..#\\.L\n" + + "#.R .##.#\n" + + "#.\\ ...#\n" + + "#..\\ ..#\n" + + "#...\\ ##\n" + + "#....\\ \\#\n" + + "#########"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap05() { + String map = + "############\n" + + "#..........#\n" + + "#.....*....#\n" + + "#..\\\\\\\\\\\\..#\n" + + "#. ....#\n" + + "#..\\\\\\\\\\\\\\.#\n" + + "#..\\.. .#\n" + + "#..\\.. ....#\n" + + "#..... ..* #\n" + + "#..### ### #\n" + + "#...R#\\#\\\\.#\n" + + "######L#####"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap06() { + String map = + "###############\n" + + "#\\\\\\.......** #\n" + + "#\\\\#.#####...##\n" + + "#\\\\#.....*##. #\n" + + "#\\#####\\...## #\n" + + "#\\......####* #\n" + + "#\\.######* #.\\#\n" + + "#\\.#. *...##.##\n" + + "#\\##. .. *...#\n" + + "#\\...... L#.#.#\n" + + "###########.#.#\n" + + "#\\..........#.#\n" + + "##.##########.#\n" + + "#R.#\\.........#\n" + + "###############"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap07() { + String map = + " #######\n" + + " ## *#\n" + + " ##R *##\n" + + " ##\\\\\\\\##\n" + + " ##....##\n" + + " ##..\\ . ##\n" + + " ## . L . ##\n" + + " ##\\\\\\# #\\\\\\\\##\n" + + " ###### #######"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap08() { + String map = + "##############\n" + + "#\\\\... ......#\n" + + "###.#. ...*..#\n" + + " #.#. ... ..#\n" + + "### #. \\ ..#\n" + + "#. .#..... **#######\n" + + "#.#\\#..... ..\\\\\\*. #\n" + + "#*\\\\#.###. ####\\\\\\ #\n" + + "#\\\\.#. ...## \\ #\n" + + "#\\#.#..... ....# \\ # \n" + + "###.#..... ....# ##\n" + + "#\\\\.#..... ....#\\ # \n" + + "########.. ..###*####\n" + + "#......... .........#\n" + + "#......... ....***..#\n" + + "#..\\\\\\\\\\ # ####.....#\n" + + "#........*R..\\\\\\ .#\n" + + "##########L##########"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap09() { + String map = + " #L#######\n" + + " #*** \\\\ #\n" + + " #\\\\\\ .. #\n" + + "#########.## ##########\n" + + "#.......\\ ..........* .#\n" + + "#*******\\......#....#\\\\ .#\n" + + "###\\.\\\\\\...**..#....... *#\n" + + "#*****\\\\ .\\\\..## #\\.#\n" + + "######### .... ##########\n" + + " # #\n" + + " ####*#### \n" + + " #.......#\n" + + "######### \\\\\\\\*##########\n" + + "#*\\\\ **# *..*\\ \\\\\\\\\\#\n" + + "#.\\**\\*** .....**.# \\\\##\\#\n" + + "#\\R...... .\\\\.. \\\\\\\\\\#\n" + + "##########################"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + + @Test + public void testMap10() { + String map = + "#############################\n" + + "#..........................\\#\n" + + "#..\\\\###...#.... ###.#\n" + + "#..\\*\\\\\\.. #.... ..##\\\\..\\#.#\n" + + "#..\\*\\.... #.... ..#\\#....#.#\n" + + "#...\\###.. #.... ....#....#.#\n" + + "#... ..... ..... .####......#\n" + + "#\\\\. #.... .......#\n" + + "#... #..#. .....*\\ ##.......#\n" + + "#.#....... ...#.. ....######\n" + + "#. ...#... ...#.\\ ....#..* #\n" + + "##........ ...#.. #....#.#\\\\#\n" + + "#.....*... .....*\\#\\\\.....*.#\n" + + "#.***.* .......*\\****.....#.#\n" + + "#.\\\\\\.. ................ .#\n" + + "#.##### .###### ##### #\n" + + "#....\\\\.................... #\n" + + "#....****...#.##.....\\\\\\\\..\\#\n" + + "#....\\\\\\\\...#.........*....\\#\n" + + "#....\\\\\\\\...#.\\\\. #\\###.\\#\n" + + "#.... ..#.... ...#\\\\\\\\. #\n" + + "#........ ..#.... ...#..... #\n" + + "#........ ........#R#\n" + + "###########################L#"; + Mine mine = new Mine(map); + String parsed = mine.toText(); + assertEquals(alignMap(map), parsed); + } + +}