为什么我得到索引超出范围异常

Why am I getting an index out of range exception

我不明白为什么在调用任何移动函数时会出现索引超出范围异常。 我将播放器设置在 "map" 的右下角,因此它应该能够向北或向西移动,但由于某种原因我一直收到索引超出范围的异常。

这是主程序:

namespace DungeonWalk
{
class Program
{
    static void Main(string[] args)
    {
        int length, width;
        Console.WriteLine("What size dungeon do you want to traverse?");
        try
        {
            Console.Write("Length: ");
            length = Int32.Parse(Console.ReadLine());
            Console.Write("Width: ");
            width = Int32.Parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("Invalid parameters.");
            return;
        }
        var map = Tile.CreateMap(length, width);
        var player = new Player(map.GetLength(0) - 1, map.GetLength(1) - 1);
        while(true)
        {
            Console.WriteLine("What do you want to move next?");
            var move = Console.ReadLine().ToLower();
            while (true)
            {
                switch (move)
                {
                    case "north":
                        Move.North(player, map[player.YPosition, player.XPosition]);
                        break;
                    case "south":
                        Move.South(player, map[player.YPosition, player.XPosition]);
                        break;
                    case "east":
                        Move.East(player, map[player.YPosition, player.XPosition]);
                        break;
                    case "west":
                        Move.West(player, map[player.YPosition, player.XPosition]);
                        break;
                    default:
                        Console.WriteLine("Not a vaild direction. Use cardinal directions.");
                        break;
                }
            }
        }
    }
}
}

这是要移动的代码:

namespace DungeonWalk
{
class Move
{
    public static void North(Player player, Tile tile)
    {
        if(tile.NorthWall)
        {
            Console.WriteLine("There is a wall before you. You cannot proceed any further.");
        }
        else
        {
            player.YPosition -= 1;
        }
    }

    public static void South(Player player, Tile tile)
    {
        if (tile.SouthWall)
        {
            Console.WriteLine("There is a wall before you. You cannot proceed any further.");
        }
        else
        {
            player.YPosition += 1;
        }
    }

    public static void West(Player player, Tile tile)
    {
        if (tile.WestWall)
        {
            Console.WriteLine("There is a wall before you. You cannot proceed any further.");
        }
        else
        {
            player.XPosition -= 1;
        }
    }

    public static void East(Player player, Tile tile)
    {
        if (tile.EastWall)
        {
            Console.WriteLine("There is a wall before you. You cannot proceed any further.");
        }
        else
        {
            player.XPosition += 1;
        }
    }
}
}

最后是 CreateMap 的代码

public static Tile[,] CreateMap(int length, int width)
    {
        var map = new Tile[length, width];
        for (int i = 0; i < length; i++)
        {
            for (int j = 0; j < width; j++)
            {
                map[i, j] = new Tile(j, i, length, width);
            }
        }
        return map;
    }

主程序中的两个while循环看起来有点奇怪,不知道是不是故意的

当前实现:

while(true)
{
    Console.WriteLine("What do you want to move next?");
    var move = Console.ReadLine().ToLower();
    while (true)
    {
        switch (move)
        {
            // Move player...
        }
    }
}

只要求用户移动一次,然后内部 while 循环会一直让玩家向指定方向移动。 (注意 break 只会跳出 switch 语句,不会跳出内部 while)。

如果消除内部循环,您将获得一次执行一个步骤的能力:

while(true)
{
    Console.WriteLine("What do you want to move next?");
    var move = Console.ReadLine().ToLower();

    switch (move)
    {
        // Move player...
    }
}

除非 Tile 访问器(例如 NorthWallXPosition 中发生了一些奇怪的事情,否则我在 Move 方法中看不到任何东西可能会导致 IndexOutOfRangeException.

map 索引可能越界了,例如这一行:

map[player.YPosition, player.XPosition]

如果墙壁配置不正确,那么,如上所述,内部 while 循环将使玩家永远朝同一方向移动,直到数组索引增加到地图的边界之外,生成一个例外。