package ex15; import java.io.IOException; public class MiniMaxPlayer extends Player { int playerTurn; public int pickMove(Board b) throws NoMoveAvailableException, IllegalMoveException, CloneNotSupportedException, IOException, ClassNotFoundException { KalahBoard kb = (KalahBoard) b.makeCopy(); playerTurn = kb.getTurn(); return calcMove(kb); } public int calcMove(KalahBoard kb){ int[] scoreArr = new int[6]; int count = 0; KalahBoard[] kalahTemp = new KalahBoard[6]; KalahBoard kalahb = kb.makeCopy(); int temp = (int) (java.lang.Math.random()*2)+3; // temp = java.lang.Math.round(temp); int currKalah = kalahb.getTurn(); int score = 0; int[] stoneNumArr = {1,2,3,4,5,6}; for(int j = 1;j<7;j++){ int temp2 = kalahb.getTurn(); try{ kalahb.makeMove(temp2, j); score = score+(kalahb.getKalah(kalahb.getTurn()) - currKalah); kalahTemp[j-1] = kalahb; scoreArr[j-1] = scoreArr[j-1] + score; score = 0; if(temp2 == kalahb.getTurn()){ score=score+5; } kalahb = kb.makeCopy(); }catch(IllegalMoveException ime){ score = 0;} } for(int i = 0;i<6;i++){ scoreArr[i] = calcBestMove(count, kalahTemp[i]);} sort(scoreArr, kalahTemp, stoneNumArr); if(temp == 3) return stoneNumArr[3]; else if(temp == 4) return stoneNumArr[4]; else return stoneNumArr[5]; // return scoreArr[temp]; } public int calcBestMove(int count, KalahBoard kb){ KalahBoard kalahb = kb.makeCopy(); KalahBoard maxkb = null; int currKalah = kalahb.getTurn(); int score = 0; int scoreMax = -100; if(count <5){ for(int j = 1;j<7;j++){ int temp2 = kalahb.getTurn(); try{ if(kalahb.getStones(temp2, j) >0){ kalahb.makeMove(temp2, j); if(temp2 != playerTurn){ score = score-(kalahb.getKalah(kalahb.getTurn()) - currKalah); if(temp2 == kalahb.getTurn()){ score=score-5; } if(score > scoreMax){ scoreMax = score; maxkb = kalahb; } } else{ score = score+(kalahb.getKalah(kalahb.getTurn()) - currKalah); if(temp2 == kalahb.getTurn()){ score=score+5; } if(score > scoreMax){ scoreMax = score; maxkb = kalahb; } } score = 0; kalahb = kb.makeCopy(); }else{ // System.out.println("Test"); } }catch(IllegalMoveException ime){} } // sort(scoreArr, kalahTemp); count++; return calcBestMove(count, maxkb); } else{ return scoreMax; } } public int findIndexOfNextSmallest(int[] unsorted, int startIndex) { int smallestIndex = startIndex; int i = startIndex; while(i