[英]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. 我正在做一个寻宝游戏-我允许用户输入一个坐标-如果它包含“ t”表示寻宝,那么他们会赢。 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".
但是我想添加一些内容,以便它可以检查他们输入的内容以及t是否在其猜测的1个元素半径之内-它会说“您很热”。 Or "You are cold" if 't' is > 1 element away.
如果“ t”距离> 1个元素,则为“您很冷”。
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. 我也尝试过嵌套的for循环-但也许我没有正确使用它。
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. 但是,您可以做的是获取T与其对X和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. 如果T与猜测X或Y之间的绝对差为1,则可以说它们很温暖或类似。
Partial example for you (prefer X/Y but have used row/col to match your work): 为您提供的部分示例(更喜欢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...");
}
Thanks for the tip off Wiz. 感谢您给Wiz的小费。
Ben is on the Right Path but code needs to be more like. 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...");
}
This assumes that TRow and Tcolumn (the Tresure location) is set in the parent class. 假定在父类中设置了TRow和Tcolumn(“宝藏”位置)。
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: 这是我的输出结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.