2017-11-10 94 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