Beispiel #1
0
 public State(State state) : this(state.RowSize, state.ColumnSize)
 {
     for (int i = 0; i < RowSize; i++)
     {
         for (int j = 0; j < ColumnSize; j++)
         {
             this.env[i, j] = state[i, j];
         }
     }
     Packman = new Packman(state.Packman.Row, state.Packman.Column);
     Data    = state.Data;
 }
Beispiel #2
0
 public EnvType this[int i, int j]
 {
     get
     {
         return(env[i, j]);
     }
     set
     {
         if (value == EnvType.Packman)
         {
             if (Packman != null)
             {
                 env[Packman.Row, Packman.Column] = EnvType.Empty;
             }
             Packman = new Packman(i, j);
         }
         env[i, j] = value;
     }
 }
Beispiel #3
0
        private static ParsedData ParseInputString(string inputString)
        {
            string[] lines = SplitStringByLine(inputString);

            if (ValidateColumns(lines) == false)
            {
                throw new Exception("Columns sizes must be same.");
            }

            int rowSize = lines.Length;
            int colSize = lines[0].Length;

            EnvType[,] env = new EnvType[rowSize, colSize];

            MemoryStream memStream = new MemoryStream(rowSize * colSize + 8);

            BinaryWriter writer = new BinaryWriter(memStream);

            Packman packman = null;

            writer.Write(rowSize);
            writer.Write(colSize);

            for (int i = 0, k = 0; i < rowSize; i++)
            {
                for (int j = 0; j < colSize; j++, k++)
                {
                    switch (lines[i][j])
                    {
                    case '%':
                        writer.Write((byte)EnvType.Wall);
                        env[i, j] = EnvType.Wall;
                        break;

                    case '.':
                        writer.Write((byte)EnvType.Food);
                        env[i, j] = EnvType.Food;
                        break;

                    case 'p':
                    case 'P':
                        writer.Write((byte)EnvType.Packman);
                        env[i, j] = EnvType.Packman;
                        packman   = new Packman(i, j);
                        break;

                    case ' ':
                        writer.Write((byte)EnvType.Empty);
                        env[i, j] = EnvType.Empty;
                        break;

                    default:
                        throw new Exception("Input string contains an invalid symbol.");
                    }
                }
            }

            memStream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(memStream);

            byte[] data = reader.ReadBytes(rowSize * colSize + 8);

            ParsedData parsedData = new ParsedData {
                Data = data, Environment = env, Packman = packman
            };

            return(parsedData);
        }