java - MinMaxAI checkers implementation -
i can't seem min max ai work. code far. error saying allscores array empty won't set move bestscored move. appreciated.
int[] move; public static int makemove(board board, int currentplayercolor, int maxingplayercolor){ int score = 0; int[] moveindex={}; if (rules.gameover(board)[0]==1){ if(rules.gameover(board)[1] == maxingplayercolor){ system.out.println("1"); return score = 1; } else if(rules.gameover(board)[1] == math.abs(maxingplayercolor-1)) { system.out.println("-1"); return score = -1; } else{ system.out.println("0"); return score = 0; } } else { int[][] availablemoves = board.availablemoves(board, currentplayercolor); int[] allscores = {}; system.out.println("loop length: "+ availablemoves.length); for(int x = 0; x<availablemoves.length; x++){ board temp = board; if(currentplayercolor == maxingplayercolor){ board.domove(availablemoves[x], temp, 7); } else { board.domove(availablemoves[x], temp, 0); } system.out.println(""+x); allscores = add(makemove(temp,math.abs(maxingplayercolor-1), maxingplayercolor), allscores); system.out.println("allscore len "+ allscores.length); } if(currentplayercolor == maxingplayercolor){ for(int x = 0; x<allscores.length; x++){ if(score < allscores[x]){ allscores = add(score, allscores); } } for(int x = 0; x<allscores.length; x++){ if(score == allscores[x]){ moveindex = add(x,moveindex); } } system.out.println("max "+moveindex.length); move = availablemoves[rand.nextint(moveindex.length)]; } else { for(int x = 0; x<allscores.length; x++){ if(score > allscores[x]){ score = allscores[x]; } } for(int x = 0; x<allscores.length; x++){ if(score == allscores[x]){ moveindex = add(x,moveindex); } } system.out.println("min "+moveindex.length); move = availablemoves[rand.nextint(moveindex.length)]; } } return 0; }
you're trying add elements onto array
allscores = add(...
but isn't how works. can't add elements onto array, can change existing elements.
so when make array, have this:
int[] allscores = {};
but creates empty array , can't that. instead, declare this:
int[] allscores = new int[size];
replacing size
number of elements want array hold.
then can access , change elements this:
allscores[index] = somenumber;
there formatting , syntax errors. try using text editor or ide includes syntax highlighting find easy mistakes.
also read on this guide learn arrays.
Comments
Post a Comment