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(); } }