请教MinMax算法问题(C++)

ohyeah1988 2014-08-07 01:53:12
以下是我写的一段代码,用MinMax算法来解决简单的KalahGame (初始状态是 {1,1,1,0,1,1,1,0}。我是先手。
代码运行出来之后的问题是:运行结果显示,我不是先手了。。。。。请各位好心人帮忙看一眼。。。谢谢大家!
代码拷贝可以直接运行。
在代码中可以看到,我的初始条件设置的是我先手的,然后按照递归,虽然从最终状态向后回溯,但是回溯到最外层,也应该是我先手。但是output显示的结果却不是这样的。
还有就是,我发现我换成小size 的初始条件,比如 {1,1,0,1,1,0},就没有这个问题,然后随着size增大,会间隔性的出现这个问题。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<iostream>
#include<limits.h>

using namespace std;

int kalahsolver(int table [], int len, bool player, int & movedhouse);
void getson(int sontable[], int len, int startpos, bool & nextplayer);

int main() {
/////////////////
int table[8] = {1,1,1,0,1,1,1,0};
int length = (sizeof(table)/sizeof(table[0]));
/////////////////
// record the player and the position
int movedhouse;
int finalscore = kalahsolver(table, length, true, movedhouse);
//
// output the expected final score
if (movedhouse == -1)
cout<<"This is Game Over and you collect the rest"<<endl;
else if (movedhouse == -2)
cout<<"This is Game Over and I collect the rest"<<endl;
else
cout<<"I should move the "<<(movedhouse+1)<<"th house"<<endl;
cout<<"My expected final score of this move is "<<finalscore<<endl;
while(1) {
}
return 0;
}

int kalahsolver (int table [], int len, bool player, int & movedhouse) {

// get the domain of my area and his area
int myhead = 0; int mystore = (len/2)-1; int mytail = mystore-1;
int yourhead = mystore+1; int yourstore = len-1; int yourtail = yourstore-1;
// check if empty, if any empty, the game stops
int myremain=0; int yourremain=0;
for (int i=myhead; i<=mytail; ++i) {
myremain += table[i];
}
for (int i=yourhead; i<=yourtail; ++i) {
yourremain += table[i];
}
if (player)
{
if ((myremain==0)||(yourremain==0)){
cout<<"i finish"<<endl;
movedhouse=-2;
return (table[mystore]+myremain);
}
else {
int score = INT_MIN;
for (int i = myhead; i<=mytail; ++i) {
if (table[i]>=1) {
bool nextplayer;
int * sontable = new int [len];
for (int j = 0; j<len; ++j) {
sontable[j] = table[j];
}
getson(sontable, len, i, nextplayer);
int sonscore = kalahsolver(sontable, len, nextplayer, movedhouse);
delete [] sontable;
if (sonscore>score) {
cout<<player<<" update the position "<<i<<endl;
movedhouse = i;
score = sonscore;
}
}
}
return score;
}
}
else
{
if ((myremain==0)||(yourremain==0)){
cout<<"you finish"<<endl;
movedhouse = -1;
return (table[mystore]+myremain);
}
else {
int score = INT_MAX;
for (int i = yourhead; i<=yourtail; ++i) {
if (table[i]>=1) {
bool nextplayer;
int * sontable = new int [len];
for (int j = 0; j<len; ++j) {
sontable[j] = table[j];
}
getson(sontable, len, i, nextplayer);
int sonscore = kalahsolver(sontable, len, nextplayer, movedhouse);
delete [] sontable;
if (sonscore<score) {
cout<<player<<" update the position "<<i<<endl;
movedhouse = i;
score = sonscore;
}
}
}
return score;
}
}
}



//////////////////////////////////////////////////////////

void getson (int sontable[], int len, int startpos, bool & nextplayer) {

// get the domain of my area and his area
int myhead = 0; int mystore = (len/2)-1; int mytail = mystore-1;
int yourhead = mystore+1; int yourstore = len-1; int yourtail = yourstore-1;
// get number of stones
int num = sontable[startpos];
sontable[startpos] = 0;
// move stones as long as there is still any
if (startpos<=mytail) {
// means my turn, so skip your store
int j = startpos;
for (int i = 1; i<=num; ++i) {
++j;
if (j>=yourstore) {
j=0;
}
sontable[j] += 1;
}
// see where the last piece is
if (j==mystore) {
nextplayer = true;
}
else if (j<=mytail) {
if (sontable[j]==1) {
int oppo = len-2-j;
sontable[mystore] += (sontable[j]+sontable[oppo]);
sontable[j] = 0;
sontable[oppo] = 0;
}
nextplayer = false;
}
else {
nextplayer = false;
}
}
else {
// means your turn, so skip my store
int j = startpos;
for (int i = 1; i<=num; ++i) {
++j;
if (j>=len){
j = 0;
}
else if (j==mystore) {
++j;
}
sontable[j] += 1;
}
// see where is the last piece
if (j==yourstore) {
nextplayer = false;
}
else if (j>=yourhead){
if (sontable[j]==1) {
int oppo = len-2-j;
sontable[yourstore] += (sontable[j]+sontable[oppo]);
sontable[j] = 0;
sontable[oppo] = 0;
}
nextplayer = true;
}
else {
nextplayer = true;
}
}
}

//////////////////////
...全文
402 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