繁体   English   中英

检查二维数组中的相邻值

[英]Check adjacent values in a 2d 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.");
}

但这似乎对我不起作用,我还不能使用列表,所以我想在不使用它们的情况下解决这个问题。

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

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循环-但也许我没有正确使用它。

并不是真正的代码问题。

但是,您可以做的是获取T与其对X和Y的猜测之间的差的绝对值(即忽略负号)。

如果T与猜测X或Y之间的绝对差为1,则可以说它们很温暖或类似。

为您提供的部分示例(更喜欢X / Y,但使用了row / col来匹配您的工作):

        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的小费。

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(“宝藏”位置)。

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

因此,我建议您这样做:

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

这是我的输出结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM