简体   繁体   中英

Check adjacent values in a 2d array

I'm making a treasure hunt game - I allow a user to input a coordinate - and if it contains 't' for treasure, then they win. But I want to add something so that it'll check what they enter and if t is within a 1 element radius of their guess - it'll say "You are hot". Or "You are cold" if 't' is > 1 element away.

I can't figure out the best way I could do this. I've tried things such as

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.");
}

But this doesn't seem to be working for me, I can't use Lists yet so I'd like to get around this without using them.

This is the part of the code that should figure it out.

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

I've also tried a nested for loop - but maybe I haven't used that correctly.

Not really a code question.

What you can do though is get the absolute value (ie ignore negative sign) of the difference between T and their guess for X and Y.

If either the absolute difference between T and the guess X or Y is 1 then you can say they are warm or whatever.

Partial example for you (prefer X/Y but have used row/col to match your work):

        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...");
        }

Thanks for the tip off Wiz.

Ben is on the Right Path but code needs to be more like.

        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...");
    }

This assumes that TRow and Tcolumn (the Tresure location) is set in the parent class.

The array can be seen as a subset of a Cartesian space. And how to position the elements in the array lends itself well to distance calculations in a Cartesian coordinate system. This means that the corresponding mathematical function can be implemented and used with confidence.

So that's what I would advice you to do for example:

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

Now I can easily check for any radius wether or not the player is in the vicinity of the treasure

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

I can also just as easily see if our player has tumbled on the treasure:

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

I can now get inputs from the user and use the methods above to output the right result. I am looping in order to test various cases.

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

This is what my output looks like:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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