示例#1
0
        /// <summary>
        /// Operate the move according to Operation o and dynamic info.
        /// </summary>
        /// <param name="o">Movement</param>
        /// <param name="info">Dynamic info</param>
        /// <returns>Whether the operation is available.</returns>
        public bool PlayerOperate(PlayerOperation o, BoardInfo info)
        {
            // Prepare variables.

            int rowNum    = RowNum;
            int columnNum = ColumnNum;
            int playerX   = info.Player.X;
            int playerY   = info.Player.Y;

            int  nxtY             = -1;
            int  nxtX             = -1;
            int  altY             = -1;
            int  altX             = -1;
            bool hasAlternateGrid = false;

            switch (o)
            {
            case PlayerOperation.Up:
            {
                if (playerY == 0)
                {
                    return(false);
                }
                nxtY             = playerY - 1;
                nxtX             = playerX;
                altY             = playerY - 2;
                altX             = playerX;
                hasAlternateGrid = (playerY > 1);
                break;
            }

            case PlayerOperation.Down:
            {
                if (playerY == rowNum - 1)
                {
                    return(false);
                }
                nxtY             = playerY + 1;
                nxtX             = playerX;
                altY             = playerY + 2;
                altX             = playerX;
                hasAlternateGrid = (playerY < rowNum - 2);
                break;
            }

            case PlayerOperation.Left:
            {
                if (playerX == 0)
                {
                    return(false);
                }
                nxtY             = playerY;
                nxtX             = playerX - 1;
                altY             = playerY;
                altX             = playerX - 2;
                hasAlternateGrid = (playerX > 1);
                break;
            }

            case PlayerOperation.Right:
            {
                if (playerX == columnNum - 1)
                {
                    return(false);
                }
                nxtY             = playerY;
                nxtX             = playerX + 1;
                altY             = playerY;
                altX             = playerX + 2;
                hasAlternateGrid = (playerX < columnNum - 2);
                break;
            }
            }


            // Logic Judgement.

            // next is brick
            if (GridMatrix[nxtY, nxtX] == GridType.Brick)
            {
                return(false);
            }

            // next is not brick
            else if (GridMatrix[nxtY, nxtX] == GridType.Ground || GridMatrix[nxtY, nxtX] == GridType.Target)
            {
                if (!hasAlternateGrid)
                {
                    throw new Exception("Map data invalid error.");
                }
                if (GridMatrix[altY, altX] == GridType.OutSide)
                {
                    throw new Exception("Map data invalid error.");
                }

                // Search if there is a box to move.

                foreach (var box in info.Boxes)
                {
                    if (nxtY == box.Y && nxtX == box.X)
                    {
                        // alternate grid is brick
                        if (GridMatrix[altY, altX] == GridType.Brick)
                        {
                            return(false);
                        }
                        else
                        {
                            // if alternate grid has a box, cannot move.
                            foreach (var boxB in info.Boxes)
                            {
                                if (boxB.Y == altY && boxB.X == altX)
                                {
                                    return(false);
                                }
                            }

                            // now the box could be moved.
                            box.Y = altY;
                            box.X = altX;
                            break;
                        }
                    }
                }
                ;

                // Move the player.
                info.Player.Y = nxtY;
                info.Player.X = nxtX;

                return(true);
            }

            // wrong GameState
            else
            {
                throw new Exception("Map Data damaged.");
            }
        }
示例#2
0
 public InfoPack(BoardInfo info, GameEngine engine)
 {
     Info   = info;
     Engine = engine;
 }
示例#3
0
        public void SetStartBoard(BoardInfo info, GameEngine engine)
        {
            GameState startState = new GameState(info);

            PBP = new PushBoxProblem(startState, engine);
        }
示例#4
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);
            }
        }
示例#5
0
 public GameState(BoardInfo info)
 {
     this.Info         = (BoardInfo)info.Clone();
     this.DistanceCost = -1;
     this.PathCost     = -1;
 }