2017-04-16 89 views
0

你好,我剛剛完成了一個網格的代碼。這些說明可讓您右移,左移,上移或下移並輸入特定值以移動。但是現在的問題是,輸入哪個值是唯一成爲真實的位置。我試圖讓全部變成真實。例如fi我們右移3,它應該是3點= true而不是最後一個。任何人都可以幫助我嗎?布爾二維數組C#使多個值爲真

  // Make a bool 2d array and save already drilled values into the array 
     bool[,] drill = new bool[401, 200]; 
     drill[200, 1] = true; 
     drill[200, 2] = true; 
     drill[200, 3] = true; 
     drill[201, 3] = true; 
     drill[202, 3] = true; 
     drill[203, 3] = true; 
     drill[203, 4] = true; 
     drill[203, 5] = true; 
     drill[204, 5] = true; 
     drill[205, 5] = true; 
     drill[205, 4] = true; 
     drill[205, 3] = true; 
     drill[206, 3] = true; 
     drill[207, 3] = true; 
     drill[207, 4] = true; 
     drill[207, 5] = true; 
     drill[207, 6] = true; 
     drill[207, 7] = true; 
     drill[206, 7] = true; 
     drill[205, 7] = true; 
     drill[204, 7] = true; 
     drill[203, 7] = true; 
     drill[202, 7] = true; 
     drill[201, 7] = true; 
     drill[200, 7] = true; 
     drill[199, 7] = true; 
     drill[199, 6] = true; 
     drill[199, 5] = true; 


     // Set some values 
     bool okay = true; 
     int column = -1; 
     int row = -5; 
     int check_column = 199; 
     int check_row = 5; 

     // Run a while loop 
     do 
     { 
      // Ask the user to input a direction 
      Console.WriteLine("Which direction would you like to go?"); 
      string direction = Console.ReadLine(); 

      // Ask the user to input a movement 
      Console.WriteLine("What value would you like to move by?"); 
      string distance = Console.ReadLine(); 
      int new_distance = Convert.ToInt32(distance); 

      // Use if statements 
      if (direction == "l" || direction == "L") 
      { 
       column = column - new_distance; 
       check_column = check_column - new_distance; 

      } 
      else if (direction == "u" || direction == "U") 
      { 
       row = row + new_distance; 
       check_row = check_row - new_distance; 

      } 
      else if (direction == "r" || direction == "R") 
      { 
       column = column + new_distance; 
       check_column = check_column + new_distance; 
      } 
      else if (direction == "d" || direction == "D") 
      { 
       row = row - new_distance; 
       check_row = check_row + new_distance; 
      } 
      else if (direction == "q" || direction == "Q" || new_distance == 0) 
      { 
       Console.WriteLine("Program will now end."); 
       okay = false; 
       break; 
      } 

      while (new_distance > 0) 
      { 


       if (drill[check_column, check_row] == true && check_row >= 0 && check_row <=200 && check_column >=0 && check_column <=400 && drill[check_column, check_row] != true) 
       { 
        Console.WriteLine("{0},{1} safe", column, row); 
        break; 
       } 

       else 
       { 
        Console.WriteLine("{0},{1} danger", column, row); 
        Console.WriteLine("Program will now end."); 
        okay = false; 
        break; 

       } 


      } 

     } while (okay); 

     if (okay == false) 
     { 
      Console.WriteLine("Thanks for using the program!"); 
     } 
Console.ReadLine(); 
+0

幾件事情1)'如果(好吧==假)'將永遠是真實的,因此沒有必要,因爲你只能從做突破,而循環後設置好=假2)在你的while循環中,你有競爭檢查'鑽[check_column,check_row] == true && .. &&鑽[check_column,check_row]!= true' 3)你永遠不會遞減 'new_distance'在你的while循環中4)你能否澄清你的意思_3 spots = true_。我沒有看到你在哪裏設置任何東西,除了在你的初始化 –

+0

好吧,例如,如果我要輸入「r」,然後「2」,那麼它應該使網格上的這兩個點是真實的,但現在,它的唯一將EXACT值2設置爲原來的值爲true – SaltyLegend

回答

0

要回答你的問題,你是不是看到每個移動爲安全的原因是因爲你不檢查每一個位置,你只需設置你的檢查位置,你想移動的位置,而不是每個增量位置。我在這個答案中增加了一個更多邏輯的類,因爲你遊戲中的每一步都需要稍微不同的邏輯來工作。

public class DrillGame 
{ 
    private const int Columns = 401; 
    private const int Rows = 200; 

