예제 소스
장기로 이해하는 규칙 엔진과 평가 함수 탐색 · 파일 59개
Ai.h
#pragma once
#include "Game.h"
#include <string>
// 평가 함수 가중치 (기본값 = 기존 하드코딩 값)
struct EvalWeights {
int chariot = 130;
int cannon = 70;
int horse = 50;
int elephant = 30;
int advisor = 30;
int pawn = 20;
int pawnAdvance = 3; // 졸/병 한 줄 전진당
int minorFile = 2; // 마/상 중앙 열 근접
int minorRank = 1; // 마/상 중앙 행 근접
int chariotKingLine = 8; // 차가 상대 궁과 같은 줄
int kingCenter = 6; // 궁이 궁성 중앙
int checkPenalty = 18; // 장군 받는 쪽 감점
int hanBonus = 15; // 한 덤
// 궁 안전 (0이면 끔 = 기존 평가)
int palaceIntruder = 0; // 내 궁성(+앞 한 줄)에 들어온 상대 기물 하나당 감점
int palaceChariot = 0; // 그중 차면 추가 감점
int palaceGuard = 0; // 궁성 안의 내 사 하나당 가점
// 탐색 끝에서 잡는 수만 이어서 보는 깊이 (0이면 끔)
int qsearch = 0;
int material(PieceType type) const;
// "key=value" 줄 형식 파일. 실패 시 false
bool loadFile(const std::string& path);
};
class Ai {
public:
explicit Ai(int depth = 3);
void setDepth(int depth);
int depth() const { return depth_; }
void setWeights(const EvalWeights& w) { weights_ = w; }
const EvalWeights& weights() const { return weights_; }
// exclude: 첫 수 후보에서 뺄 수 (다른 수가 하나도 없으면 무시)
Move chooseMove(const Game& game, const std::vector<Move>& exclude = {}) const;
private:
int negamax(Game& game, int depth, int alpha, int beta) const;
int quiesce(Game& game, int qdepth, int alpha, int beta) const;
int evaluate(const Game& game) const;
int kingSafety(const Game& game, Side side) const;
int positionBonus(const Game& game, Pos pos, Piece piece) const;
int depth_ = 3;
EvalWeights weights_{};
};