C# 基于瓷砖的棋盘游戏骰子数量限制(增量算法)

C# 基于瓷砖的棋盘游戏骰子数量限制(增量算法),c#,C#,开发一个基于2D瓷砖的“棋盘游戏”,当一个玩家掷骰子时,我正在与我必须做出的限制作斗争(如果你落下一个5等,则移动5个瓷砖) 我尝试使用以下逻辑: 从起点开始 检查两侧、上方和下方的位置 检查相邻瓷砖是否可行走,如果可以,请将其更改为可到达 去邻居家,重复 我一直在寻找A*和D*路径,但这对我来说是一个新课题,他们似乎更关注从A点到B点,而不是我需要的“到达” 如何通过代码实现这一点 我创建了一个2D数组,该数组保存着我的瓷砖(我需要一个普通的瓷砖贴图数组用于其他用途): for(int

开发一个基于2D瓷砖的“棋盘游戏”,当一个玩家掷骰子时,我正在与我必须做出的限制作斗争(如果你落下一个5等,则移动5个瓷砖)

我尝试使用以下逻辑:

  • 从起点开始
  • 检查两侧、上方和下方的位置
  • 检查相邻瓷砖是否可行走,如果可以,请将其更改为可到达
  • 去邻居家,重复
我一直在寻找A*和D*路径,但这对我来说是一个新课题,他们似乎更关注从A点到B点,而不是我需要的“到达”

如何通过代码实现这一点

我创建了一个2D数组,该数组保存着我的瓷砖(我需要一个普通的瓷砖贴图数组用于其他用途):

for(int i=0;i<27;i++)
{
对于(int j=0;j<33;j++)
{
tileMap[i,j]=goTile[i*33+j];
}
}
我现在使用tileMap作为我的位置因子,例如,我的球员当前位置是tileMap[2,4]

然后我尝试开发一个函数:

void pathFinding(Vector2 playerPosition, int diceNumber)
    {
        GameObject currentPos = tileMap[(int)playerPosition.x, (int)playerPosition.y];

        for (int i = 0; i < diceNumber; i++) {
                if (tileMap[(int)playerPosition.x + 1, (int)playerPosition.y].tag == "walkableGrid")
                {
                    tileMap[(int)playerPosition.x + 1, (int)playerPosition.y].gameObject.tag = "reachable";
                    playerPosition.x++;
                }

                if (tileMap[(int)playerPosition.x  - 1, (int)playerPosition.y].tag == "walkableGrid")
                {
                    playerPosition.x--;
                }

                if (tileMap[(int)playerPosition.x, (int)playerPosition.y + 1].tag == "walkableGrid")
                {
                    playerPosition.y++;
                }

                if (tileMap[(int)playerPosition.x, (int)playerPosition.y - 1].tag == "walkableGrid")
                {
                    playerPosition.y--;
                }
            }
    }
void寻路(矢量2播放器位置,整数)
{
GameObject currentPos=tileMap[(int)playerPosition.x,(int)playerPosition.y];
for(int i=0;i

但是完成这项工作(如果可以的话)需要很多行代码,我相信有一种更快的方法使用嵌套的for循环,也许?

。。。你希望我们在这里回答什么?编辑后的帖子向我们展示了一些数据结构的代码,否则很难猜到正确的解决方案。还有,你试过自己编写代码吗?补充了我如何设置tilemap以及我自己对算法的尝试。看起来这与Unity无关。您可以使用任何编程语言中的命令行界面来解决此问题。
//I have now edited the code to better reflect your real data

public void ShowMoves(Vector2 playerPosition, int diceNumber, bool[] blocks)
{

    int x = (int)playerPosition.x;
    int y = (int)playerPosition.y;

    if(tileMap.GetUpperBound(0) < x + 1)
    {
        if(tileMap[x + 1, y].tag == "walkableGrid" && blocks[0])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x + 1, y), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), false, y != tileMap.GetUpperBound(1), y != 0 });
        }
    }

    if(x - 1 >= 0)
    {
        if(tileMap[x - 1, y].tag == "walkableGrid" && blocks[1])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x - 1, y), diceNumber - 1, new bool[] { false, x != 0, y != tileMap.GetUpperBound(1), y != 0 });
        }
    }

    if(tileMap.GetUpperBound(1) < y + 1)
    {
        if(tileMap[x, y + 1].tag == "walkableGrid" && blocks[2])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x, y + 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, y != tileMap.GetUpperBound(1), false });
        }
    }

    if(y - 1 >= 0)
    {
        if(tileMap[x, y - 1].tag == "walkableGrid" && blocks[3])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x, y - 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, false, y != 0 });
        }
    }
}
//I have now edited the code to better reflect your real data

public void ShowMoves(Vector2 playerPosition, int diceNumber, bool[] blocks)
{

    int x = (int)playerPosition.x;
    int y = (int)playerPosition.y;

    if(tileMap.GetUpperBound(0) < x + 1)
    {
        if(tileMap[x + 1, y].tag == "walkableGrid" && blocks[0])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x + 1, y), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), false, y != tileMap.GetUpperBound(1), y != 0 });
        }
    }

    if(x - 1 >= 0)
    {
        if(tileMap[x - 1, y].tag == "walkableGrid" && blocks[1])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x - 1, y), diceNumber - 1, new bool[] { false, x != 0, y != tileMap.GetUpperBound(1), y != 0 });
        }
    }

    if(tileMap.GetUpperBound(1) < y + 1)
    {
        if(tileMap[x, y + 1].tag == "walkableGrid" && blocks[2])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x, y + 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, y != tileMap.GetUpperBound(1), false });
        }
    }

    if(y - 1 >= 0)
    {
        if(tileMap[x, y - 1].tag == "walkableGrid" && blocks[3])
        {
            /*Light up the tile*/
            if(diceNumber > 0)
                ShowMoves(new Vector2(x, y - 1), diceNumber - 1, new bool[] { x != tileMap.GetUpperBound(0), x != 0, false, y != 0 });
        }
    }
}
public void ShowMoves(Vector2 playerPosition, int diceNumber, bool[] blocks = new bool[] {true, true, true, true})