码迷,mamicode.com
首页 > 其他好文 > 详细

随机生成河流

时间:2018-01-09 18:35:49      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:first   post   stat   set   string   switch   row   bool   cells   

  class Program
    {
        public class Row
        {
            public int Id { get; set; }
            public List<Cell> Cells { get; set; }
        }
        public class Cell
        {
            public int SeqNum { get; set; }

            public bool IsRiver { get; set; } = false;

            public Cell Up { get; set; }
            public Cell Down { get; set; }
            public Cell Left { get; set; }
            public Cell Right { get; set; }
        }
        public class Map
        {
            public List<Row> Rows { get; set; }
            public static Map Init(int rowCount, int cellCount)
            {
                var rows = new List<Row>();
                for (int x = 0; x < rowCount; x++)
                {
                    var cells = new List<Cell>();
                    for (int y = 0; y < cellCount; y++)
                    {
                        var cell = new Cell { SeqNum = y };
                        cells.Add(cell);
                    }
                    var row = new Row { Id = x, Cells = cells };
                    rows.Add(row);
                }

                var map = new Map { Rows = rows };

                foreach (var row in map.Rows)
                {
                    foreach (var cell in row.Cells)
                    {
                        var left = row.Cells.FirstOrDefault(x => x.SeqNum == cell.SeqNum - 1);
                        if (left != null)
                            cell.Left = left;

                        var right = row.Cells.FirstOrDefault(x => x.SeqNum == cell.SeqNum + 1);
                        if (right != null)
                            cell.Right = right;


                        var nexRow = map.Rows.FirstOrDefault(x => x.Id == row.Id + 1);
                        if (nexRow != null)
                        {
                            var down = nexRow.Cells.FirstOrDefault(x => x.SeqNum == cell.SeqNum);
                            if (down != null)
                                cell.Down = down;
                        }


                        var upRow = map.Rows.FirstOrDefault(x => x.Id == row.Id - 1);
                        if (upRow != null)
                        {
                            var up = upRow.Cells.FirstOrDefault(x => x.SeqNum == cell.SeqNum);
                            if (up != null)
                                cell.Up = up;
                        }
                    }
                }


                map = DrawRiver(map, rowCount, cellCount,25);
                map = DrawRiver(map, rowCount, cellCount,75);

                return map;
            }

            private static Map DrawRiver(Map map, int rowCount, int cellCount,int startRow)
            {
                var random = new Random(Guid.NewGuid().GetHashCode());

                var source = map.Rows[startRow].Cells[1];
                source.IsRiver = true;

                while (true)
                {
                    int direction = random.Next(0, 3);

                    switch (direction)
                    {
                        case 0:
                            if (source.Up != null)
                            {
                                source = source.Up;
                                source.IsRiver = true;
                            }
                            break;
                        //case 1:
                        //    if (source.Left != null)
                        //    {
                        //        source = source.Left;
                        //        source.IsRiver = true;
                        //    }
                        //    break;
                        case 2:
                            if (source.Down != null)
                            {
                                source = source.Down;
                                source.IsRiver = true;
                            }
                            break;
                        case 1:
                            if (source.Right != null)
                            {
                                source = source.Right;
                                source.IsRiver = true;
                            }
                            break;
                    }

                    if (source.SeqNum == 0 || source.SeqNum == cellCount - 1)
                        break;
                }
                return map;
            }
        }


        static void Main(string[] args)
        {
            var map = Map.Init(100, 100);

            foreach (var row in map.Rows)
            {
                string str = "";
                foreach (var cell in row.Cells)
                {
                    if (cell.IsRiver)
                        str += "";
                    else
                        str += "";
                }
                Console.WriteLine(str);
            }

            Console.ReadKey();
        }
    }

 

随机生成河流

标签:first   post   stat   set   string   switch   row   bool   cells   

原文地址:https://www.cnblogs.com/xuhongcai/p/8252649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!