java - Minimax Connect 4 AI trouble -


i making connect 4 ai, except game continues until 42 spaces filled.
score kept every 4 in row gets 1 point.

public int[] max_value(gameboard playboard, int depth){     gameboard temp = new gameboard(playboard.playboard);     int h = 0, temph = 999, tempcol=0;     int mydepth = depth - 1;     int[] temph2 = new int[2];     boolean nochildren = true;     if(mydepth != -1){         for(int = 0; < 7; i++){             if(temp.isvalidplay(i)){                 count++;                 temp.playpiece(i);                 nochildren = false;                 temph2 = min_value(temp, mydepth);                 if(temph2[1] < temph){                     temph=temph2[1];                     tempcol = i;                 }                 temp.removepiece(i);             }         }     }        int[] x = new int[2];     if(nochildren){         h = temp.getheuristic();     }     else{         h = temph;         x[0]=tempcol;     }     x[1]=h;     return x;  }  public int[] min_value(gameboard playboard, int depth){     gameboard temp = new gameboard(playboard.playboard);     int h = 0, temph = -999, tempcol=0;     int mydepth = depth - 1;     int[] temph2 = new int[2];     boolean nochildren = true;     if(mydepth != -1){         for(int = 0; < 7; i++){             if(temp.isvalidplay(i)){                 count++;                 temp.playpiece(i);                 nochildren = false;                 temph2 = max_value(temp, mydepth);                 if(temph2[1] > temph){                     temph=temph2[1];                     tempcol = i;                 }                 temp.removepiece(i);             }         }     }        int[] x = new int[2];     if(nochildren){         h = temp.getheuristic();     }     else{         h = temph;         x[0]=tempcol;     }     x[1]=h;     return x;  } 

i feel stumbled through everything, , feels terrible code. however, have never attempted before, , appreciate input. can't tell going wrong. evaluation function gives 1 point each 4 in row can find given state. main function calls min_value function start things off depth of 10.

i attempting return column value of heuristic. hope have provided enough information. insight.

allright, after implementing methods not shown (like evaluation, playmove, remove etc.) able debug this. assuming these functions implemented in correct way in version, mistake never call evaluation function if depth -1:

you have this:

[...]if(mydepth != -1) {/*restofthecode*/}[...] 

but need this:

[...]if(mydepth == -1) { return temp.getheuristic(); } /*restofthecode*/ [...] 

that way, whenever reach depth -1 (a leaf in minimax tree), board evaluated , value returned (which excactly need in minimax).

do modification in both parts (min , max) , shuold allright. if there other problems, feel free ask.


Comments