/// <summary> /// Load a "<Row>" element. A row element is composed of X number of /// child tile elements, where X is the width of the board. The tile /// elements can be "<Empty>", "<Tile>", "<Outputter>", or "<Receiver>" /// elements. /// </summary> /// <param name="rowElement"></param> /// <param name="currentRow"></param> private void LoadRow(XmlElement rowElement, List<TileInfo> currentRow) { foreach (var child in rowElement.ChildElements()) { switch (child.Name) { case EMPTY_ELEMENT: currentRow.Add(ReadEmptyTileElement(child)); break; case TILE_ELEMENT: currentRow.Add(ReadTileElement(child)); break; default: Error_InvalidBoardTileElement(child.Name); break; } } }
private void LoadBoard(XmlElement boardElement) { int dimX = INDETERMINATE_ROW_WIDTH, dimY = boardElement.ChildNodes.Count; var rows = new List<List<TileInfo>>(); var currentRow = new List<TileInfo>(); var emptyRowIndices = new List<EmptyRowIndexStruct>(); foreach (var child in boardElement.ChildElements()) { if (child.Name == ROW_ELEMENT) { var len = child.ChildNodes.Count; if (dimX == INDETERMINATE_ROW_WIDTH) dimX = len; else if (dimX != len) Error_InconsistentBoardRowWidths(); LoadRow(child, currentRow); } else if (child.Name == EMPTY_ROW_ELEMENT) { bool rowOpen = AttemptReadOpenAttribute(child); // If we haven't yet determined the width of the board, we // have to insert it afterwards. if (dimX == INDETERMINATE_ROW_WIDTH) { emptyRowIndices.Add( new EmptyRowIndexStruct { Index = rows.Count - 1, Open = rowOpen }); continue; } else { for (int i = 0; i < dimX; i++) currentRow.Add(new TileInfo(TileType.Empty, rowOpen)); } } rows.Add(currentRow); currentRow = new List<TileInfo>(); } // If we could not determine the width of the board. This occurs when // the board is entirely composed of terminal row elements (like "<EmptyRow/>"). if (dimX == INDETERMINATE_ROW_WIDTH) Error_IndeterminateBoardWidth(); // Reinsert the empty row indices. foreach (var index in emptyRowIndices) { var newRow = new List<TileInfo>(); for (int i = 0; i < dimX; i++) newRow.Add(new TileInfo(TileType.Empty, index.Open)); rows.Insert(index.Index, newRow); } BoardDimensions = new Point(dimX, dimY); Board = new TileInfo[dimY, dimX]; var outputters = new List<Point>(); var receivers = new List<Point>(); int j = 0; foreach (var row in rows) { foreach (var info in row) { Board[j / dimX, j % dimX] = info; if (info.Type == TileType.Outputter) outputters.Add(new Point(j % dimX, j / dimX)); else if (info.Type == TileType.Receiver) receivers.Add(new Point(j % dimX, j / dimX)); j++; } } Outputters = outputters.ToArray(); Receivers = receivers.ToArray(); }