2016-04-13 9 views
1

私は2D配列を使ってチェスのようなデザインをコンソールにしようとしています。 私は "|"プレイフィールドを設計するには " - "を使用します。 しかし、私は、フィールドの色の切り替えに問題持っている(白|黒|白)ここ は(番号フィールド上の色の変化なし)私のコードです2D配列のチェスボード

public class Program 
{ 
    static void Main(string[] args) 
    { 
     int[] array = new int[10]; 
     int[,] array2 = new int[6, 9]; 

     for(int i = 0;i < array2.GetLength(0); i++) 
     { 
      if (i == array2.GetLength(0)-1 || i == 0) 
      { 
       for (int h = 0; h < array2.GetLength(1); h++) 
        decidingColors(false); 
        Console.Write("|" + "-"); 
      } 
      else 
      for (int x = 0;x < array2.GetLength(1); x++) 
      { 
        decidingColors(false); 
       Console.Write("|"); 
        decidingColors(true); 
       Console.Write(array2[i, x]); 

      } 
      decidingColors(false); 
      Console.Write("|"); 
      Console.WriteLine(); 
     } 
     Console.ReadLine(); 
    } 
    public static void decidingColors(bool wentThrough) 
    { 
     if(wentThrough == true) 
     { 
      Console.BackgroundColor = ConsoleColor.White; 
      Console.ForegroundColor = ConsoleColor.Black; 
     } 
     else 
     { 
      Console.BackgroundColor = ConsoleColor.Black; 
      Console.ForegroundColor = ConsoleColor.White; 
     } 

    } 
} 

私は別の方法を使用してみましたが、それは常にましたどうにかしてコードに入れて壊した。良い解決策はありますか?

ありがとうございます!

+1

通常、ボード上の各四角形にカスタムクラスを作成するのが好きです。四角形は、四角形の部分を含む多くの情報を含むことができます。コードをもっと簡単にするでしょう。 – jdweng

答えて

1

x%2 == 0を使用して、必要に応じて奇数または偶数要素を決定することができます。

for (int x = 0;x < array2.GetLength(1); x++) 
{ 
    decidingColors(false); 
    Console.Write("|"); 
    decidingColors(x % 2 == 0); 
    Console.Write(array2[i, x]); 
} 
2

あなたのループは、印刷していると思われるものを正確に印刷していない可能性があります。字下げが実際のロジックと一致するように、インデックスにhのループに中括弧を追加する必要があります。

+0

私は何かを変更していたときにそれがスリップしました、すでに修正されました。ありがとうございました – Lopah