c++ - 棋子移动错误

标签 c++

我最近开始为一个大学项目开发​​一个用 C++ 编写的国际象棋引擎,但我的棋子移动功能遇到了问题。我知道棋子应该向前移动一格或沿对角线攻击一格。好吧,我的功能允许棋子攻击空白区域,我不知道为什么。我的棋盘分为两部分:一 block 记住棋子属于哪个玩家,另一 block 具有棋子的名称(如 q、Q、p、P...和空格)。一个提示将非常受欢迎。 (抱歉我的英语水平不佳)

代码如下所示:

bool move_P(int move_start_i, int move_start_j, int move_finish_i, int move_finish_j, char table[][9])
{
    switch (table[move_finish_i][move_finish_j])
    {
        case ' ':
        {
            if (move_start_i - 1 == move_finish_i) // move pawn
            {
                return true;
            }
        }
        default:
        {
            if (move_finish_i == move_start_i - 1 && move_finish_j == move_start_j - 1) // atack pawn ^<-
            {
                if (player[move_finish_i][move_finish_j] == player[move_start_i - 1][move_start_j - 1])
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else if (move_finish_i == move_start_i - 1 && move_finish_j == move_start_j + 1) // atack pawn ->^
            {
                if (player[move_finish_i][move_finish_j] == player[move_start_i - 1][move_start_j + 1])
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
    return false;
}

最佳答案

您的玩家检查错误,

player[move_finish_i][move_finish_j] == player[move_start_i - 1][move_start_j - 1]

必须是

player[move_finish_i][move_finish_j] == player[move_start_i][move_start_j]

你也应该使用

return player[move_finish_i][move_finish_j] != player[move_start_i][move_start_j];

主要问题是检查

move_start_i - 1 == move_finish_i

您必须添加对 j 位置的检查!

关于c++ - 棋子移动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180300/

相关文章:

c++ - 拥有比工作更多的 OpenMP 线程对性能有何影响?

c# - 字节数组编码

c++ - 数组变量是 C++ 中的引用吗?

c++ - iMX6 上的 Qt5 : how to make sure the hardware acceleration is in use?

c++ - 只有变量名的语句

c++将字符串参数分配给字符串

c++ - 使用 std::cout 的表格布局

php - 相似数据算法

c++ - 如何使用 XOR 加密字符串而不包含奇怪的字符?

c++ - std::function.target 返回 null