用 C 优化国际象棋游戏

Optimizing Chess Game in C

我正在用 C 语言制作一个简单的国际象棋游戏,我想知道我可以对其进行哪些优化。目前,我有一个结构游戏,它具有游戏的当前状态(主菜单、暂停菜单、播放等)、回合、3 个用作布尔值的整数、指向棋盘的指针和指向所选棋子的指针:

typedef struct game{
    ChessBoard *board;
    ChessPiece *selectedPiece;

    ChessColor turn;
    State state;

    //Booleans
    int inGame;
    int checkmate;
    int check;
}Game;

棋盘有一个二维指针数组,指向棋子、玩家和最后移动的棋子(对于过路人):

typedef struct chessboard{
    ChessPiece *pieces[8][8];

    Player *player1;
    Player *player2;

    ChessPiece *lastMovedBlackPiece;
    ChessPiece *lastMovedWhitePiece;
} ChessBoard;

最后是作品:

typedef struct chesspiece{
    //Properties
    int x;
    int y;
    ChessColor color;

    //Type
    Type type;

    int numberOfMoves;
} ChessPiece;

每次玩家选择一个棋子时,程序都会计算并显示所选棋子的有效着法,并且在移动一个棋子后,程序会通过检查可能的着法来验证敌方国王是被控制还是被将死棋子的数量(如果棋子可以保护他,如果国王可以移动到别处)。

我看到有人为有效的移动创建列表而不是每次都计算,但我必须为每个棋子创建一个列表并在回合改变时计算玩家的所有可能移动?这会提高性能吗?我还看到板子只是一个数组,那会不会有更好的性能?

基本上,我可以在我的代码中进行哪些优化以获得更好的性能?

代码:https://github.com/WalrusNine/chess-game-c

Basically, which are the possible optimizations I can do in my code for better performance?

这是一个博大精深的话题。我在 Java (https://github.com/amir650/BlackWidow-Chess) 中编写了一个功能齐全的国际象棋引擎,我可以告诉你,你可以做很多事情。

首先阅读:https://chessprogramming.wikispaces.com。首先关注引擎的正确性。它是否处理转换、过路、检查、将死、发现检查等。如果不正确,性能无关紧要!

接下来编写一个 minimax 和评估函数 - 作为您的基本搜索程序,并测量它每秒可以为您评估的板数。

从那里开始,事情开始变得开放:

1) Alpha Beta Pruning
2) Board representation (bit-board vs array)
3) Null Move Heuristic
4) DB for opening
5) DB end engame
6) Transposition tables
7) ..it goes on

无论您进行何种优化,都要确保正确性不会倒退。这意味着要编写像样的单元测试。