在这个程序中,我们使用极大极小算法来对抗人工智能。这个密码不是我的。我只是想弄明白为什么它不起作用。请解释为什么它在虚空cmp函数中显示错误。
#include<stdio.h>
#include<iostream>
using namespace std;
char brd[]= {49,50,51,52,53,54,55,56,57};
int mv=0;
void board() {
cout << "|_" << brd[0] << "_|_" << brd[1] << "_|_" << brd[2] << "_|\n";
cout << "|_" << brd[3] << "_|_" << brd[4] << "_|_" << brd[5] << "_|\n";
cout << "|_" << brd[6] << "_|_" << brd[7] << "_|_" << brd[8] << "_|\n\n";
}
int iswin(char le,char ar[]) {
int cr,h,v;
cr= ar[0]==le && ar[4]==le && ar[8]==le || ar[2]==le && ar[4]==le && ar[6]==le;
h= ar[0]==le && ar[1]==le && ar[2]==le || ar[3]==le && ar[4]==le && ar[5]==le || ar[6]==le && ar[7]==le && ar[8]==le ;
v= ar[0]==le && ar[3]==le && ar[6]==le || ar[1]==le && ar[4]==le && ar[7]==le || ar[2]==le && ar[5]==le && ar[8]==le ;
return (cr||h||v); }这是个错误
void cmp() {
int bestscore=-1000,score,move,k,i;
for(i=0; i<=8; i++) {
if(brd[i]!='X'&& brd[i]!='O') {
k=brd[i];
brd[i]='O';
score=minimax(brd,0);
brd[i]=k;
if(score>bestscore) {
move=i;
bestscore=score;
} } }
brd[move]='O'; }这是导致错误的函数。它就在无效cmp之后。
int minimax(char cb[],int max) {
int i,bestscore,score,k;
if(iswin('O',cb))
return 100;
if(iswin('X',cb))
return -100;
if(draw())
return 0;
if(max) {
bestscore=-1000;
for(i=0; i<=8; i++) {
if(cb[i]!='X'&& cb[i]!='O') {
k=cb[i];
cb[i]='O';
score=minimax(cb,0);
cb[i]=k;
if(score>bestscore)
bestscore=score;
} }
return bestscore; }
else {
bestscore=800;
for(i=0; i<=8; i++) {
if(brd[i]!='X'&& brd[i]!='O') {
k=cb[i];
cb[i]='X';
score=minimax(cb,1);
cb[i]=k;
if(score<bestscore)
bestscore=score;
} }
return bestscore;
} }

发布于 2021-12-28 16:40:15
编译器清楚地解释了这个问题:它找不到标识符minmax。之所以找不到它,是因为到了定义iswin的时候,minmax还没有被声明。在代码中,minmax在使用后被声明(并定义),因此编译器找不到标识符。为了修复它,只需在iswin定义之前的任何地方声明它,使用:
int minimax(char cb[],int max);您可以在以下页面中了解更多有关它的内容:微软,myprogrammingnotes。
如果是原教旨主义者,最好拿一本好书。
https://stackoverflow.com/questions/70509667
复制相似问题