我想使用极小化极大搜索(带有alpha-beta剪枝)或者更确切地说是negamax搜索,来让计算机程序玩一种纸牌游戏。
这个纸牌游戏实际上包括4个玩家。因此为了能够使用极小化极大等算法,我将游戏简化为“我”对抗“其他人”。每次“移动”后,您可以客观地从游戏本身读取当前状态的评估。当4名玩家都已放置纸牌后,最高分数将赢得所有纸牌的总数,而且纸牌的价值也要计算在内。
由于不知道其他3个玩家手中的牌具体分布情况,我认为必须模拟所有可能的牌分布(“世界”),其中包括非你所拥有的牌。你有12张牌,而其他三个玩家总共有36张牌。
因此,我的方法是使用此算法,其中player
是1到3之间的数字,代表程序可能需要为其找到步骤的三个计算机玩家,而-player
则代表对手,即其他三个玩家的集合。
private Card computerPickCard(GameState state, ArrayList<Card> cards) {
int bestScore = Integer.MIN_VALUE;
Card bestMove = null;
int nCards = cards.size();
for (int i = 0; i < nCards; i++) {
if (state.moveIsLegal(cards.get(i))) { // if you are allowed to place this card
int score;
GameState futureState = state.testMove(cards.get(i)); // a move is the placing of a card (which returns a new game state)
score = negamaxSearch(-state.getPlayersTurn(), futureState, 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (score > bestScore) {
bestScore = score;
bestMove = cards.get(i);
}
}
}
// now bestMove is the card to place
}
private int negamaxSearch(int player, GameState state, int depthLeft, int alpha, int beta) {
ArrayList<Card> cards;
if (player >= 1 && player <= 3) {
cards = state.getCards(player);
}
else {
if (player == -1) {
cards = state.getCards(0);
cards.addAll(state.getCards(2));
cards.addAll(state.getCards(3));
}
else if (player == -2) {
cards = state.getCards(0);
cards.addAll(state.getCards(1));
cards.addAll(state.getCards(3));
}
else {
cards = state.getCards(0);
cards.addAll(state.getCards(1));
cards.addAll(state.getCards(2));
}
}
if (depthLeft <= 0 || state.isEnd()) { // end of recursion as the game is finished or max depth is reached
if (player >= 1 && player <= 3) {
return state.getCurrentPoints(player); // player's points as a positive value (for self)
}
else {
return -state.getCurrentPoints(-player); // player's points as a negative value (for others)
}
}
else {
int score;
int nCards = cards.size();
if (player > 0) { // make one move (it's player's turn)
for (int i = 0; i < nCards; i++) {
GameState futureState = state.testMove(cards.get(i));
if (futureState != null) { // wenn Zug gültig ist
score = negamaxSuche(-player, futureState, depthLeft-1, -beta, -alpha);
if (score >= beta) {
return score;
}
if (score > alpha) {
alpha = score; // alpha acts like max
}
}
}
return alpha;
}
else { // make three moves (it's the others' turn)
for (int i = 0; i < nCards; i++) {
GameState futureState = state.testMove(cards.get(i));
if (futureState != null) { // if move is valid
for (int k = 0; k < nCards; k++) {
if (k != i) {
GameState futureStateLevel2 = futureState.testMove(cards.get(k));
if (futureStateLevel2 != null) { // if move is valid
for (int m = 0; m < nCards; m++) {
if (m != i && m != k) {
GameState futureStateLevel3 = futureStateLevel2.testMove(cards.get(m));
if (futureStateLevel3 != null) { // if move is valid
score = negamaxSuche(-player, futureStateLevel3, depthLeft-1, -beta, -alpha);
if (score >= beta) {
return score;
}
if (score > alpha) {
alpha = score; // alpha acts like max
}
}
}
}
}
}
}
}
}
return alpha;
}
}
}
这似乎可以正常工作,但是对于深度为1(depthLeft=1
),程序平均需要计算50,000个移动(放置卡片)。当然,这太多了!
那么我有以下问题:
- 实现是否正确?您能否像这样模拟游戏?特别是关于不完美的信息?
- 如何提高算法的速度和工作负载?
- 例如,我可以将可能的移动集减少到随机的50%,以提高速度,同时保持良好的结果吗?
- 我发现UCT算法是一个好的解决方案(也许)。您知道这个算法吗?您能帮我实现它吗?