Here is my code:
public static int minimax(int[][] board, int depth, boolean maximizingPlayer) { if(depth==0 || (!w && !s && !a && !d)) return ocena(board); // evaluation method int[] r = wygenerujRuchy(board); // generate all possible moves if(k2==0) k2 = r[0]; if(maximizingPlayer) { najWynik = -10000; for(int i=0; i<r.length; i++) { for(int k=0; k<game2048.wielkosc; k++) { //copy board for(int j=0; j<game2048.wielkosc; j++) { planszaL[k][j] = board[k][j]; } } val = minimax(zrobRuch(planszaL,r[i]), depth - 1, false); if(val>=najWynik) { k2 = r[i]; // the best move najWynik = val; // the best score } //System.out.println(najWynik); } //System.out.println(depth + "\t" + najWynik + "\t" + maximizingPlayer + "\t" + k2); return najWynik; // return the best score } else { najWynik = 10000; // score val = minimax(dodajKlocek(board), depth - 1, true); najWynik = Math.min(najWynik, val); // the worst score //System.out.println(najWynik); System.out.println(depth + "\t" + najWynik + "\t" + maximizingPlayer + "\t" + k2); return najWynik; // return score } }
I made a 2048 game solver and I want to implement mini-max alghoritm.
But it works only when I lunch with depth=1, this isn't good enough for me because the avarage score is 14000-15000 points and the best score is ~65000. I think my evaluation function is very good but I don't why it give random moves when depth>1.
Could you help me?