【问题标题】:How to display a location depending on the value of an array如何根据数组的值显示位置
【发布时间】:2013-03-28 00:51:25
【问题描述】:

我一直在课堂上学习 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 可能是主页。 0,1 可能是村庄。 0,2 可能是沙漠。 1,5 可能是湖。

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

任何帮助表示赞赏。

【问题讨论】:

    标签: c# arrays


    【解决方案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 的解释使用字典

    【讨论】:

      【解决方案2】:

      我建议您使用结构和字符串的dictionary(或 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");
      

      【讨论】:

        猜你喜欢
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        相关资源
        最近更新 更多