Esempio n. 1
0
        public override string ToString()
        {
            string s = $"{FromCell.ToCoordinateString()}-{ToCell.ToCoordinateString()}";

            return(PromoteTo != PieceType.None
                ? $"{s}={PromoteTo.With(Color.White).GetSymbol()}"
                : s);
        }
    public static int CellDistanceToEnd(MazeCell mazeCell, FromCell fromCell)
    {
        var distance = 0;

        if (mazeCell.endCell)
        {
            return(++distance);
        }

        if (mazeCell.topCell != null && fromCell != FromCell.top)
        {
            distance += CellDistanceToEnd(mazeCell.topCell, FromCell.bottom);
        }

        if (mazeCell.bottomCell != null && fromCell != FromCell.bottom)
        {
            distance += CellDistanceToEnd(mazeCell.bottomCell, FromCell.top);
        }

        if (mazeCell.leftCell != null && fromCell != FromCell.left)
        {
            distance += CellDistanceToEnd(mazeCell.leftCell, FromCell.right);
        }

        if (mazeCell.rightCell != null && fromCell != FromCell.right)
        {
            distance += CellDistanceToEnd(mazeCell.rightCell, FromCell.left);
        }

        if (distance > 0)
        {
            distance++;
        }

        return(distance);
    }
    public MazeCell NextCellToEnd(MazeCell mazeCell, FromCell fromCell)
    {
        if (mazeCell.endCell)
        {
            return(mazeCell);
        }

        if (mazeCell.topCell != null && fromCell != FromCell.top)
        {
            var topCell = NextCellToEnd(mazeCell.topCell, FromCell.bottom);
            if (topCell != null && fromCell != FromCell.player)
            {
                return(mazeCell);
            }
            else if (topCell != null && fromCell == FromCell.player)
            {
                return(topCell);
            }
        }

        if (mazeCell.bottomCell != null && fromCell != FromCell.bottom)
        {
            var bottomCell = NextCellToEnd(mazeCell.bottomCell, FromCell.top);
            if (bottomCell != null && fromCell != FromCell.player)
            {
                return(mazeCell);
            }
            else if (bottomCell != null && fromCell == FromCell.player)
            {
                return(bottomCell);
            }
        }

        if (mazeCell.leftCell != null && fromCell != FromCell.left)
        {
            var leftCell = NextCellToEnd(mazeCell.leftCell, FromCell.right);
            if (leftCell != null && fromCell != FromCell.player)
            {
                return(mazeCell);
            }
            else if (leftCell != null && fromCell == FromCell.player)
            {
                return(leftCell);
            }
        }

        if (mazeCell.rightCell != null && fromCell != FromCell.right)
        {
            var rightCell = NextCellToEnd(mazeCell.rightCell, FromCell.left);
            if (rightCell != null && fromCell != FromCell.player)
            {
                return(mazeCell);
            }
            else if (rightCell != null && fromCell == FromCell.player)
            {
                return(rightCell);
            }
        }

        return(null);
    }
    public static List <MazeCell> GetPathFromCell(MazeCell mazeCell, FromCell fromCell)
    {
        var path = new List <MazeCell>();

        if ((mazeCell.cellType == CellType.TJunction || mazeCell.cellType == CellType.XJunction) && fromCell != FromCell.player)
        {
            path.Add(mazeCell);
            mazeCell.calculated++;
            return(path);
        }

        if (mazeCell.topCell != null && fromCell != FromCell.top &&
            (
                (mazeCell.topCell.calculated < 1 && (mazeCell.topCell.cellType == CellType.DeadEnd || mazeCell.topCell.cellType == CellType.Corner || mazeCell.topCell.cellType == CellType.Straight)) ||
                (mazeCell.topCell.calculated < 3 && mazeCell.topCell.cellType == CellType.TJunction) ||
                (mazeCell.topCell.calculated < 4 && mazeCell.topCell.cellType == CellType.XJunction)
            )
            )
        {
            path.AddRange(GetPathFromCell(mazeCell.topCell, FromCell.bottom));
            path.Add(mazeCell);
            mazeCell.calculated++;
            return(path);
        }

        if (mazeCell.bottomCell != null && fromCell != FromCell.bottom &&
            (
                (mazeCell.bottomCell.calculated < 1 && (mazeCell.bottomCell.cellType == CellType.DeadEnd || mazeCell.bottomCell.cellType == CellType.Corner || mazeCell.bottomCell.cellType == CellType.Straight)) ||
                (mazeCell.bottomCell.calculated < 3 && mazeCell.bottomCell.cellType == CellType.TJunction) ||
                (mazeCell.bottomCell.calculated < 4 && mazeCell.bottomCell.cellType == CellType.XJunction)
            )
            )
        {
            path.AddRange(GetPathFromCell(mazeCell.bottomCell, FromCell.top));
            path.Add(mazeCell);
            mazeCell.calculated++;
            return(path);
        }

        if (mazeCell.leftCell != null && fromCell != FromCell.left &&
            (
                (mazeCell.leftCell.calculated < 1 && (mazeCell.leftCell.cellType == CellType.DeadEnd || mazeCell.leftCell.cellType == CellType.Corner || mazeCell.leftCell.cellType == CellType.Straight)) ||
                (mazeCell.leftCell.calculated < 3 && mazeCell.leftCell.cellType == CellType.TJunction) ||
                (mazeCell.leftCell.calculated < 4 && mazeCell.leftCell.cellType == CellType.XJunction)
            )
            )
        {
            path.AddRange(GetPathFromCell(mazeCell.leftCell, FromCell.right));
            path.Add(mazeCell);
            mazeCell.calculated++;
            return(path);
        }

        if (mazeCell.rightCell != null && fromCell != FromCell.right &&
            (
                (mazeCell.rightCell.calculated < 1 && (mazeCell.rightCell.cellType == CellType.DeadEnd || mazeCell.rightCell.cellType == CellType.Corner || mazeCell.rightCell.cellType == CellType.Straight)) ||
                (mazeCell.rightCell.calculated < 3 && mazeCell.rightCell.cellType == CellType.TJunction) ||
                (mazeCell.rightCell.calculated < 4 && mazeCell.rightCell.cellType == CellType.XJunction)
            )
            )
        {
            path.AddRange(GetPathFromCell(mazeCell.rightCell, FromCell.left));
            path.Add(mazeCell);
            mazeCell.calculated++;
            return(path);
        }

        path.Add(mazeCell);
        mazeCell.calculated++;

        return(path);
    }