0

我只是想知道是否有更简单的方法可以做到这一点:

for (int i = 0; i < 1; i++)
{
    for (int j = 0; i < 8; j+2)
    {
        board[ i, j ] = 2;
        board[( i + 1 ), j ] = 2;
        board[( i + 2 ), j ] = 2;
    }
}  

我想要做的是将棋子放在实际的棋盘上。所以这是把黑色的碎片放在上面。

PS如果你也可以在底部的一组件(白色)上给我一些帮助。

4

3 回答 3

4

除了修复循环之外,您还可以明确放置片段,使其更具可读性

int[,] board = new[,]{{1,0,1,0,1,0,1,0},
                      {0,1,0,1,0,1,0,1},
                      {1,0,1,0,1,0,1,0},
                      {0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0},
                      {0,1,0,1,0,1,0,1},
                      {1,0,1,0,1,0,1,0},
                      {0,1,0,1,0,1,0,1}};
于 2013-05-29T11:42:18.383 回答
0

您错过了告诉我们您想要实现的具体目标。我在您的代码中看到了几个大错误,所以我假设您没有完全掌握 for 循环的工作原理。我希望我在这里试图解释它不是太冒昧


循环

For循环用于多次执行相同部分的代码。执行多少次取决于您设置的条件。大多数情况下,您会以这种格式看到它:

for (int i = 0; i < n; i++)
{
    // Some code
}

{这个 for 循环在括号 (和})内执行代码n。这不仅是定义循环的方法。更彻底的循环定义如下:

for (<initialization>; <condition>; <afterthought>)
  • 初始化- 您可以使用一些循环所需的变量。这在循环内的代码第一次执行之前执行一次。这是可选的,您可以将其留空并使用之前在条件中声明的变量。
  • 条件- 这是在循环内每次执行代码之前执行的。如果条件表达式的计算结果为true,则执行循环。一旦执行了循环并执行了事后考虑,就会一次又一次地评估条件,直到它评估为false。条件也是可选的。如果你忽略它,在 C# 中循环将再次执行,直到你以不同的方式中断循环。
  • 事后诸葛亮- 每次循环内的代码完成执行时都会执行此操作。这通常用于增加循环所依赖的变量。这也是可选的。

修复你的代码

我假设您想像棋盘一样在 8x8 矩阵中标记字段,尽管您的问题中没有说明这一点。你可以这样做:

// For each row in a board (start with 0, go until 7, increase by 1)
for (int i = 0; i < 8; i++)
{
    // start coloring the row. Determine which field within the row needs
    // to be black. In first row, first field is black, in second second
    // field is black, in third row first field is black again and so on.
    // So, even rows have black field in first blace, odd rows have black
    // on second place.
    // We calculate this by determining division remained when dividing by 2.
    int firstBlack = i % 2;
    // Starting with calculated field, and until last field color fields black.
    // Skip every second field. (start with firstBlack, go until 7, increase by 2)
    for (int j = firstBlack; j < 8; j += 2)
    {
        // Color the field black (set to 2)
        board[i][j] = 2;
    }
}

你可以看到我的评论内联。


代码中的大错误

// This loop would be executed only once. It goes from 0 to less than 1 and is increased
// after first execution. You might as well done it without the loop.
for (int i = 0; i < 1; i++)
{
    // This doesn't make sense, because you use i in condition, and initialize
    // and update j.
    // Also, you are trying to update j, but you are not doing so. You are not
    // assigning anything to you. You should do j+=2 to increase by two. Or you
    // can do j = j + 2
    for (int j = 0; i < 8; j+2)
    {
        // I didn't really understand what you were trying to achieve here
        board[ i, j ] = 2;
        board[( i + 1 ), j ] = 2;
        board[( i + 2 ), j ] = 2;
    }
}  
于 2013-05-29T12:03:12.307 回答
0

模数可以解决问题,但我认为 TonyS 的答案是我的第一反应,我更喜欢下面显示的答案。

  char[,] board = new char[8,8];
    private void InitializeBoard()
    {
        string BoardRepresentation = "";
        //for every board cell
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                //initialize board cell
                board[i, j] = '0';

                if (j <= 2)//black top
                {
                    //Modulo is the trick
                    if ((j - i) == 0 || ((j - i) % 2) == 0)
                    {
                        board[i, j] = 'B';
                    }
                }
                else if (j >= 5) //white bot
                {
                    if ((j - i) == 0 || ((j - i) % 2) == 0)
                    {
                        board[i, j] = 'W';
                    }
                }
            }
        }

        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 8; i++)
            {
                BoardRepresentation += board[i, j] + " ";
            }
            BoardRepresentation += Environment.NewLine;
        }
    }
于 2013-05-29T12:03:24.737 回答