示例#1
0
        public override bool CanMove(string direction)
        {
            Square requestedSquare = null;

            switch (direction)
            {
            case "Right":
                requestedSquare = CurrentSquare.RightSquare;

                if (requestedSquare.PlayObject is Boulder)
                {
                    Boulder boulder = (Boulder)requestedSquare.PlayObject;
                    return(boulder.MoveToRight());
                }
                break;

            case "Left":
                requestedSquare = CurrentSquare.LeftSquare;

                if (requestedSquare.PlayObject is Boulder)
                {
                    Boulder boulder = (Boulder)requestedSquare.PlayObject;
                    return(boulder.MoveToLeft());
                }
                break;

            case "Up":
                requestedSquare = CurrentSquare.UpSquare;
                break;

            case "Down":
                requestedSquare = CurrentSquare.DownSquare;
                break;

            default:
                return(false);
            }

            if (requestedSquare.PlayObject is IGatherable)
            {
                CollectGatherable((IGatherable)requestedSquare.PlayObject);
                return(true);
            }
            else if (requestedSquare.PlayObject is Creature || requestedSquare.PlayObject is Wall || requestedSquare.PlayObject is Boulder)
            {
                return(false);
            }
            else if (requestedSquare is ExitSquare)
            {
                return(GatheredAllItems());
            }

            return(true);
        }
        public Square CreatePlayGround(string[] rows)
        {
            // From string [] to square[][]
            Square[][] squares    = new Square[rows.Length][];
            int        arrayIndex = 0;

            foreach (string row in rows)
            {
                squares[arrayIndex] = new Square[row.Length];
                int rowIndex = 0;
                foreach (char square in row)
                {
                    Square      currentSquare = null;
                    PlayElement playElement   = null;
                    switch (square)
                    {
                    case 'R':
                        currentSquare = new NormalSquare();
                        playElement   = new Rockford(currentSquare);
                        PlayElements["Players"].Add(playElement);
                        break;

                    case 'M':
                        currentSquare = new MudSquare();
                        break;

                    case 'B':
                        currentSquare = new NormalSquare();
                        playElement   = new Boulder(currentSquare);
                        break;

                    case 'D':
                        currentSquare = new NormalSquare();
                        playElement   = new Diamond(currentSquare);
                        PlayElements["Gatherables"].Add(playElement);
                        break;

                    case 'W':
                        currentSquare = new NormalSquare();
                        playElement   = new NormalWall(currentSquare);
                        break;

                    case 'S':
                        currentSquare = new NormalSquare();
                        playElement   = new SteelWall(currentSquare);
                        break;

                    case 'F':
                        currentSquare = new NormalSquare();
                        playElement   = new FireFly(currentSquare);
                        PlayElements["Enemies"].Add(playElement);
                        break;

                    case 'E':
                        currentSquare = new ExitSquare();
                        break;

                    case ' ':
                        currentSquare = new NormalSquare();
                        break;
                    }
                    if (playElement != null)
                    {
                        currentSquare.AddPlayElement(playElement);
                    }
                    squares[arrayIndex][rowIndex] = currentSquare;
                    rowIndex++;
                }
                arrayIndex++;
            }
            AssignPlayersToEnemies();
            AssignToCollectItemsToPlayers();
            AssignSideSquares(squares);
            return(squares[0][0]); // return first square of the sequence
        }