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

时间:2015-08-16 01:43:09

标签: c# indexoutofrangeexception

当我调用任何移动函数时,我不明白为什么我的索引超出了范围异常。 我将播放器设置在" 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;
    }

1 个答案:

答案 0 :(得分:0)

主程序中的两个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循环将使玩家永远沿同一方向移动,直到数组索引增加超出地图边界,产生异常