我正在开发一个随机地牢生成器,只是为了好玩/作为学习一些新事物的辅助项目。我编写了一个函数,它为任何给定的单元格返回一个整数哈希值,它为您提供有关它应该是什么类型的游戏对象的信息。即如果它是一堵墙,面对什么方向,它是不是一个角落,等等。这是功能当前的样子。
private int CellHashValue(int xIndex, int yIndex, char centerTile)
{
int hashValue = 0;
if (dungeon2DArray[xIndex - 1, yIndex + 1] == centerTile)
{
hashValue += 1;
}
if (dungeon2DArray[xIndex, yIndex + 1] == centerTile)
{
hashValue += 2;
}
if (dungeon2DArray[xIndex + 1, yIndex + 1] == centerTile)
{
hashValue += 4;
}
if (dungeon2DArray[xIndex - 1, yIndex] == centerTile)
{
hashValue += 8;
}
if (dungeon2DArray[xIndex + 1, yIndex] == centerTile)
{
hashValue += 16;
}
if (dungeon2DArray[xIndex - 1, yIndex - 1] == centerTile)
{
hashValue += 32;
}
if (dungeon2DArray[xIndex, yIndex - 1] == centerTile)
{
hashValue += 64;
}
if (dungeon2DArray[xIndex + 1, yIndex - 1] == centerTile)
{
hashValue += 128;
}
return hashValue;
}
我的问题是,是否有一种更有效、更快的方法来进行这些我可能没有想到的检查?地牢数组的大小范围从 100x100 到 1000x1000,尽管该函数不会在每个单元格上调用。我有一个单独的列表,其中包含房间,并且我迭代以实例化对象的每个方向都有开始和结束索引。