[英]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.