为什么会出现索引超出范围的异常

本文关键字:范围 异常 索引 为什么 | 更新日期: 2023-09-27 18:08:11

我不明白为什么当我调用任何移动函数时,我会得到索引超出范围异常。我将玩家设置在"地图"的右下角,所以它应该能够向北或向西移动,但由于某种原因,我总是得到索引超出范围的异常。

这是主程序:

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循环将使玩家永远朝着同一个方向移动,直到数组索引超出地图的界限,产生异常。