2017-04-16 9 views
0

こんにちは私はちょうどグリッドのコードを完成させました。指示により、右、左、上または下に移動し、移動する特定の値を入力することができます。しかし、現在問題となっているのは、真となる唯一の場所はどの値が入力されたかです。私は全体の量が真実になるように努めています。たとえば、私たちは右に3を移動する、それは3つのスポット=本当に最後の代わりに真でなければなりません。誰もそれで私を助けることができますか?ブール2D配列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

あなただけのDOから破壊されているので、物事のカップルは、1) '場合は(大丈夫== false)を'常に真であるため、必要ありませんし、一方あなたのwhileループで、 'drill [check_column、check_row] == true && .. && drill [check_column、check_row]!= true'をチェックして競合しています3)あなたは決して減少していません ' new_distance'あなたのwhileループで4)あなたは3つのスポット=真を意味するものを明確にすることができますか?私はあなたの初期設定を除いてどこに何かを真に設定しているのを見ていません –

+0

たとえば、 "r"と "2"を入力するとグリッド上の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の値は2次元配列を設定するだけですが、印刷する値は-1 + +と-5 +の番号になりますので、何を変更する必要がありますか? – SaltyLegend

関連する問題