0

我一直在课堂上学习 C#,并尝试在空闲时间自学。

我正在尝试用 C# 制作我的第一款游戏。到目前为止,我的代码是:

 static void Main(string[] args)
    {

        string aValue;
        int Limit = 20;

        int[,] MOVEMENT = new int[20,20];           

        // MOVEMENT

        for (int i = 0; i < 20;)
            for (int j = 0; j < 20;)
            {
                Console.WriteLine("Which direction do you wish to move in?");
                aValue = Console.ReadLine();

                // MOVE NORTH
                if (aValue == "North" || aValue == "north")
                {
                    i = i + 1;

                    if (i >= 20)
                    {
                        Console.WriteLine("You cannot move any further in this direction!");
                        i = 20;
                    }
                }

                // MOVE SOUTH
                if (aValue == "South" || aValue == "south")
                {
                    i = i - 1;

                    if (i <= 0)
                    {
                        Console.WriteLine("You cannot move any further in this direction!");
                        i = 0;
                    }
                }

                // MOVE EAST
                if (aValue == "East" || aValue == "east")
                {
                    j = j + 1;

                    if (j >= 20)
                    {
                        Console.WriteLine("You cannot move any further in this direction!");
                        j = 20;
                    }
                }

                // MOVE WEST
                if (aValue == "West" || aValue == "west")
                {
                    j = j - 1;

                    if (j <= 0)
                    {
                        Console.WriteLine("You cannot move any further in this direction!");
                        j = 0;
                    }
                }


                // Display where the character is after each movement and list possible events 
                // in the area. 

                Console.WriteLine("You are now at {0},{1}", i, j);


            }
        Console.ReadLine();
}

现在想让数组的每个元素对应游戏中的不同位置。例如:0,0 可能是 Home。0,1 可能是村庄。0,2 可能是沙漠。1,5 可能是湖。

我希望每个位置都有自己的描述和一些事件(使用 If 语句等)。我不确定如何让代码显示位置(这只是元素的值吗?),这取决于 i 和 j 也相等。我被告知最好使用 switch 语句来完成这项工作,但我已经尝试过了,但我无法让它工作。

任何帮助表示赞赏。

4

2 回答 2

1

为什么不使用对象?

public class Location{
    public int Latitude   {get;set;}
    public int Longitude  {get;set;}
    public string Name{get;set;}
    public string Description  {get;set;}
}

并使用 LINQ 检索名称和值

yourIEnumerableLocation.FirstOrDefault(x=> x.Latitude== yourXposition && x.Longitude == yourYposition)

或按照 Gam Erix 的解释使用字典

于 2013-03-28T01:02:11.090 回答
0

我建议您使用结构和字符串的字典(或 2 个字符串的结构 - 分别是描述和位置),您可以在其中为每个位置分配一些信息,如下所示:

using System;
public struct Point
{
   public int x, y;

   public Point(int p1, int p2)
   {
      x = p1;
      y = p2;
   }
}
public struct Information
{
   public string description, location;

   public Information(string desc, string loc)
   {
      description = desc;
      location = loc;
   }
}
var info = new Dictionary<Point, Information>();

info[Point(i,j)] = Information("This is our lovely lake","Lake");
于 2013-03-28T01:01:58.833 回答