コード例 #1
0
        private List <string> Calc(int x, int y, List <string> path, TipoTablero config, int step, int steps)
        {
            if (step > steps)
            {
                return(new List <string>());                // se me acabaron los movimientos
            }
            if (x < 0 || y < 0 || y == config.Heigth || x == config.Width)
            {
                return(new List <string>());                                                          // se sale del tablero
            }
            var X     = x.ToString("D3");
            var Y     = y.ToString("D3");
            var coord = X + Y;

            if (path.Contains(coord))
            {
                return(new List <string>());
            }

            var acum = new List <string>();

            if (step > 0)
            {
                acum.Add(coord);
            }
            path.Add(coord);

            // up
            var dest = X + (y - 1).ToString("D3");

            if (!HasWall(coord, dest, config))
            {
                acum.AddRange(Calc(x, y - 1, path.ToArray().ToList(), config, step + 1, steps));
            }

            // down
            dest = X + (y + 1).ToString("D3");
            if (!HasWall(coord, dest, config))
            {
                acum.AddRange(Calc(x, y + 1, path.ToArray().ToList(), config, step + 1, steps));
            }

            // left
            dest = (x - 1).ToString("D3") + Y;
            if (!HasWall(coord, dest, config))
            {
                acum.AddRange(Calc(x - 1, y, path.ToArray().ToList(), config, step + 1, steps));
            }

            // rigth
            dest = (x + 1).ToString("D3") + Y;
            if (!HasWall(coord, dest, config))
            {
                acum.AddRange(Calc(x + 1, y, path.ToArray().ToList(), config, step + 1, steps));
            }

            return(acum);
        }
コード例 #2
0
 private bool HasWall(string c1, string c2, TipoTablero tipoTablero)
 {
     return(tipoTablero.Walls.Any(p => (p.V1 == c1 && p.V2 == c2) || (p.V1 == c2 && p.V2 == c1)));
 }