체스판을 만들어보자! 35번 36번은 같은 문제이다.
먼저 chess_Borad의 Rows와 Column을 8칸 X 8칸으로 해준다.
각각 줄별로 loop하고 각 라인의 각의 홀짝으로 색깔을 번갈아가며 색칠하는 알고리즘을 사용하였다.
public MainWindow()
{
InitializeComponent();
chess_Board.Rows = 8;
chess_Board.Columns = 8;
for (int k = 1; k < 9; k++) //줄별로
{
if (k % 2 == 0)
{
for (int j = 0; j < 8; j++)
{
if (j % 2 == 0)
{
Rectangle r1 = new Rectangle();
r1.Fill = Brushes.Black;
chess_Board.Children.Add(r1);
}
else
{
Rectangle r1 = new Rectangle();
r1.Fill = Brushes.Red;
chess_Board.Children.Add(r1);
}
}
}
else
{
for (int j = 0; j < 8; j++)
{
if (j % 2 == 0)
{
Rectangle r1 = new Rectangle();
r1.Fill = Brushes.Red;
chess_Board.Children.Add(r1);
}
else
{
Rectangle r1 = new Rectangle();
r1.Fill = Brushes.Black;
chess_Board.Children.Add(r1);
}
}
}
}
}
'C#' 카테고리의 다른 글
038_ListArray (0) | 2022.05.11 |
---|---|
037_Calculator (0) | 2022.04.27 |
034_UserControl (0) | 2022.04.13 |
033_StackPanel (0) | 2022.04.12 |
032_Grid (0) | 2022.04.12 |