如何根据数组的值显示位置

时间:2013-03-28 00:51:26

标签: c# arrays

我一直在课堂上学习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也是相等的。 有人告诉我,切换声明最好是让这个工作,但我已经尝试过,我无法使其工作。

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分: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)

或使用Dictionnary作为解释Gam Erix

答案 1 :(得分:0)

我建议您使用dictionary结构和字符串(或2个字符串的结构 - 分别为description和location),您可以在其中为每个位置分配一些信息:

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");
相关问题