    public void Play() 
    { 
     // Make a bool 2d array and save already drilled values into the array 
     bool[,] drill = ProvideDrill(); 

     // Set some values 
     bool okay = true; 

     _currentGameRow = -1; 
     _currentGameColumn = -5; 
     _currentArrayRow = 5; 
     _currentArrayColumn = 199; 

     // Run a while loop 
     do 
     { 
      // Ask the user to input a direction 
      Console.WriteLine("Which direction would you like to go?"); 
      string direction = Console.ReadLine(); 

      // Ask the user to input a movement 
      Console.WriteLine("What value would you like to move by?"); 
      string distanceInput = Console.ReadLine(); 
      int distanceToMove = Convert.ToInt32(distanceInput); 

      // Use if statements 
      if (direction == "l" || direction == "L") 
      { 
       okay = TryMoveLeft(distanceToMove); 
      } 
      else if (direction == "u" || direction == "U") 
      { 
       okay = TryMoveUp(distanceToMove); 
      } 
      else if (direction == "r" || direction == "R") 
      { 
       okay = TryMoveRight(distanceToMove); 
      } 
      else if (direction == "d" || direction == "D") 
      { 
       okay = TryMoveDown(distanceToMove); 
      } 
      else if (direction == "q" || direction == "Q" || distanceToMove == 0) 
      { 
       Console.WriteLine("Program will now end."); 
       okay = false; 
       break; 
      } 

     } while (okay); 

     if (okay == false) 
     { 
      Console.WriteLine("Thanks for using the program!"); 
     } 
     Console.ReadLine(); 
    } 

    private bool TryMoveLeft(int distanceToMove) 
    { 
     while(distanceToMove > 0) 
     { 
      _currentArrayColumn = _currentArrayColumn - 1; 
      _currentGameColumn = _currentGameColumn - 1; 

      if (!TryMoveColumn(distanceToMove)) 
      { 
       return false; 
      } 

      distanceToMove--; 
     } 
     return true; 
    } 

    private bool TryMoveRight(int distanceToMove) 
    { 
     while (distanceToMove > 0) 
     { 
      _currentArrayColumn = _currentArrayColumn + 1; 
      _currentGameColumn = _currentGameColumn + 1; 

      if (!TryMoveColumn(distanceToMove)) 
      { 
       return false; 
      } 

      distanceToMove--; 
     } 
     return true; 
    } 

    private bool TryMoveUp(int distanceToMove) 
    { 
     while (distanceToMove > 0) 
     { 
      _currentArrayRow = _currentArrayRow - 1; 
      _currentGameRow = _currentGameRow - 1; 

      if (!TryMoveRow(distanceToMove)) 
      { 
       return false; 
      } 

      distanceToMove--; 
     } 
     return true; 
    } 

    private bool TryMoveDown(int distanceToMove) 
    { 
     while (distanceToMove > 0) 
     { 
      _currentArrayRow = _currentArrayRow + 1; 
      _currentGameRow = _currentGameRow + 1; 

      if (!TryMoveRow(distanceToMove)) 
      { 
       return false; 
      } 

      distanceToMove--; 
     } 
     return true; 
    } 

    private bool TryMoveColumn(int distanceToMove) 
    { 
     var drill = ProvideDrill(); 

     if (_currentArrayColumn >= 0 && _currentArrayColumn < Columns && drill[_currentArrayColumn, _currentArrayRow]) 
     { 
      Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe"); 
      return true; 
     } 
     else 
     { 
      Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger"); 
      Console.WriteLine("Program will now end."); 
      return false; 
     } 
    } 

    private bool TryMoveRow(int distanceToMove) 
    { 
     var drill = ProvideDrill(); 

     if (_currentArrayRow >= 0 && _currentArrayRow < Rows && drill[_currentArrayColumn, _currentArrayRow]) 
     { 
      Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe"); 
      return true; 
     } 
     else 
     { 
      Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger"); 
      Console.WriteLine("Program will now end."); 
      return false; 
     } 
    } 

    private bool[,] ProvideDrill() 
    { 
     bool[,] drill = new bool[Columns, Rows]; 
     drill[200, 1] = true; 
     drill[200, 2] = true; 
     drill[200, 3] = true; 
     drill[201, 3] = true; 
     drill[202, 3] = true; 
     drill[203, 3] = true; 
     drill[203, 4] = true; 
     drill[203, 5] = true; 
     drill[204, 5] = true; 
     drill[205, 5] = true; 
     drill[205, 4] = true; 
     drill[205, 3] = true; 
     drill[206, 3] = true; 
     drill[207, 3] = true; 
     drill[207, 4] = true; 
     drill[207, 5] = true; 
     drill[207, 6] = true; 
     drill[207, 7] = true; 
     drill[206, 7] = true; 
     drill[205, 7] = true; 
     drill[204, 7] = true; 
     drill[203, 7] = true; 
     drill[202, 7] = true; 
     drill[201, 7] = true; 
     drill[200, 7] = true; 
     drill[199, 7] = true; 
     drill[199, 6] = true; 
     drill[199, 5] = true; 
     return drill; 
    } 

    private int _currentArrayRow; 
    private int _currentArrayColumn; 
    private int _currentGameRow; 
    private int _currentGameColumn; 
} 

樣本輸出

enter image description here

+0

因此,這將檢查每一步? – SaltyLegend

+0

@SaltyLegend。是的,這將檢查每一步。我編輯了我的答案,因爲我忘了將嘗試移動的結果設置爲好值。另外,我添加了一些示例輸出。 –

+0

啊謝謝,但我也必須實際使用-1,-5的值。 200,400值只是設置2d數組,但是對於要打印的值,它們將是-1 - +數字和-5 - +數字,所以我需要更改哪些值? – SaltyLegend

相關問題