c++ 游戏移动生成器函数的清理代码

Cleaning code for move generator function for game in c++

我正在开发我自己的国际象棋游戏,我需要为棋盘上的某个棋子生成走法。该板以以下格式表示;

    int board[8][8];

棋盘上的每个数字代表游戏中的一个棋子。由于国际象棋有两个方面,即玩家是白色的,而玩家是黑色的。我在这个数组中也得到了负值。

例如白用整数2

表示

因此,黑色的,将用整数-2表示

这给我留下了一个非常简单的条件来检查它是黑色还是白色

回到问题,假设我需要为棋子生成移动

不了解国际象棋规则的朋友,如果新方格上有对手个棋子,棋子可以沿对角线移动1格。

我是白方玩家。现在我的情况是;

if (number < 0)

我是黑人玩家。现在我的情况是;

if (number > 0)

你看到问题了吗?此条件必须应用于所有棋子以及它们可以移动的所有可能位置。这是一个非常巨大的数字。现在的问题是我必须为白色播放器复制我的代码,为黑色播放器创建另一个函数并将其粘贴到其中。并且只是在这里和那里改变那个单一的条件,因为 all 其他国际象棋规则对两个玩家都是相同的。而不是具有 6 个功能。我现在有 12 个。

如何在不复制代码的情况下实现这一点?

我当前的代码使用两个独立的函数。我想过遍历数组中的每个元素并在生成黑色时更改符号,但这使得效率非常低

这是一个可编译的示例,我已尝试在保持清晰的同时将其保持在尽可能小的程度:

#include<iostream>
#include<vector>

int board[8][8] = {
    {0,0,0,0,0,0,0,0},
    {0,0,-1,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0},
    {0,0,1,0,0,0,0,0},
    {0,0,0,0,0,0,0,0}
};
// This board is the chess board and the little one representing the white pawn, and -1:black
std::vector < std::vector <int> > generatemovesPawn_white(int row,int col){
    std::vector<std::vector<int>> moves;
    std::vector<int> Move;
    if (board[row-1][col-1] < 0) {
        Move.push_back(row-1);
        Move.push_back(col-1);
        moves.push_back(Move);
    }
    return moves;
}

int main (){
    std::vector<std::vector<int>> moves = generatemovesPawn_white(6,1);
    return 1;
}

现在,对于黑色,我只需要将条件从:

if (board[row-1][col-1] < 0)

if (board[row-1][col-1] > 0)

希望你看到我的问题

您可以将条件提取到 bool 参数中:

std::vector<std::vector<int>> 
  generatemovesPawn(int row, int col, bool white_or_black)
{
    std::vector<std::vector<int>> moves;
    std::vector<int> Move;
    if (white_or_black) {
        Move.push_back(row-1);
        Move.push_back(col-1);
        moves.push_back(Move);
    }
    return moves;
}

并添加一个间接级别:

std::vector<std::vector<int>> 
  generatemovesPawn_white(int row, int col)
{
  if (board[row-1][col-1] < 0)
    return generatemovesPawn(row, col, true);
  if (board[row-1][col-1] > 0)
    return generatemovesPawn(row, col, false);
  return {};
}

在函数参数中添加一个 bool 来说明它是白色还是黑色,并添加一个 if 语句来检查 bool

std::vector < std::vector <int> > generatemovesPawn(int row,int col, bool wb)
{
//...


    if (wb && board[row-1][col-1] < 0)
    {
        //... do white stuff
    }
    else if (!wb && board[row-1][col-1] > 0)
    {
        //... do black stuff
    }
    //...
}

你需要让你的代码依赖于一个变量,而不是硬编码颜色。

例如,我没有尝试就更改了您的代码,如下所示。它采用一种颜色并根据颜色调整其行为。它使用变量 dx,dy 来存储每个颜色移动的方向棋子。

// This board is the chess board and the little one representing the white pawn, and -1:black

std::vector < std::vector <int> > generatemovesPawn(int row, int col, bool whiteToPlay)
{
    int dx = -1;
    int dy = whiteToPlay ? 1 : -1;
    int pawnId = whiteToPlay ? 1 : -1;

    std::vector<std::vector<int>> moves;
    std::vector<int> Move;
    if (board[row + dx][col + dy] == pawnId) 
    {
        Move.push_back(row + dx);
        Move.push_back(col + dy);
        moves.push_back(Move);
    }
    return moves;
 }

但我最初的建议是:在 https://codereview.stackexchange.com 上询问。这是这些问题的正确位置。