跳棋协助

本文关键字: | 更新日期: 2023-09-27 18:15:47

我只是想知道是否有更简单的方法:

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

我要做的是把棋子放在实际的棋盘上。这是为了把黑色的部分放在顶部。

注:如果你还能给我一些帮助在底部的组件(白色)。

跳棋协助

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

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

模可以解决问题,但我认为TonyS的答案是我的第一反应,我更喜欢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;
        }
    }

你没告诉我们你到底想要实现什么。我在您的代码中发现了几个大错误,因此我假定您还没有完全掌握for循环的工作原理。我希望我在这里解释它不会太冒昧


For循环

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

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

for循环执行括号({})内的代码n次。这不是定义循环的唯一方法。更详细的循环定义如下:

for (<initialization>; <condition>; <afterthought>)
  • Initialization -可以设置一些循环所需的变量。这在循环中的代码第一次执行之前执行一次。这是可选的,你可以将其保留为空,并使用之前在条件中声明的变量。
  • Condition -在循环内每次执行代码之前执行。如果条件表达式求值为true,则执行循环。一旦执行了循环,并且执行了,就会一次又一次地计算condition,直到计算结果为false。条件也是可选的。如果你省略它,c#循环将再次执行,直到你以不同的方式打破循环。
  • Afterthought -每次循环内的代码完成执行时执行。这通常用于增加循环所依赖的变量。

修改代码

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

// 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;
    }
}  
相关文章:
  • 没有找到相关文章