icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
branchindvdum
changeset 6 0dbbc8983926
child 7 251961e44b5f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 00:36:43 2012 +0400
     1.3 @@ -0,0 +1,58 @@
     1.4 +package ru.bosony.model.mine;
     1.5 +
     1.6 +import ru.bosony.model.cellscontents.CellContent;
     1.7 +import ru.bosony.model.io.TextRepresentable;
     1.8 +
     1.9 +public class Mine implements TextRepresentable {
    1.10 +
    1.11 +	public Cell[][]	cells;
    1.12 +	protected int	sizeX;
    1.13 +	protected int	sizeY;
    1.14 +
    1.15 +	public Mine(String text) {
    1.16 +		fromText(text);
    1.17 +	}
    1.18 +
    1.19 +	@Override
    1.20 +	public String toText() {
    1.21 +		String text = "";
    1.22 +		for (int y = 0; y < sizeY; y++) {
    1.23 +			for (int x = 0; x < sizeX; x++) {
    1.24 +				text += cells[x][y].getContent().toText();
    1.25 +			}
    1.26 +			text += "\n";
    1.27 +		}
    1.28 +		return text.replaceAll("\n*$", "");
    1.29 +	}
    1.30 +
    1.31 +	private void fromText(String text) {
    1.32 +		String[] rows = text.split("\n+");
    1.33 +		int y = rows.length;
    1.34 +		int x = 0;
    1.35 +		for (String row : rows) {
    1.36 +			if (x < row.length())
    1.37 +				x = row.length();
    1.38 +		}
    1.39 +		sizeX = x;
    1.40 +		sizeY = y;
    1.41 +		cells = new Cell[x][y];
    1.42 +		for (int curY = 0; curY < rows.length; curY++) {
    1.43 +			String row = rows[curY];
    1.44 +			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    1.45 +				CellContent content = null;
    1.46 +				if (curX < row.length())
    1.47 +					content = CellContent.fromText("" + row.charAt(curX));
    1.48 +				else
    1.49 +					content = CellContent.Empty;
    1.50 +				Cell cell = new Cell(curX + 1, rows.length - curY, content);
    1.51 +				cells[curX][curY] = cell;
    1.52 +			}
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56 +	@Override
    1.57 +	public String toString() {
    1.58 +		return toText();
    1.59 +	}
    1.60 +
    1.61 +}