2017-11-10 96 views
0

我正在制作寻宝游戏 - 我允许用户输入坐标 - 如果它包含“宝贝”的“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循环 - 但也许我没有正确使用。

+0

我真的建议改变你的if语句使用'开关{}也case'关于如何使用调试器读取了并在您的代码中设置断点 – MethodMan

+0

我会读取switch语句,我知道如何设置断点 - 我必须更多地了解调试器。 –

+0

使用switch语句.. – MethodMan

回答

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

谢谢你的提示关闭奇才。

+0

需要重构才能使用< or >。用当前的代码,你会得到一个误报,如果它是一行,但8000列。如果(Math.Abs​​(guessRow - tRow)<= 1 && Math.Abs​​(guessColumn - tColumn)<= 1) – WizardHammer

+0

hehe如此疲倦有点冲到那一个...更好修复 –

+0

好吧,我用你的修改过的代码,它的工作完全谢谢。 –

0

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

 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位置)。

+0

对不起,如果我错了 - 但在if结构中,你以'你很热'结束,你已经把guessColumn <= tColumn)<= 1 ----你打算把guessColumn <= tColumn? –

+0

正确诊断,错误治疗 guessColumn <= tColumn)<= 1 是错误的应该是guessColumn - tColumn)<= 1在示例中已更正 – WizardHammer

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

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

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