简体   繁体   English

C++ 显示可能的象棋在空棋盘中移动

[英]C++ display possible bishop moves in an empty chess board

This task asks to display possible bishop moves in a chessboard (8 x 8) as in an example shown below:此任务要求在棋盘 (8 x 8) 中显示可能的象棋移动,如下所示:

x = 4, y = 4
1 0 0 0 0 0 1 0,
0 1 0 0 0 1 0 0,
0 0 1 0 1 0 0 0,
0 0 0 2 0 0 0 0,
0 0 1 0 1 0 0 0,
0 1 0 0 0 1 0 0,
1 0 0 0 0 0 1 0,   
0 0 0 0 0 0 0 1

Image for clear understanding图片便于理解

    #include <iostream>

using namespace std;

int main() {
int array[8][8], x , y;

for (int i = 0; i < 8; i++)
{
    for (int j = 0; j < 8; j++)
    {
        array[i][j] = 0;
    }
}

cout << "Input x coordinate: ";
cin >> x;

cout << "Input y coordinate: ";
cin >> y;

for (int i = 0; i < 8; i++) 
    { // 1st diagonal
        array[x + i][y + i] = 1;
        array[x - i][y - i] = 1;
    }

    for (int i = 0; i < 8; i++) 
    { // 2nd diagonal
        array[x + i][y - i] = 1;
        array[x - i][y + i] = 1;
    }

array[x][y] = 2;

for (int i = 0; i < 8; i++) //Cout
{
    for (int j = 0; j < 8; j++)
    {
        cout << array[i][j];
    }
    cout << endl;
}

return 0;
}

It seems to work only with 1st diagonal它似乎只适用于第一个对角线

This is more of an algorithm/math answer than C++.这比 C++ 更像是一个算法/数学答案。

Suppose the grid's bottom left point is the origin (ie i = 0, j = 0 ), and the coordinate of the top right point in the grid is i=7, j=7 .假设网格的左下角为原点(即i = 0, j = 0 ),网格右上角的坐标为i=7, j=7

A bishop that is on i=0, j=0 can hit anything on these two lines: i=0, j=0上的主教可以在这两行上命中任何内容:

i = j and i = - j i = ji = - j

When you put the bishop at point x, y instead of 0,0 , these lines change to:当您将主教放在点x, y而不是0,0时,这些行变为:

i - x = j - y and i - x = - (j - y) i - x = j - yi - x = - (j - y)

Now, you can iterate all points in the matrix and check which ones satisfy the line equation:现在,您可以迭代矩阵中的所有点并检查哪些点满足线方程:

int main() {
  int x, y;

  std::cout << "Input x coordinate: ";
  std::cin >> x;

  std::cout << "Input y coordinate: ";
  std::cin >> y;

  int array[8][8];

  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if ((i - x == j - y) || (i - x == -(j - y))) {
        array[i][j] = 1;
      } else {
        array[i][j] = 0;
      }
    }
  }

  array[x][y] = 2;

  // We should print the matrix from top to bottom.
  // j represents the y coordinate, and i represents the x coordinate.
  for (int j = 7; j >= 0; j--) {
    for (int i = 0; i < 8; i++) {
      std::cout << array[i][j];
    }
    std::cout << std::endl;
  }
  return 0;
}

It looks like it's working for both diagonals when I run it.当我运行它时,它看起来对两个对角线都有效。 There is a small bug that might mess things up though.有一个小错误可能会搞砸。 You'll want to add bounds checks like this:您需要像这样添加边界检查:

    for (int i = 0; i < 8; i++) 
    { // 1st diagonal
        if (x + i < 8 && y + i < 8)
            array[x + i][y + i] = 1;

        if (x - i >= 0 && y - i >= 0)
            array[x - i][y - i] = 1;
    }

    for (int i = 0; i < 8; i++) 
    { // 2nd diagonal
        if (x + i < 8 && y - i >= 0)
            array[x + i][y - i] = 1;

        if (x - i >= 0 && y + i < 8)
            array[x - i][y + i] = 1;
    }

Without these bounds checks you'll be accessing elements outside of the array bounds and could be messing up other entries in the array.如果没有这些边界检查,您将访问数组边界之外的元素,并且可能会弄乱数组中的其他条目。 There are other ways to do the bounds checking, but this might be the easiest.还有其他方法可以进行边界检查,但这可能是最简单的。

To illustrate the issue, assume x = 4 and then in the for loop when i = 5 .为了说明这个问题,假设x = 4 ,然后在i = 5时进入 for 循环。 When you're indexing into the array with array[x - i] that'll be the same as array[-1] .当您使用array[x - i]对数组进行索引时,它将与array[-1]相同。 When you index into an array with a negative value you'll be messing with the wrong memory.当你索引一个负值的数组时,你会弄乱错误的 memory。

I would start with a very simple case.我将从一个非常简单的案例开始。

Suppose we want to plot the diagonal matrix of size 4x4:假设我们想要 plot 大小为 4x4 的对角矩阵:

 i  0 1 2 3
j   
0   1 0 0 0
1   0 1 0 0
2   0 0 1 0
3   0 0 0 1

We have a non zero value when:在以下情况下,我们有一个非零值:

i = j   (I)

Now, suppose we shift this diagonal horizontally so that the non-zero value of the first line is located at X 0 .现在,假设我们水平移动这条对角线,使第一行的非零值位于 X 0处。 For example, for X 0 = 1:例如,对于 X 0 = 1:

      X0

 i' 0 1 2 3
j'  
0   0 1 0 0
1   0 0 1 0
2   0 0 0 1
3   0 0 0 0

The shifted coordinates are:平移后的坐标为:

i' = i + X0 (II)

Doing the same for shifting vertically by Y 0 :对垂直移动 Y 0执行相同的操作:

j' = j + Y0 (III)

With (II) and (III) in (I) we have:有了(I)中的(II)和(III),我们有:

i' - X0 = j' - Y0   (IV)

Now we do the same for the antidiagonal matrix:现在我们对反对角矩阵做同样的事情:

 i  0 1 2 3
j   
0   0 0 0 1
1   0 0 1 0
2   0 1 0 0
3   1 0 0 0

We have a non zero value when:在以下情况下,我们有一个非零值:

j = 3 - i   (V)

Shifting horizontally by X 0 we have:水平移动 X 0我们有:

i' = -3 + i + X0    (VI)

Shifting vertically by Y 0 :垂直移动 Y 0

j' = j + Y0         (VII)

With (VI) and (VII) in (V): (V)中的(VI)和(VII):

j' - Y0 = X0 - i'   (VIII)

The code just needs to check for (IV) and (VIII):代码只需要检查 (IV) 和 (VIII):

#include <iostream>
using namespace std;

int main()
{
    int array[8][8] = {0,};

    int x = 0;
    int y = 0;
    
    cout << "Input x coordinate: ";
    cin >> x;
    
    cout << "Input y coordinate: ";
    cin >> y;
    
    for (int j = 0; j < 8; j++)
        for (int i = 0; i < 8; i++) 
            if (   i - x == j - y
                || j - y == x - i)
                array[j][i] = 1;
    
    array[y][x] = 2;

    for (int j = 0; j < 8; j++)
    {
        for (int i = 0; i < 8; i++)
            cout << array[j][i];
        cout << endl;
    }
    
    return 0;
}

Note that the matrix is plotted following the convention array[lines][columns] , so to set cartesian coordinates we write array[y][x] .请注意,矩阵是按照约定array[lines][columns]绘制的,因此要设置笛卡尔坐标,我们编写array[y][x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM