Esempio n. 1
0
        /// <summary>
        /// Read the statistic and dynamic information from the file. checked.
        /// </summary>
        /// <returns></returns>
        private async Task <InfoPack> GetBoardInfo(int stageIndex)
        {
            MyPoint        player  = new MyPoint(-1, -1);
            List <MyPoint> boxes   = new List <MyPoint>();
            List <MyPoint> targets = new List <MyPoint>();

            GridType[,] gridMatrix;
            BoardInfo  info;
            GameEngine engine;

            try
            {
                var uri  = new Uri(String.Format("ms-appx:///Assets/Stages/{0:G}.txt", stageIndex));
                var file = await StorageFile.GetFileFromApplicationUriAsync(uri).AsTask().ConfigureAwait(false);

                var encoding = Encoding.GetEncoding("utf-8");

                using (Stream stream = await file.OpenStreamForReadAsync())
                {
                    using (StreamReader reader = new StreamReader(stream, encoding, false))
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            throw new FileLoadException("Wrong Stage File Format.");
                        }

                        string[] numStrings  = line.Split(' ');
                        int      rowNum      = Convert.ToInt32(numStrings[0], 10);
                        int      columnCount = Convert.ToInt32(numStrings[1], 10);
                        gridMatrix = new GridType[rowNum, columnCount];

                        for (int i = 0; i < rowNum; i++)
                        {
                            line = reader.ReadLine();
                            if (line == null)
                            {
                                throw new FileLoadException("Wrong Stage File Line Format.");
                            }
                            if (line.Length != columnCount)
                            {
                                throw new FileLoadException("Wrong Stage File Line Format.");
                            }

                            for (int j = 0; j < columnCount; j++)
                            {
                                gridMatrix[i, j] = BlockCode[line[j]];

                                if (line[j] == '@')
                                {
                                    // player
                                    player.Y = i;
                                    player.X = j;
                                    // i is row index -- y
                                    // j is column index -- x
                                }
                                else if (line[j] == '$')
                                {
                                    // box
                                    var box = new MyPoint(j, i);
                                    boxes.Add(box);
                                }
                                else if (line[j] == '*')
                                {
                                    // red box
                                    var box = new MyPoint(j, i);
                                    boxes.Add(box);
                                    var target = new MyPoint(j, i);
                                    targets.Add(target);
                                }
                                else if (line[j] == '.')
                                {
                                    // target list: only for the AI.
                                    var target = new MyPoint(j, i);
                                    targets.Add(target);
                                }
                            }
                        }

                        if (player.X < 0 || player.Y < 0)
                        {
                            throw new FileLoadException("No Player Position in Stage File.");
                        }

                        info   = new BoardInfo(player, boxes);
                        engine = new GameEngine(gridMatrix, targets);
                    }
                }

                return(new InfoPack(info, engine));
            }
            catch (Exception e)
            {
                var message = "The file could not be read:";
                message      += e.Message;
                PathText.Text = message;
                return(null);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Manhatton distance of two points.
 /// </summary>
 /// <param name="p1"></param>
 /// <param name="p2"></param>
 /// <returns></returns>
 private int Manhatton(MyPoint p1, MyPoint p2)
 {
     return(Math.Abs(p1.X - p2.X) + Math.Abs(p1.Y - p2.Y));
 }