0

移動関数を呼び出すと、範囲外のインデックスが発生する理由がわかりません。「マップ」の右下隅にプレーヤーを設定しているので、北または西に移動できるはずですが、何らかの理由でインデックスが範囲外の例外を取得し続けます。

これがメインプログラムです:

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;
    }
4

1 に答える 1

0

メイン プログラムの 2 つの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)。

内側のループをなくすと、一度に 1 つのステップを実行できるようになります。

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

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

Tileアクセサーで何かおかしなことが起こっていない限り(NorthWallまたは などXPosition)、MoveメソッドにIndexOutOfRangeException.

ただし、mapインデックス作成は範囲外になる可能性があります。たとえば、次の行です。

map[player.YPosition, player.XPosition]

壁が正しく構成されていない場合、上記のように、while配列のインデックスがマップの境界を超えて増加し、例外が生成されるまで、内側のループによってプレイヤーは永遠に同じ方向に移動し続けます。

于 2015-08-16T04:54:51.773 に答える