File size: 3,788 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0078", "name": "Implement a material + piece-square table evaluation function for chess"}
{"type": "message", "message": {"role": "system", "content": "You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance."}}
{"type": "message", "message": {"role": "user", "content": "Implement a material + piece-square table evaluation function for chess"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n\n// Piece values in centipawns\nconst int PIECE_VALUES[12] = {\n    100, 320, 330, 500, 900, 20000,  // white P N B R Q K\n    100, 320, 330, 500, 900, 20000   // black p n b r q k\n};\n\n// Piece-square tables (middlegame, from White's perspective)\nconst int PST_MG[6][64] = {\n    // Pawn\n    {  0,  0,  0,  0,  0,  0,  0,  0,\n      50, 50, 50, 50, 50, 50, 50, 50,\n      10, 10, 20, 30, 30, 20, 10, 10,\n       5,  5, 10, 25, 25, 10,  5,  5,\n       0,  0,  0, 20, 20,  0,  0,  0,\n       5, -5,-10,  0,  0,-10, -5,  5,\n       5, 10, 10,-20,-20, 10, 10,  5,\n       0,  0,  0,  0,  0,  0,  0,  0 },\n    // Knight\n    { -50,-40,-30,-30,-30,-30,-40,-50,\n      -40,-20,  0,  0,  0,  0,-20,-40,\n      -30,  0, 10, 15, 15, 10,  0,-30,\n      -30,  5, 15, 20, 20, 15,  5,-30,\n      -30,  0, 15, 20, 20, 15,  0,-30,\n      -30,  5, 10, 15, 15, 10,  5,-30,\n      -40,-20,  0,  5,  5,  0,-20,-40,\n      -50,-40,-30,-30,-30,-30,-40,-50 },\n    // Bishop\n    { -20,-10,-10,-10,-10,-10,-10,-20,\n      -10,  0,  0,  0,  0,  0,  0,-10,\n      -10,  0, 10, 10, 10, 10,  0,-10,\n      -10,  5,  5, 10, 10,  5,  5,-10,\n      -10,  0, 10, 10, 10, 10,  0,-10,\n      -10, 10, 10, 10, 10, 10, 10,-10,\n      -10,  5,  0,  0,  0,  0,  5,-10,\n      -20,-10,-10,-10,-10,-10,-10,-20 },\n    // Rook\n    {  0,  0,  0,  0,  0,  0,  0,  0,\n       5, 10, 10, 10, 10, 10, 10,  5,\n      -5,  0,  0,  0,  0,  0,  0, -5,\n      -5,  0,  0,  0,  0,  0,  0, -5,\n      -5,  0,  0,  0,  0,  0,  0, -5,\n      -5,  0,  0,  0,  0,  0,  0, -5,\n      -5,  0,  0,  0,  0,  0,  0, -5,\n       0,  0,  0,  5,  5,  0,  0,  0 },\n    // Queen\n    { -20,-10,-10, -5, -5,-10,-10,-20,\n      -10,  0,  0,  0,  0,  0,  0,-10,\n      -10,  0,  5,  5,  5,  5,  0,-10,\n       -5,  0,  5,  5,  5,  5,  0, -5,\n        0,  0,  5,  5,  5,  5,  0, -5,\n      -10,  5,  5,  5,  5,  5,  0,-10,\n      -10,  0,  5,  0,  0,  0,  0,-10,\n      -20,-10,-10, -5, -5,-10,-10,-20 },\n    // King\n    { -30,-40,-40,-50,-50,-40,-40,-30,\n      -30,-40,-40,-50,-50,-40,-40,-30,\n      -30,-40,-40,-50,-50,-40,-40,-30,\n      -30,-40,-40,-50,-50,-40,-40,-30,\n      -20,-30,-30,-40,-40,-30,-30,-20,\n      -10,-20,-20,-20,-20,-20,-20,-10,\n       20, 20,  0,  0,  0,  0, 20, 20,\n       20, 30, 10,  0,  0, 10, 30, 20 }\n};\n\nint evaluate(const BitboardBoard& board) {\n    int score = 0;\n    for (int pt = 0; pt < 6; pt++) {\n        uint64_t bb = board.pieces[pt];\n        while (bb) {\n            int sq = __builtin_ctzll(bb);\n            score += PIECE_VALUES[pt] + PST_MG[pt][sq ^ 56]; // flip for white\n            bb &= bb - 1;\n        }\n        bb = board.pieces[pt + 6];\n        while (bb) {\n            int sq = __builtin_ctzll(bb);\n            score -= PIECE_VALUES[pt + 6] + PST_MG[pt][sq]; // black uses same table\n            bb &= bb - 1;\n        }\n    }\n    // Mobility bonus\n    uint64_t mobility = countMobility(board);\n    score += popcount(mobility & board.occupancy[WHITE]) * 5;\n    score -= popcount(mobility & board.occupancy[BLACK]) * 5;\n    // King safety\n    score += evaluateKingSafety(board, WHITE);\n    score -= evaluateKingSafety(board, BLACK);\n    return board.sideToMove == WHITE ? score : -score;\n}"}}