2017-04-16 2 views
0

Hallo, ich habe gerade einen Code eines Rasters fertiggestellt. Mit den Anweisungen können Sie nach rechts, links, oben oder unten gehen und einen bestimmten Wert eingeben, um sich fortzubewegen. Das Problem ist jetzt jedoch, welcher Wert eingegeben wird, ist der einzige Ort, der wahr wird. Ich versuche, die gesamte Menge wahr werden zu lassen. Zum Beispiel, wenn wir uns nach rechts bewegen, sollte es 3 Punkte = wahr statt nur der letzte sein. Kann mir jemand dabei helfen?Boolesche 2D-Arrays C# Mehrere Werte wahr machen

  // 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

Ein paar Dinge 1) '(okay == false)' wird immer wahr sein und ist daher nicht notwendig, weil Sie nur von Ihrem do brechen, während Schleife nach der Einstellung okay = false 2) in Ihrer while-Schleife haben Sie konkurrierenden check 'drill [check_column, check_row] == true && .. && drill [check_column, check_row]! = true' 3) Sie dekrementieren nie ' new_distance' in deiner while-Schleife 4) Könntest du klarstellen, was du meinst _3 spots = true_. Ich sehe nicht, wo Sie alles auf True setzen, außer in Ihrer Initialisierung –

+0

Okay, zum Beispiel wenn ich "r" dann "2" eingeben soll, dann sollte es diese 2 Punkte auf dem Raster wahr machen, aber im Moment ist es nur Setzen Sie den EXAKTEN Wert 2 richtig auf Original um wahr – SaltyLegend

Antwort

0

Sie Frage zu beantworten, den Grund Sie nicht jede Bewegung als sicher ist, weil Sie jede Position Überprüfung nicht sehen, das Setzen Sie wurden einfach Ihre Prüfpositionserkennungsteil Sie auf die Position jeder inkrementelle Position bewegen und nicht wollte . Ich habe in dieser Antwort eine Klasse mit etwas mehr Logik hinzugefügt, da jede Bewegung in Ihrem Spiel eine etwas andere Logik erfordert, um zu funktionieren.

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

Beispielausgabe

enter image description here

+0

Also wird dies jeden Schritt prüfen? – SaltyLegend

+0

@SaltyLegend. Ja, das wird jeden Schritt prüfen. Ich habe meine Antwort bearbeitet, weil ich vergessen habe, den OK-Wert auf das Ergebnis von try move zu setzen. Außerdem habe ich eine Beispielausgabe hinzugefügt. –

+0

Ah danke, aber ich muss auch die Werte von -1, -5 verwenden. Die 200, 400 Werte dienen nur dazu, das 2d Array zu setzen, aber für die zu druckenden Werte sind das die Zahlen -1 - + und -5 - +, was muss ich ändern? – SaltyLegend

Verwandte Themen