检查二维数组中的相邻值

时间:2017-11-10 20:28:44

标签: c# multidimensional-array

我正在制作寻宝游戏 - 我允许用户输入坐标 - 如果它包含宝藏的't',那么他们就赢了。但我想添加一些东西,以便它会检查它们输入的内容,如果t在它们猜测的1个元素半径范围内 - 它会说“你很热”。或者“你很冷”,如果't'是>距离1个元素。

我无法弄清楚我能做到的最佳方式。我尝试过像

这样的事情
if (Board[Row+1,Column] == 't' || Board[Row+1,Column+1] == 't' etc etc etc)
{
   Console.WriteLine("You are hot.");
}
else
{
   Console.Writeline("You are cold.");
}

但这对我来说似乎没有用,我还不能使用Lists所以我想在不使用它们的情况下解决这个问题。

这是应该弄清楚的代码的一部分。

string HotOrCold = "";
if (Board[Row, Column] != 'x')
{
    Board[Row, Column] = 'm';
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.Clear();
    if () // Here Is Where It Should Figure It Out

    Console.WriteLine("Uh-oh! You haven't found the treasure, try again!");
    Console.WriteLine("You are {0}.", HotOrCold);
    Console.ForegroundColor = ConsoleColor.Gray;
    wonGame = false;
    PrintBoard(Board);
    SaveGame(username, ref Board);
}

我也尝试过嵌套for循环 - 但也许我没有正确使用它。

3 个答案:

答案 0 :(得分:1)

不是真正的代码问题。

你可以做的是获得T与他们对X和Y的猜测之间差异的绝对值(即忽略负号)。

如果T和猜测X或Y之间的绝对差值为1,那么你可以说它们是温暖的或其他什么。

您的部分示例(更喜欢X / Y,但使用了行/列来匹配您的工作):

        var rowDiff = Math.Abs(guessRow - tRow);
        var columnDiff = Math.Abs(guessColumn - tColumn);

        if (rowDiff == 0 && columnDiff == 0)    
        {
            Console.WriteLine("You got it...");
        }
        else if (rowDiff <= 1 && columnDiff <= 1)
        {
            Console.WriteLine("You are hot...");
        }
        else
        {
            Console.WriteLine("You are cold...");
        }

感谢Wiz提示。

答案 1 :(得分:0)

Ben在正确的道路上,但代码需要更像。

        public static void FoundTreasure(int guessColumn, int guessRow )
    {

        if (Math.Abs(guessRow - TRow) == 0 && Math.Abs(guessColumn - TColumn) == 0)
        {
            Console.WriteLine("You got it...");
            return;
        }


        if (Math.Abs(guessRow - TRow) <= 1 && Math.Abs(guessColumn - TColumn) <= 1)
        {
            Console.WriteLine("You are hot...");
            return;
        }

        Console.WriteLine("You are cold...");
    }

这假设在父类中设置了TRow和Tcolumn(Tresure位置)。

答案 2 :(得分:0)

可以将数组视为笛卡尔空间的子集。如何在阵列中定位元素非常适合在笛卡尔坐标系中进行距离计算。这意味着可以放心地实现和使用相应的数学函数。

这就是我建议你做的事情,例如:

static double distanceToTreasure(int x, int y)
{
   double dX = x * 1.0D;
   double dY = y * 1.0D;
   double dWinX = winningX * 1.0D;
   double dWinY = winningY * 1.0D;
   double deltaX = Math.Abs(dWinX - dX);
   double deltaY = Math.Abs(dWinY - dY);
   return Math.Sqrt(Math.Pow(deltaX, 2.0D) + Math.Pow(deltaY, 2.0D));
}

现在我可以轻松检查玩家是否在宝藏附近的任何半径

static Boolean isInTheViccinityOfTreasure(double radius, int x, int y)
{
   return distanceToTreasure(x,y) <= radius;
}

我也可以很容易地看出我们的玩家是否已经砸到了宝藏:

static Boolean hasGotTheTreasure(int x, int y)
{
   return distanceToTreasure(x, y) == 0.0D;
}

我现在可以从用户那里获得输入并使用上面的方法输出正确的结果。我正在循环以测试各种情况。

public static void Main(string[] args)
{
   while (true)
   {
      // datas setting
      initDatas();
      // Random treasure position selection
      setupWinningPosition();

      // This is cheating: some kind of debugging so you can test the results
      Console.WriteLine("Don' t tell tx=" + winningX + " and tY = " + winningY);

      Console.WriteLine("Enter x");
      int x = Int32.Parse(Console.ReadLine());
      Console.WriteLine("Enter y");
      int y = Int32.Parse(Console.ReadLine());

      if (hasGotTheTreasure(x, y))
      {
         Console.WriteLine("You got the treasure dude!!!");
      }

      // A radius of 1 is used here
      else if (isInTheViccinityOfTreasure(1, x, y))
      {
         Console.WriteLine("That's getting hot: keep going boy...");
      }
      else
      {
         Console.WriteLine("Oops it's getting very cold");
      }

      // This is the way out of the loop by hitting 'q'
      ConsoleKeyInfo key = Console.ReadKey();
      if (key.KeyChar == 'q')
      {
         break;
      }   
   }
}

这就是我的输出:

enter image description here

相关问题