这看起来很有趣,所以我决定尝试一下。
输出:
Castle Data:
4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
Castle:
+--+--+--+--+--+--+--+
| | | |
+--+ +--+ + +--+ +
| | | | | | |
+ +--+ +--+ +--+ +
| | | | | |
+ +--+--+ +--+ + +
| | | |
+--+--+--+--+--+--+--+
Searching for rooms...
New room found at row 0, col 0.
New room found at row 0, col 2.
New room found at row 0, col 4.
New room found at row 1, col 5.
New room found at row 2, col 3.
Total rooms: 5
Press any key to quit...
代码:
class Program
{
static void Main(string[] args)
{
string data =
"4" + Environment.NewLine +
"7" + Environment.NewLine +
"11 6 11 6 3 10 6" + Environment.NewLine +
"7 9 6 13 5 15 5" + Environment.NewLine +
"1 10 12 7 13 7 5" + Environment.NewLine +
"13 11 10 8 10 12 13" + Environment.NewLine;
string[] dataFromFile = data.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("Castle Data:");
foreach(string row in dataFromFile)
{
Console.WriteLine(row);
}
Console.WriteLine();
CastleRoom[,] castle = CastleFromData(dataFromFile);
if (castle != null)
{
Console.WriteLine("Castle:");
DisplayCastle(castle);
Console.WriteLine();
Console.WriteLine("Searching for rooms...");
int roomCount = 0;
for(int row=0; row<castle.GetLength(0);row++)
{
for(int col=0; col<castle.GetLength(1); col++)
{
if (!castle[row, col].Visited)
{
Console.WriteLine("New room found at row " + row + ", col " + col + ".");
roomCount++;
VisitRoomAt(castle, row, col);
}
}
}
Console.WriteLine();
Console.WriteLine("Total rooms: " + roomCount);
Console.WriteLine();
}
Console.Write("Press any key to quit...");
Console.ReadKey();
}
static void VisitRoomAt(CastleRoom[,] castle, int row, int col)
{
if (castle!=null && row>=0 && row<=castle.GetUpperBound(0) && col>=0 && col<=castle.GetUpperBound(1))
{
CastleRoom room = castle[row, col];
if (room!=null && !room.Visited)
{
room.Visited = true;
if (!room.HasWestWall) { VisitRoomAt(castle, row, col-1); }
if (!room.HasNorthWall) { VisitRoomAt(castle, row-1, col); }
if (!room.HasEastWall) { VisitRoomAt(castle, row, col + 1); }
if (!room.HasSouthWall) { VisitRoomAt(castle, row + 1, col); }
}
}
}
static void DisplayCastle(CastleRoom[,] castle)
{
for (int row = 0; row < castle.GetLength(0); row++)
{
for (int col = 0; col < castle.GetLength(1); col++)
{
Console.Write("+" + (castle[row, col].HasNorthWall ? "--" : " "));
}
Console.WriteLine("+");
for (int col = 0; col < castle.GetLength(1); col++)
{
Console.Write((castle[row, col].HasWestWall ? "|" : " ") + " ");
if (col == (castle.GetLength(1) - 1) && castle[row, col].HasEastWall)
{
Console.Write("|");
}
}
Console.WriteLine();
if (row == (castle.GetLength(0)-1))
{
for (int col = 0; col < castle.GetLength(1); col++)
{
Console.Write("+" + (castle[row, col].HasSouthWall ? "--" : " "));
}
Console.WriteLine("+");
}
}
}
static CastleRoom[,] CastleFromData(string[] castleData)
{
int rows, columns;
if (!int.TryParse(castleData[0], out rows) || !int.TryParse(castleData[1], out columns))
{
return null;
}
if (rows <= 0 || columns <= 0)
{
return null;
}
if (castleData.Length != (rows + 2))
{
return null;
}
CastleRoom[,] castle = new CastleRoom[rows, columns];
for (int row = 2; row < castleData.Length; row++)
{
string[] rowData = castleData[row].Split();
if (rowData.Length != columns)
{
return null;
}
int roomValue;
for (int col = 0; col < rowData.Length; col++)
{
if (!int.TryParse(rowData[col], out roomValue))
{
return null;
}
if (roomValue < 0 || roomValue > 50)
{
return null;
}
castle[(row - 2), col] = new CastleRoom(roomValue);
}
}
return castle;
}
}
class CastleRoom
{
[Flags] public enum CastleWalls
{
West = 1,
North = 2,
East = 4,
South = 8
}
private CastleWalls _walls;
public bool Visited = false;
public CastleRoom(int walls)
{
_walls = (CastleWalls)walls;
}
public bool HasWestWall
{
get
{
return _walls.HasFlag(CastleWalls.West);
}
}
public bool HasNorthWall
{
get
{
return _walls.HasFlag(CastleWalls.North);
}
}
public bool HasEastWall
{
get
{
return _walls.HasFlag(CastleWalls.East);
}
}
public bool HasSouthWall
{
get
{
return _walls.HasFlag(CastleWalls.South);
}
}
public override string ToString()
{
return ((int)_walls).ToString();
}
}