예제 #1
0
        public PlayArea(char[,] area, ParamsCollection paramsCollection)
        {
            _paramsCollection = paramsCollection;
            _area             = new int[area.GetLength(0), area.GetLength(1)];

            for (int col = 0; col < area.GetLength(0); col++)
            {
                for (int row = 0; row < area.GetLength(1); row++)
                {
                    TryMakeCellIndex(col, row, out ICellIndex cellIndex);

                    switch (area[col, row])
                    {
                    case '.':
                    case '\0':
                        cellIndex.SetEmptyCell();
                        break;

                    case 'X':
                    case '0':
                        cellIndex.SetLifeCell();
                        break;

                    default:
                        throw new Exception($"Invalid cell input {area[col, row]}");
                    }
                }
            }
        }
예제 #2
0
        public PlayData(string[] data)
        {
            #region Dictionary<string, string> GetParams(ref int pos)

            ParamsCollection GetParams(ref int pos)
            {
                Dictionary <string, string> dataParams = new Dictionary <string, string>();

                while (pos < data.Length && !string.IsNullOrWhiteSpace(data[pos]))
                {
                    var parts = data[pos].Split(':');
                    dataParams.Add(parts[0], parts[1]);

                    pos++;
                }

                return(new ParamsCollection(dataParams));
            }

            #endregion

            #region void GetDimensions(ref int pos, out int cols, out int rows)

            void GetDimensions(ref int pos, out int cols, out int rows)
            {
                if (pos >= data.Length)
                {
                    cols = rows = 0;
                    return;
                }

                string[] parts = data[pos].Split(' ');
                cols = int.Parse(parts[0]);
                rows = int.Parse(parts[1]);
            }

            #endregion

            #region char[,] GetArea(ref int pos, int cols, int rows)

            PlayArea GetArea(ref int pos, int cols, int rows)
            {
                char[,] area = new char[cols, rows];

                int row = 0;

                while (pos < data.Length)
                {
                    int col = 0;
                    foreach (var c in data[pos])
                    {
                        area[col++, row] = c;
                    }

                    pos += 1;
                    row += 1;
                }

                return(new PlayArea(area, Params));
            }

            #endregion

            Command = data[0];

            int parsingRow = 1;

            Params = GetParams(ref parsingRow);

            parsingRow += 1;

            GetDimensions(ref parsingRow, out var colCount, out var rowCount);

            parsingRow += 1;

            Area = GetArea(ref parsingRow, colCount, rowCount);
        }