[英]Returning multiple minimax values [score, x, y] using structs in C
我的代碼中的問題在於 minimax 算法,更具體地說是返回值。 我目前用 C++ 編寫的解決方案如下,使用元組、返回分數、x 和 y 位置。 我決定使用 struct 在 C 中做類似的事情。
在給定當前板子 position [3468] 的情況下,該算法會正確找到所有可能的最終狀態,但不會返回正確的下一步移動坐標。
鑒於現在輪到 Xs,我們應該從 function 中得到 [score = 0, x = 0, y = 1]。 目前返回的值為 [score = 1, x = 1, y = 0]。
我應該如何正確實現結構以使算法正常工作?
#include <stdio.h>
int s = 3; //Board size
int cnt = 0;
char player = 'X', opponent = 'O';
struct Best {
int score, x, y;
};
void printBoard(char v[][s]);
char eval(char v[][s]);
struct Best miniMax(char Table[][s], int depth, bool isMaximising); //Problem: structs?
int main(int argc, char const *argv[]) {
char b[3][3] = {
{'X','_','_'},
{'_','O','_'},
{'_','_','_'}
};
printBoard(b);
struct Best s = miniMax(b, 0, true);
printf("Possible games: %d\n", cnt);
cnt = 0;
printf("Score: %d, Best move: [%d][%d]\n", s.score, s.x, s.y);
return 0;
}
void printBoard(char v[][s]){
for (size_t i = 0; i < s; i++){
for (size_t j = 0; j < s; j++){
printf("%c|", v[i][j]);
}
printf("\n");
}
printf("\n");
}
char eval(char v[][s]){
for (size_t i = 0; i < s; i++) {
if (v[i][0] == v[i][1] && v[i][1] == v[i][2] && v[i][0] != '_') //Row
return v[i][0];
if (v[0][i] == v[1][i] && v[1][i] == v[2][i] && v[0][i] != '_') //Col
return v[0][i];
}
if (v[0][0] == v[1][1] && v[1][1] == v[2][2] && v[0][0] != '_') //Diag
return v[0][0];
if (v[0][2] == v[1][1] && v[1][1] == v[2][0] && v[0][2] != '_') //Diag2
return v[0][2];
for (size_t i = 0; i < s; i++) {
for (size_t j = 0; j < s; j++) {
if (v[i][j] == '_')
return '\0';
}
}
return '=';
}
以下是 C 中存在問題的 miniMax function 實現:
struct Best miniMax(char Table[][s], int depth, bool isMaximising) {
struct Best v;
char result = eval(Table);
if (result == 'X') {
cnt++;
v.score = 1;
v.x = 0;
v.y = 0;
return v;
}
if (result == 'O') {
cnt++;
v.score = -1;
v.x = 0;
v.y = 0;
return v;
}
if (result == '=') {
cnt++;
v.score = 0;
v.x = 0;
v.y = 0;
return v;
}
if (isMaximising) {
int bestScore = -100;
for (size_t i = 0; i < s; i++) {
for (size_t j = 0; j < s; j++) {
if (Table[i][j] == '_') {
Table[i][j] = player;
v = miniMax(Table, depth + 1, false);
if (v.score > bestScore) {
bestScore = v.score;
v.score = bestScore;
v.x = i;
v.y = j;
}
Table[i][j] = '_';
}
}
}
return v;
} else {
int bestScore = 100;
for (size_t i = 0; i < s; i++) {
for (size_t j = 0; j < s; j++) {
if (Table[i][j] == '_') {
Table[i][j] = opponent;
v = miniMax(Table, depth + 1, true);
if (v.score < bestScore) {
bestScore = v.score;
v.score = bestScore;
v.x = i;
v.y = j;
}
Table[i][j] = '_';
}
}
}
return v;
}
}
C++ 中的實現:
#include <tuple>
#include <iostream>
using namespace std;
tuple <int, int, int> Minimax(char Board[][s], int depth, bool isMaximising){
char res = eval(Board);
if (res == 'X') {
cnt++;
return {1, 0, 0};
}
if (res == 'O') {
cnt++;
return {-1, 0, 0};
}
if (res == '=') {
cnt++;
return {0, 0, 0};
}
if (isMaximising) {
int bestScore = -100;
int px = 0, py = 0;
for (size_t i = 0; i < s; i++) {
for (size_t j = 0; j < s; j++) {
if (Board[i][j] == '_') {
Board[i][j] = player;
auto [score, x, y] = Minimax(Board, depth + 1, false);
if (score > bestScore) {
bestScore = score;
px = i;
py = j;
}
Board[i][j] = '_';
}
}
}
return {bestScore, px, py};
} else {
int bestScore = 100;
int qx = 0, qy = 0;
for (size_t i = 0; i < s; i++) {
for (size_t j = 0; j < s; j++) {
if (Board[i][j] == '_') {
Board[i][j] = opponent;
auto [score, x, y] = Minimax(Board, depth + 1, true);
if (score < bestScore) {
bestScore = score;
qx = i;
qy = j;
}
Board[i][j] = '_';
}
}
}
return {bestScore, qx, qy};
}
}
基本問題是,每次 miniMax 遞歸調用自身時,它都會用返回的值覆蓋v
,從而丟失任何早期迭代中保存的最佳值。 所以你總是從最后一次測試的 position 中得到 x/y,而不是從最好的那個中得到。
添加一個新的struct Best best
value,它是您在找到更好的移動時更新並返回的那個。 請注意,您也不需要bestScore
local 然后 - 您可以使用best.score
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.