私は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;
}
}
}
私は別の方法を使用してみましたが、それは常にましたどうにかしてコードに入れて壊した。良い解決策はありますか?
ありがとうございます!
通常、ボード上の各四角形にカスタムクラスを作成するのが好きです。四角形は、四角形の部分を含む多くの情報を含むことができます。コードをもっと簡単にするでしょう。 – jdweng