Exemple #1
0
        public override Tuple <string, string> getResult()
        {
            Max     = 500;
            TheGrid = new Grid(Max, Max, 2.0f);
            Stack <Coordinate> NestStack = new Stack <Coordinate>();

            Rooms = new List <Coordinate>();
            Coordinate                E = new Coordinate(0, 1);
            Coordinate                W = new Coordinate(0, -1);
            Coordinate                N = new Coordinate(1, 0);
            Coordinate                S = new Coordinate(-1, 0);
            HashSet <Coordinate>      CurrentPositions = new HashSet <Coordinate>();
            List <List <Coordinate> > NextPositions    = new List <List <Coordinate> >();

            CurrentPositions.Add(new Coordinate(Max / 2, Max / 2));
            char LastChar = ' ';
            int  Lenght   = Input.Length;
            int  Progress = 0;

            foreach (char c in Input)
            {
                bool       Poppipop      = false;
                bool       UselessBranch = false;
                bool       PushiPsush    = false;
                Coordinate NextPosition  = new Coordinate(0, 0);
                foreach (Coordinate CurrentPosition in CurrentPositions)
                {
                    //Coordinate NextPosition = new Coordinate(CurrentPosition);
                    switch (c)
                    {
                    case 'E':
                        CurrentPosition.AddTo(E);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        CurrentPosition.AddTo(E);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        if (!Rooms.Contains(CurrentPosition))
                        {
                            Rooms.Add(new Coordinate(CurrentPosition));
                        }
                        break;

                    case 'W':
                        CurrentPosition.AddTo(W);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        CurrentPosition.AddTo(W);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        if (!Rooms.Contains(CurrentPosition))
                        {
                            Rooms.Add(new Coordinate(CurrentPosition));
                        }
                        break;

                    case 'N':
                        CurrentPosition.AddTo(N);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        CurrentPosition.AddTo(N);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        if (!Rooms.Contains(CurrentPosition))
                        {
                            Rooms.Add(new Coordinate(CurrentPosition));
                        }
                        break;

                    case 'S':
                        CurrentPosition.AddTo(S);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        CurrentPosition.AddTo(S);
                        TheGrid.SetCellCost(CurrentPosition.GetPosition(), 1);
                        if (!Rooms.Contains(CurrentPosition))
                        {
                            Rooms.Add(new Coordinate(CurrentPosition));
                        }
                        break;

                    case '(':
                        PushiPsush   = true;
                        NextPosition = new Coordinate(CurrentPosition);
                        break;

                    case ')':
                        Poppipop      = true;
                        UselessBranch = LastChar == '|';
                        NextPositions.Last().Add(new Coordinate(CurrentPosition));
                        CurrentPosition.Assimilate(NestStack.Peek());
                        //if (LastChar != '|')
                        //    NextPosition = new Coordinate( NestStack.Peek());
                        //else
                        //    NextPositions.Add(new Coordinate( NestStack.Peek()));
                        break;

                    case '|':
                        NextPositions.Last().Add(new Coordinate(CurrentPosition));
                        CurrentPosition.Assimilate(NestStack.Peek());
                        break;

                    default:
                        break;
                    }
                    LastChar = c;
                }
                if (PushiPsush)
                {
                    NextPositions.Add(new List <Coordinate>());
                    NestStack.Push(new Coordinate(NextPosition));
                }
                if (Poppipop)
                {
                    if (!UselessBranch)
                    {
                        foreach (Coordinate coo in NextPositions.Last())
                        {
                            if (!CurrentPositions.Contains(coo))
                            {
                                CurrentPositions.Add(coo);
                            }
                        }
                        //CurrentPositions.AddRange(new List<Coordinate>(NextPositions.Last()));
                    }
                    NextPositions.Remove(NextPositions.Last());
                    NestStack.Pop();
                }
                Progress++;
            }
            StringBuilder OutString = new StringBuilder();

            for (int x = 0; x < Max; x++)
            {
                for (int y = 0; y < Max; y++)
                {
                    if (TheGrid.GetCellCost(new Position(x, y)) != 1)
                    {
                        TheGrid.BlockCell(new Position(x, y));
                    }
                }
            }
            for (int x = Max / 2 + 50; x > Max / 2 - 50; x--)
            {
                for (int y = Max / 2 - 50; y < Max / 2 + 50; y++)
                {
                    if (x == Max / 2 && y == Max / 2)
                    {
                        OutString.Append("X");
                    }
                    else
                    if (TheGrid.GetCellCost(new Position(x, y)) == 1)
                    {
                        OutString.Append(".");
                    }
                    else
                    {
                        OutString.Append("#");
                    }
                }
                OutString.Append("\r\n");
            }
            int Sum  = 0;
            int Sum2 = 0;

            //return Tuple.Create(OutString.ToString(),"");
            return(Tuple.Create(getPartOne(), Sum2.ToString() + "\n" + OutString.ToString()));
        }